[SOLVED] Possible to have Tar Command run on multiple files in a directory at one time?
Linux - NewbieThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Possible to have Tar Command run on multiple files in a directory at one time?
I'm monitoring a script that I wrote to tar up files on one server and then move them to an logging server. Want to make sure the script captures all output and we have match on both each day before I know its ok.
Tar can only be ran on one file at a time, correct?
Code:
[root@server jun]$ tar -tf *.tar.gz | wc -l
tar: server.aud-2016-06-06.tar.gz: Not found in archive
tar: server.aud-2016-06-07.tar.gz: Not found in archive
tar: server.aud-2016-06-08.tar.gz: Not found in archive
tar: server.aud-2016-06-09.tar.gz: Not found in archive
tar: server.aud-2016-06-10.tar.gz: Not found in archive
tar: server.aud-2016-06-11.tar.gz: Not found in archive
tar: server.aud-2016-06-12.tar.gz: Not found in archive
tar: server.aud-2016-06-13.tar.gz: Not found in archive
tar: 0
Error exit delayed from previous errors
I've actually coded up a for loop in bash where it will go thru all the files in the current directory and do the count and output to a txt file in /tmp.
Code:
#!/bin/bash
for i in *.tar.gz;
do tar -tf $i | wc -l > /tmp/tar_server_output.txt ;
done
That is not the output from that script. First, when reading from stdin "wc -l" will produce a line with just a single number. Second, that script overwrites the output file on each iteration of the loop, so all you would see is a single line with the last number.
To answer your original question, yes, tar can work with only a single archive file at a time. Any additional arguments will be taken as names to search for in that archive.
Yes you can use it in the for loop, but you need to append to a file and not overwrite it each time.
I would add that another reason that the output shown does not come from the script provided is the last file does not even match the glob used (*.tar.gz), so even if it had worked that file
should never have been listed or tested with wc.
for i in `ls -1 *.tar.gz`;
do tar -tvf $i | wc -l >> /tmp/tar_server_output.txt;
done
Actually, please do not try something like the above. ls should never be used to feed a for loop and the already utilised globbing is already superior.
Also, as explained rknichols, the output from piped data to wc -l will return only a number so the addition of verbose output will not help the required file name
appearing in the output
Actually, please do not try something like the above. ls should never be used to feed a for loop and the already utilised globbing is already superior.
Too late...
Code:
[root@server jun]$ ./tar_count3.sh
tar: -rw-------: Cannot open: No such file or directory
tar: Error is not recoverable: exiting now
tar: 1: Cannot open: No such file or directory
tar: Error is not recoverable: exiting now
tar: oracle: Cannot open: No such file or directory
tar: Error is not recoverable: exiting now
tar: oracle: Cannot open: No such file or directory
tar: Error is not recoverable: exiting now
tar: 50899: Cannot open: No such file or directory
tar: Error is not recoverable: exiting now
tar: Jun: Cannot open: No such file or directory
tar: Error is not recoverable: exiting now
tar: 7: Cannot open: No such file or directory
tar: Error is not recoverable: exiting now
10: Invalid argument
tar: 10\:30: Cannot open: Input/output error
tar: Error is not recoverable: exiting now
tar: -rw-------: Cannot open: No such file or directory
tar: Error is not recoverable: exiting now
tar: 1: Cannot open: No such file or directory
tar: Error is not recoverable: exiting now
tar: oracle: Cannot open: No such file or directory
tar: Error is not recoverable: exiting now
tar: oracle: Cannot open: No such file or directory
tar: Error is not recoverable: exiting now
tar: 49663: Cannot open: No such file or directory
tar: Error is not recoverable: exiting now
tar: Jun: Cannot open: No such file or directory
tar: Error is not recoverable: exiting now
tar: 7: Cannot open: No such file or directory
tar: Error is not recoverable: exiting now
10: Invalid argument
tar: 10\:30: Cannot open: Input/output error
tar: Error is not recoverable: exiting now
tar: -rw-------: Cannot open: No such file or directory
tar: Error is not recoverable: exiting now
tar: 1: Cannot open: No such file or directory
tar: Error is not recoverable: exiting now
tar: oracle: Cannot open: No such file or directory
tar: Error is not recoverable: exiting now
tar: oracle: Cannot open: No such file or directory
tar: Error is not recoverable: exiting now
tar: 58694: Cannot open: No such file or directory
tar: Error is not recoverable: exiting now
tar: Jun: Cannot open: No such file or directory
tar: Error is not recoverable: exiting now
tar: 8: Cannot open: No such file or directory
tar: Error is not recoverable: exiting now
11: Invalid argument
tar: 11\:38: Cannot open: Input/output error
tar: Error is not recoverable: exiting now
tar: -rw-------: Cannot open: No such file or directory
tar: Error is not recoverable: exiting now
tar: 1: Cannot open: No such file or directory
tar: Error is not recoverable: exiting now
tar: oracle: Cannot open: No such file or directory
tar: Error is not recoverable: exiting now
tar: oracle: Cannot open: No such file or directory
tar: Error is not recoverable: exiting now
tar: 48311: Cannot open: No such file or directory
tar: Error is not recoverable: exiting now
tar: Jun: Cannot open: No such file or directory
tar: Error is not recoverable: exiting now
tar: 9: Cannot open: No such file or directory
tar: Error is not recoverable: exiting now
12: Invalid argument
tar: 12\:48: Cannot open: Input/output error
tar: Error is not recoverable: exiting now
tar: -rw-------: Cannot open: No such file or directory
tar: Error is not recoverable: exiting now
tar: 1: Cannot open: No such file or directory
tar: Error is not recoverable: exiting now
tar: oracle: Cannot open: No such file or directory
tar: Error is not recoverable: exiting now
tar: oracle: Cannot open: No such file or directory
tar: Error is not recoverable: exiting now
tar: 47484: Cannot open: No such file or directory
tar: Error is not recoverable: exiting now
tar: Jun: Cannot open: No such file or directory
tar: Error is not recoverable: exiting now
tar: 10: Cannot open: No such file or directory
tar: Error is not recoverable: exiting now
Each invocation of the echo command writes one line, and that line consists of the number produced by the tar and wc pipeline followed by the filename that was used. Run the script with "bash -x ..." to see what it is doing.
I will add that the reason you got so many errors from the ls version was you used -l (el) as opposed to -1 (one) ... but it still not the recommended way because of word splitting.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.