Visit Our UNIX and Linux User Community

zipping functionality (tar) not working as expected

Thread Tools Search this Thread
Top Forums Shell Programming and Scripting zipping functionality (tar) not working as expected
# 1  
Old 02-12-2012
Question zipping functionality (tar) not working as expected

Hi all,

I here have an index file ($index) which lists the full paths of some files, and am tying to use "tar" to zip all of them.

I ran a command like below,

cat $index | xargs tar -rcf $archived_file

Strangely I noticed only part of files in that index were zipped in my $archived_file.

Where are the other files?

# 2  
Old 02-12-2012
may you please post the content of the file referenced by $index?
# 3  
Old 02-12-2012
The -c option on your tar command is causing a new file to be created with each invocation. When there are more files in your input file than can be placed on the command line xargs will invoke tar multiple times, and with each invocation tar creates a new file. What you are seeing in the final tar file is the set of files that were placed on the command line to tar with the last invocation.

To solve this problem, just remove the -c option:

xargs <$index  tar -rf $archived_file

You also don't need to use cat; xargs can read from stdin so the index file can be redirected in making the process more efficient.

EDIT: One more thought...
Because the -r option always appends if the target file exists, you should always remove the file before executing your tar command:

rm -f $archived_file     # ensure it doesn't exist
xargs <$index  tar -rf $archived_file

Last edited by agama; 02-12-2012 at 12:21 PM.. Reason: additional thought
These 2 Users Gave Thanks to agama For This Post:
# 4  
Old 02-12-2012
Hi Agama

Thanks - your advice made great sense. Just one more question - if I simply run that command, it will tell me something like,

"tar: $archived_file: No such file or directory"

So do I need to used "touch $archived_file" to create an empty archived_file first?

# 5  
Old 02-12-2012
Is it saying exactly "$archive_file" or the actual file name? If it's the actual file name, then your version of tar might require you to create an empty/dummy tar file to extend. You can try touching the file, that's certainly the easiest, but if your version of tar won't allow an empty file to be extended, then you'll need to create a real tar file to extend. Probably the easiest way to do this is:

echo "tar created on $(date)" >marker
tar -cf $archive_file marker

Hope this helps.

Previous Thread | Next Thread
Test Your Knowledge in Computers #256
Difficulty: Easy
MILNET gave birth to NIPRNet, which was used to transmit sensitive but unclassified data between internal users and also provide those users with a gateway to the commercial Internet.
True or False?

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

Test -e not working as expected (by me)

I ran into the following and still do not understand entirely the rationale behind this. If someone could explain why things are as they are I'd be thankful. The following was tested on AIX 7.1 with ksh88, but i suspect that to be ubiquitous. In an installation routine i had to create a set of... (6 Replies)
Discussion started by: bakunin
6 Replies

2. Shell Programming and Scripting

Script not working as expected

Hi, I have prepared a script and trying to execute it but not getting expected output. Could you please help and advise what is going wrong. "If else" part in below script is not working basically. I am running it on HP-UX. for i in slpd puma sfmdb do echo "******\t$i\t*******" echo... (10 Replies)
Discussion started by: sv0081493
10 Replies

3. UNIX for Dummies Questions & Answers

Nohup not working as expected

Hi. I am trying to start a script on my router that will execute even if i log off. To execute the script I write: nohup ./dslconnection > dslstat.out 2>&1 & It starts the job: 21968 admin 1604 S /bin/ash ./dslconnection The problem is that when I log back in the job has been... (6 Replies)
Discussion started by: sebcou
6 Replies

4. UNIX for Dummies Questions & Answers

-atime not working as expected

I need to sort through a volume that contains video files by access time and delete files that have not been accessed over x days. I have to use the access time as video files are originals that do not get modified, just read Testing commands on a local test folder... $ date Wed Sep 28... (10 Replies)
Discussion started by: canon273
10 Replies

5. Shell Programming and Scripting

Why this is not working in expected way?

total=0 seq 1 5 | while read i ; do total=$(($total+$i)) echo $total done echo $totalThis outputs: 1 3 6 10 15 0whereas I am expecting: 1 3 6 10 15 15My bash version: (4 Replies)
Discussion started by: meharo
4 Replies

6. UNIX for Dummies Questions & Answers

zipping all the tar files to singlr file in directory

Hi, i have more than 300 tar files in directory and i want to zip all tar files to single file. could anybody tell me the command since i know how to do zip for single tar file: bash-3.00$gzip 2008_11_10.tar bash-3.00$ pwd /oracle1/archivebackup in this directory i have lot files... (2 Replies)
Discussion started by:
2 Replies

7. Shell Programming and Scripting

tar: 0511-194 Reached end-of-file before expected.

Hello everyone! I wrote a script for backing up a folder. It goes fine but today it started to spit out this error, when a folder is taring: tar: 0511-194 Reached end-of-file before expected. I didn't make any changes! - OS: UNIX AIX ibm 3 - the folder I'm trying to tar is 11Gb large... (4 Replies)
Discussion started by: Funky_ass
4 Replies

8. UNIX for Dummies Questions & Answers

tar'ing and zipping files

If I have a directory /directory1 and want to tar and zip everything in it into a file new_tar.tar.gz on disk (not tape) How can I do it? I tried tar -cv /new_tar.tar /directory1/* But I got an error: tar: /dev/rmt/0: No such device or address (4 Replies)
Discussion started by: FredSmith
4 Replies

9. Shell Programming and Scripting

ls not working as expected within ksh

Hi, I use the command ls a\b\c\*.txt from the command line on HP UNIX and it works fine - It lists all files matching *.txt in the a\b\c directory When embeded in a ksh script `ls a\b\c\*.txt` it does not work - I get *.txt not found (even though there are files) I tried... (10 Replies)
Discussion started by: GNMIKE
10 Replies

10. Shell Programming and Scripting

which not working as expected

Hello. Consider the following magic words: # ls `which adduser` ls: /usr/sbin/adduser: No such file or directory # Hmmm... Then: # ls /usr/sbin/adduser /usr/sbin/adduser # Now what? Unforunately this little sniippet is used in my debian woody server's mysql pre install script.... (2 Replies)
Discussion started by: osee
2 Replies

Featured Tech Videos