System Administration
cancel
Showing results for 
Search instead for 
Did you mean: 

command to find and tar files

 
PKS_1
Regular Advisor

command to find and tar files

Hi

I am looking for a one liner which finds all files starting with string "back" and then tars it.
13 REPLIES
Pete Randall
Outstanding Contributor

Re: command to find and tar files

Something like "find /startdir -name "start*" |tar"?


Pete

Pete
James R. Ferguson
Acclaimed Contributor

Re: command to find and tar files

Hi:

You could do:

# cd /path && tar -cvf /tmp/myarchive $(find . -type f -name "*back*)

Regards!

...JRF...

Re: command to find and tar files

JRFs option will work, but will start to run into problems when the number of files exceeeds the limits of the command line (in terms of number of arguments and argument length). tar isn't that flexible in this situation, but pax can be persuaded to read the files to archive from stdin as follows:

Backup everything in /usr that starts with "pass":

find /usr -name "pass*" | pax -wf /tmp/mypassarchive.pax

cpio would do the trick as well...

HTH

Duncan

HTH

Duncan
Steven Schweda
Honored Contributor

Re: command to find and tar files

> [...] pax can be persuaded to read the
> files to archive from stdin [...]

GNU "tar" has an option for that, too.
Suraj K Sankari
Honored Contributor

Re: command to find and tar files

Hi,

As everybody said here one more example
from where you want to search from root "/" or some specific directiory from root "/" give the following command.

#find / -xdev -name back* | tar -xvf back.tar

for other location suppose /home then do like this

#find /home -xdev -name back* | tar -xvf back.tar

with cpio find from current directiory

#find . -print -depth |cpio -ov >tree.cpio

This will retrieve the files archived in the file directory.

#cpio -idv < tree.cpio

Suraj
Laurent Menase
Honored Contributor

Re: command to find and tar files

made on the thumb without testing it
find . -type f | while read a
do
b="$( dd bs=4 count=1 if=$a 2>/dev/null |vis)"
if [ "$b" = "back" ]
then
echo $a
fi
done| xargs tar cvf /tmp/mytar.tar
Laurent Menase
Honored Contributor

Re: command to find and tar files

or an other way more funny/elegant:

myM=/tmp/m$$
echo "0\tstring\tback\tmybackfile >$myM

find . -type f | xargs file -m $myM| grep "\tmybackfile" |cut -d : -f | xargs tar cvf /tmp/mytarfile.tar


I used "file" to check the beginning of each file
defining a magic entry
Laurent Menase
Honored Contributor

Re: command to find and tar files

Just a typo: in cut option,
find . -type f | xargs file -m $myM| grep "\tmybackfile" |cut -d : -f 1 | xargs tar cvf /tmp/mytarfile.tar

Re: command to find and tar files

>Duncan: find /usr -name "pass*" | pax -wf /tmp/mypassarchive.pax

Or you can use .tar: ... | pax -wf /tmp/mypassarchive.tar

>Suraj: find / -xdev -name back* | tar -xvf back.tar

As mentioned, this won't work without gnu tar or pax.
Sagar Sirdesai
Trusted Contributor

Re: command to find and tar files

Hi
find /dir -depth -name "back*" -type f -print | xargs tar -cvf back.tar


Thanks

Sagar

Re: command to find and tar files

>Sagar: find /dir -depth -name "back*" -type f -print | xargs tar -cvf back.tar

As Duncan, Steven and I said, you can't use tar with find, unless you gnu tar or pax.

Your example will continually overwrite back.tar and not in a good way.
Laurent Menase
Honored Contributor

Re: command to find and tar files

are you looking for file starting with string back or file which filename start with back?

my solution was to backup files starting with back, not for which filename start with back.

else it is simply
find . -name back\* | xargs tar cvf mytar

Re: command to find and tar files

>Laurent: find . -name back\* | xargs tar cvf mytar

How many times do we need to mention you can't use find and HP's tar this way? :-)