LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Tar using Matching pattern and removing leading paths (https://www.linuxquestions.org/questions/linux-newbie-8/tar-using-matching-pattern-and-removing-leading-paths-4175624754/)

mbelmer85 03-01-2018 07:03 PM

Tar using Matching pattern and removing leading paths
 
I'm trying to backup the log files contained in /var/log while removing leading paths at creation.

without removing the leading paths I can do this simply with:

tar -cvf ~/backup.tar /var/log/*.log

unfortunately I can't seem to remove the leading paths. I have looked online and found using -C will change the working directory for the command but when I do so, it doesn't recognize the pattern glob and says it cannot find the file or directory *.log

tar -cvf ~/backup.tar /var/log/ *.log

I'm sure my syntax must be off and I've tried some silly variations.

rtmistler 03-02-2018 07:23 AM

I think if you go to the /var/log directory and perform that command this way, it may do what you want:
Code:

/var/logs:$ tar -cvf ~/backup.tar *.log

pan64 03-02-2018 07:56 AM

you can use --transform option to modify filenames, probably something like this:
Code:

tar -cvf ~/backup.tar --transform 's!/var/log!!' /var/log/*.log
not tested!

MadeInGermany 03-04-2018 02:07 AM

+1 for post#2.
Tip: cd in a subshell to preserve the current directory in the main shell.
Code:

pwd
(cd /var/log && tar -cvf ~/backup.tar *.log)
pwd


AwesomeMachine 03-06-2018 02:24 AM

You can use on extract
Code:

# tar xf --strip-components=NUMBER file.tar
to trim off the leading paths.


All times are GMT -5. The time now is 09:18 AM.