LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 03-01-2018, 07:03 PM   #1
mbelmer85
LQ Newbie
 
Registered: Mar 2018
Posts: 1

Rep: Reputation: Disabled
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.
 
Old 03-02-2018, 07:23 AM   #2
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,883
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
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
 
Old 03-02-2018, 07:56 AM   #3
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,892

Rep: Reputation: 7317Reputation: 7317Reputation: 7317Reputation: 7317Reputation: 7317Reputation: 7317Reputation: 7317Reputation: 7317Reputation: 7317Reputation: 7317Reputation: 7317
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!
 
Old 03-04-2018, 02:07 AM   #4
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,804

Rep: Reputation: 1202Reputation: 1202Reputation: 1202Reputation: 1202Reputation: 1202Reputation: 1202Reputation: 1202Reputation: 1202Reputation: 1202
+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
 
Old 03-06-2018, 02:24 AM   #5
AwesomeMachine
LQ Guru
 
Registered: Jan 2005
Location: USA and Italy
Distribution: Debian testing/sid; OpenSuSE; Fedora; Mint
Posts: 5,524

Rep: Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015
You can use on extract
Code:
# tar xf --strip-components=NUMBER file.tar
to trim off the leading paths.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Removing a matching pattern in begining of the file vrs Programming 2 03-19-2014 03:58 PM
tar: removing leading '/' from member names Aarif Linux - Newbie 5 04-03-2013 08:43 PM
/bin/tar: Removing leading `/' from member names guarriman Linux - General 16 02-09-2010 08:19 PM
/bin/tar: Removing leading `/' from member names IFeelYourPain Linux - General 11 12-01-2007 02:32 PM
tar: Removing leading `/' means? packets Linux - Newbie 3 11-08-2007 07:17 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 02:54 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration