Operating System - HP-UX
1753701 Members
4939 Online
108799 Solutions
New Discussion юеВ

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 13
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

I am an HPE Employee
Accept or Kudo
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
Dennis Handly
Acclaimed Contributor

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.