LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Ubuntu (https://www.linuxquestions.org/questions/ubuntu-63/)
-   -   having issue with tar and find programs under Ubuntu 11.10 (https://www.linuxquestions.org/questions/ubuntu-63/having-issue-with-tar-and-find-programs-under-ubuntu-11-10-a-944145/)

kevinandrews 05-09-2012 08:24 PM

having issue with tar and find programs under Ubuntu 11.10
 
Hello,

Basically what I'm trying to do is tar up a bunch of files on my system. I want to use tar to create the archive and use find to get all of the files to be archived. I want all of the logfileson my system in this archive. Here is what happened, and it makes no sense. Wht's going on? What am I doing wrong? Thanks for your help!

root@techwizard:~#
tar -cf logs2012-05-09.tar > find / -name '*.log'
tar: invalid option -- 'e'
Try `tar --help' or `tar --usage' for more information.
root@techwizard:~#

fukawi1 05-10-2012 05:01 AM

you can use the -exec option of find.
Code:

fukawi1 /var/log # find . -iname '*.log' -exec tar -czvf /tmp/tmp.tar.gz '{}' \;
./gdm/:0.log
./gdm/:0-greeter.log
./gdm/:0-slave.log
./Xorg.9.log
./dracut.log
./yum.log
./prelink/prelink.log
./setroubleshoot/setroubleshootd.log
./pm-powersave.log
./wpa_supplicant.log
./audit/audit.log
./boot.log
./iscsiuio.log
./Xorg.0.log
./anaconda/anaconda.program.log
./anaconda/anaconda.log
./anaconda/anaconda.storage.log
./anaconda/anaconda.ifcfg.log
./jockey.log
fukawi1 /var/log #

This will execute the tar command using the found results in place of the {}'s the 's are to correctly handle files with spaces/etc, and the \; signifies the end of the command to be executed.

rknichols 05-10-2012 07:27 AM

Quote:

Originally Posted by fukawi1 (Post 4674940)
you can use the -exec option of find.
Code:

fukawi1 /var/log # find . -iname '*.log' -exec tar -czvf /tmp/tmp.tar.gz '{}' \;
./gdm/:0.log
./gdm/:0-greeter.log
...
./anaconda/anaconda.ifcfg.log
./jockey.log
fukawi1 /var/log #

That will execute the tar command using the found results in place of the {}'s the 's are to correctly handle files with spaces/etc, and the \; signifies the end of the command to be executed.

That command will execute tar separately and create the archive anew for each file found. You will end up with /tmp/tmp.tar.gz containing only the last file found, in this case "./jockey.log".

michaelk 05-10-2012 07:45 AM

rknichols is correct.

You need to use xargs and these links might help.
http://www.linuxquestions.org/questi...pected-856455/
http://linuxgazette.net/111/tag/4.html

rknichols 05-10-2012 11:22 PM

Using xargs can silently fail too if the argument list becomes longer than what can be passed in a single exec() call. To be ironclad, you need to pass all the names null-terminated to a single invocation of tar:
Code:

find / -name '*.log' -print0 | tar -cf logs2012-05-09.tar --null --files-from -
AFAIK, that should be able to handle anything except permissions problems or running out of space for the archive, though it might be worthwhile to prune various pseudo-filesystems like /proc and /sys from the find search.

fukawi1 05-11-2012 03:42 AM

Good catch, in my defense, that was a cut and paste error there of a command I tried erroneously working on reaching the below example, which is what should have been pasted. Apologies for the confusion..
Code:

fukawi1 /var/log # find . -iname '*.log' -exec tar -rvf /tmp/test.tar '{}' \;
./gdm/:0.log
./gdm/:0-greeter.log
./gdm/:0-slave.log
./Xorg.9.log
./dracut.log
./yum.log
./prelink/prelink.log
./setroubleshoot/setroubleshootd.log
./pm-powersave.log
./wpa_supplicant.log
./audit/audit.log
./boot.log
./iscsiuio.log
./Xorg.0.log
./anaconda/anaconda.program.log
./anaconda/anaconda.log
./anaconda/anaconda.storage.log
./anaconda/anaconda.ifcfg.log
./jockey.log
fukawi1 /var/log # tar -tf /tmp/test.tar
./jockey.log
./gdm/:0.log
./gdm/:0-greeter.log
./gdm/:0-slave.log
./Xorg.9.log
./dracut.log
./yum.log
./prelink/prelink.log
./setroubleshoot/setroubleshootd.log
./pm-powersave.log
./wpa_supplicant.log
./audit/audit.log
./boot.log
./iscsiuio.log
./Xorg.0.log
./anaconda/anaconda.program.log
./anaconda/anaconda.log
./anaconda/anaconda.storage.log
./anaconda/anaconda.ifcfg.log
./jockey.log
fukawi1 /var/log #

That being said, I can see rknichols solution covers more bases as far as things going wrong are concerned.

Learn something new every day, as they say.


All times are GMT -5. The time now is 10:57 PM.