LinuxQuestions.org
Review your favorite Linux distribution.
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-11-2013, 12:44 PM   #1
Batistuta_g_2000
Member
 
Registered: Oct 2011
Posts: 85
Blog Entries: 1

Rep: Reputation: Disabled
How to save tar files so parent directory is not included?


This is making me dizzy!!!

I have a script which "awks" filenames from configuration file and creates a list of these files.
These files are then used to make a tar.gz file.

So far is working but I want to just get the files restored - not the parent directory. I have tried to "cd" in the script to /important_files/ and run the tar from there but no-dice!

What I got:
root@ubuntu:~/assignment1/backups# tar -zxvf backup-20130311-171803.tar.gz
home/ubuntu/assignment1/important_files/file1
home/ubuntu/assignment1/important_files/file2
home/ubuntu/assignment1/important_files/file3
home/ubuntu/assignment1/important_files/file4

What I want to see:
root@ubuntu:~/assignment1/backups# tar -zxvf backup-20130311-171803.tar.gz
file1
file2
file3
file4

Beginning:
Backup Filenames are added in a config file by user.
Then these filenames are copied to temp file.
So can't use $variable - location has to be absolute
Then tar is used to zip -T from temp to BACKUPS location.

All good but is backing up entire parent directories / paths rather than just the files without the paths.

Last edited by Batistuta_g_2000; 03-11-2013 at 12:45 PM.
 
Old 03-11-2013, 02:13 PM   #2
yowi
Member
 
Registered: Dec 2002
Location: Au
Distribution: Debian
Posts: 209

Rep: Reputation: 54
There's "-C" and "-T" which may help but post your script!
 
Old 03-11-2013, 04:12 PM   #3
Batistuta_g_2000
Member
 
Registered: Oct 2011
Posts: 85
Blog Entries: 1

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by yowi View Post
There's "-C" and "-T" which may help but post your script!
How come, do they supress the directory?
 
Old 03-11-2013, 11:38 PM   #4
shivaa
Senior Member
 
Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,800
Blog Entries: 4

Rep: Reputation: 286Reputation: 286Reputation: 286
Look, the command:
Code:
tar -zxvf backup-20130311-171803.tar.gz
...Will show you filenames according to what filenames you have used while creating the tar file i.e. what's mentioned in your $CONFILE.

So there are two ways to get only filename (not their absolute path), either add only filenames i.e. file1, file2 etc. in $CONFILE and create tar by going into /home/ubuntu/assignment1/important_files directory, as:
Code:
cd /home/ubuntu/assignment1/important_files
tar -zcvf backup-20130311-171803.tar.gz -T $CONFILE
Where, $CONFILE contains:
Code:
file1
file2
file3...
OR, on the other hand, you can use basename command to truncate filename from it's absolute path, as:
Code:
~$ basename /home/ubuntu/assignment1/important_files/file4 
file4
OR while creating the $CONFILE, you can use:
Code:
awk -F"/" '{print $5}' $CONFILE

Last edited by shivaa; 03-11-2013 at 11:40 PM.
 
Old 03-12-2013, 05:32 AM   #5
Batistuta_g_2000
Member
 
Registered: Oct 2011
Posts: 85
Blog Entries: 1

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by shivaa View Post
Look, the command:
Code:
tar -zxvf backup-20130311-171803.tar.gz
...Will show you filenames according to what filenames you have used while creating the tar file i.e. what's mentioned in your $CONFILE.

So there are two ways to get only filename (not their absolute path), either add only filenames i.e. file1, file2 etc. in $CONFILE and create tar by going into /home/ubuntu/assignment1/important_files directory, as:
Code:
cd /home/ubuntu/assignment1/important_files
tar -zcvf backup-20130311-171803.tar.gz -T $CONFILE
Where, $CONFILE contains:
Code:
file1
file2
file3...
OR, on the other hand, you can use basename command to truncate filename from it's absolute path, as:
Code:
~$ basename /home/ubuntu/assignment1/important_files/file4 
file4
OR while creating the $CONFILE, you can use:
Code:
awk -F"/" '{print $5}' $CONFILE
Great thanks a lot, but now I am having other problems:
I read from my confile line 21 - EOF and write > to "backuplist" then edit it to > "tempbackuplist" then add the "/" and write > back over "backuplist".
I then copy "backuplist" to directory where important_files are and try to create a tar.gz - but not finding the files.

So question is -
- where should I be running my tar command from?
- where should my backuplist be in order to be read?
backuplist:
/file1
/file2
/file3

[/code]
BACKUP_FILE=/root/assignment1/backups/backup-20130313-102446.tar.gz
BACKUP_TARGETS="backuplist.txt"
awk 'NR==21,NR==EOF' "$CONFILE" | grep -v confile.cfg > "backuplist.txt"
awk -F/ '{ print $6 }' "$BACKUP_TARGETS" > "tempbackuplist.txt"
sed -e 's/^/\//' "tempbackuplist.txt" > "$BACKUP_TARGETS"
echo -e "\nBacking up these files now:" >> "$LOGFILE"
cp "$BACKUP_TARGETS" "$IMPORTANT_FILES_DIRECTORY"
cd "$IMPORTANT_FILES_DIRECTORY"
echo "The present working directory is `pwd`"
sleep 2s
echo "$BACKUPTARGETS are to be backed up now"
sleep 2s
tar -czvf "$BACKUP_FILE" -T "$BACKUP_TARGETS" # > "$LOGOUT" 2> "$LOGERR"
echo "$TIMESTAMP BACKUP file $BACKUP_FILE created" # >> "$LOGFILE
[code]

Error:

+ tar -czvf /root/assignment1/backups/backup-20130313-102446.tar.gz -T backuplist.txt
tar: Removing leading `/' from member names
tar: /file1: Cannot stat: No such file or directory
tar: /file2: Cannot stat: No such file or directory
tar: /file9: Cannot stat: No such file or directory
 
Old 03-12-2013, 05:46 AM   #6
shivaa
Senior Member
 
Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,800
Blog Entries: 4

Rep: Reputation: 286Reputation: 286Reputation: 286
If you read my previous post, I had mentioned to run it in parent directory of file1, file2... like this:
Code:
BACKUP_FILE=/root/assignment1/backups/backup-20130313-102446.tar.gz
BACKUP_TARGETS="backuplist.txt"
awk 'NR==21,NR==EOF' "$CONFILE" | grep -v confile.cfg > "backuplist.txt"
awk -F/ '{ print $6 }' "$BACKUP_TARGETS" > "tempbackuplist.txt"
sed -e 's/^/\//' "tempbackuplist.txt" > "$BACKUP_TARGETS"
echo -e "\nBacking up these files now:" >> "$LOGFILE"
cp "$BACKUP_TARGETS" "$IMPORTANT_FILES_DIRECTORY"
cd "$IMPORTANT_FILES_DIRECTORY"
echo "The present working directory is `pwd`"
sleep 2s
echo "$BACKUPTARGETS are to be backed up now"
sleep 2s
cd /home/ubuntu/assignment1/important_files
tar -czvf "$BACKUP_FILE" -T "$BACKUP_TARGETS" # > "$LOGOUT" 2> "$LOGERR"
echo "$TIMESTAMP BACKUP file $BACKUP_FILE created" # >> "$LOGFILE
 
Old 03-12-2013, 05:49 AM   #7
Batistuta_g_2000
Member
 
Registered: Oct 2011
Posts: 85
Blog Entries: 1

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by shivaa View Post
If you read my previous post, I had mentioned to run it in parent directory of file1, file2... like this:
Code:
BACKUP_FILE=/root/assignment1/backups/backup-20130313-102446.tar.gz
BACKUP_TARGETS="backuplist.txt"
awk 'NR==21,NR==EOF' "$CONFILE" | grep -v confile.cfg > "backuplist.txt"
awk -F/ '{ print $6 }' "$BACKUP_TARGETS" > "tempbackuplist.txt"
sed -e 's/^/\//' "tempbackuplist.txt" > "$BACKUP_TARGETS"
echo -e "\nBacking up these files now:" >> "$LOGFILE"
cp "$BACKUP_TARGETS" "$IMPORTANT_FILES_DIRECTORY"
cd "$IMPORTANT_FILES_DIRECTORY"
echo "The present working directory is `pwd`"
sleep 2s
echo "$BACKUPTARGETS are to be backed up now"
sleep 2s
cd /home/ubuntu/assignment1/important_files
tar -czvf "$BACKUP_FILE" -T "$BACKUP_TARGETS" # > "$LOGOUT" 2> "$LOGERR"
echo "$TIMESTAMP BACKUP file $BACKUP_FILE created" # >> "$LOGFILE
That is what cd "$IMPORTANT_FILES_DIRECTORY" is above. But did not see files?
 
Old 03-12-2013, 06:01 AM   #8
Batistuta_g_2000
Member
 
Registered: Oct 2011
Posts: 85
Blog Entries: 1

Original Poster
Rep: Reputation: Disabled
Actually got it - sed -e 's/^/\//' "tempbackuplist.txt" > "$BACKUP_TARGETS"

was causing the problem and was un-necessary - thanks for the help.

Last edited by Batistuta_g_2000; 03-12-2013 at 06:03 AM.
 
Old 03-13-2013, 04:50 PM   #9
yowi
Member
 
Registered: Dec 2002
Location: Au
Distribution: Debian
Posts: 209

Rep: Reputation: 54
Code:
backup_file="/tmp/backup_$(date +%Y_%m_%d)"
backup_targets="backuplist.txt"
while read target;
do
  tar -Af ${backup_file}.tar -C $(dirname ${target}) $(basename ${target})
done < ${backup_targets}
gzip ${backup_file}.tar
was what I came up with.

Removing the directories gets messy if you have two file names the same which seems likely so I'd raise that as an issue.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
find files but move the parent directory instead? Springs Linux - General 4 12-21-2012 11:27 AM
List Files in parent directory for user input lucie2012 Programming 5 10-06-2012 04:34 AM
Accessing files of a subdirectory whose parent directory is in a different partition. ajitht1986 Linux - General 1 06-10-2011 07:54 AM
Shell script to automatically delete files with the same name as the parent directory pratap.iisc Programming 9 10-12-2009 10:17 AM
save parent directory in rsync packets Linux - Newbie 2 01-24-2008 08:22 PM


All times are GMT -5. The time now is 08:22 PM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration