1756540 Members
2516 Online
108848 Solutions
New Discussion юеВ

find files and tar them

 
SOLVED
Go to solution
Shivkumar
Super Advisor

find files and tar them

Hi,

How do i use find and tar command together ?
I just want to find some files and tar the output in a tar file.

Thanks,
Shiv
6 REPLIES 6
James R. Ferguson
Acclaimed Contributor
Solution

Re: find files and tar them

Hi Shiv:

If we assume HP-UX (since it is in that community that you ask), then I'd do something like this:

# FILES=$(find /path -xdev -type f -mtime +7)
# tar -cvf /tmp/myarchive ${FILES}

...or whatever your specification is for collecting the files ...

Regards!

...JRF...

Steven Schweda
Honored Contributor

Re: find files and tar them

> # FILES=$(find /path -xdev -type f -mtime +7)
> # tar -cvf /tmp/myarchive ${FILES}

And what is the command-line length limit
these days?

And then there are the potential problems in
the funny-file-name class:

dyi # ls -l gtt
total 0
-rw-r--r-- 1 root sys 0 Aug 11 12:59 a a

dyi # FILES=$( find gtt -type f )

dyi # echo ${FILES}
gtt/a a

dyi # tar cvf gtt.tar ${FILES}
tar: cannot stat gtt/a. Not dumped.
tar: cannot stat a. Not dumped.

GNU "tar" has a "--files-from=file-name"
("-T file-name") option which can be helpful
in cases where "${FILES}" would cause
problems. "--files-from=-" or "-T -" works
to get the file names from stdin, so a "find"
pipeline can feed in the file names. For
example:

dyi # find gtt -type f | gtar cfv gtt.tar -T -
gtt/a a

dyi # tar tfv gtt.tar
rw-r--r-- 0/3 0 Aug 11 12:59 2009 gtt/a a


dyi # uname -a
HP-UX dyi B.11.31 U ia64 4235313755 unlimited-user license

dyi # gtar --version
tar (GNU tar) 1.18
[...]

For a variety of reasons, it's often a bad
idea to use file-name wild cards (like "*"),
or equivalent constructs, in a command line.
One of the best reasons to use "find" is to
avoid problems like this, but sloppy use of
"find" can effectively demolish many of its
advantages.
Andrey Chipalyuk
Frequent Advisor

Re: find files and tar them

You can try -exec option of find command also.
For example:

find ./ -name "..." -exec tar cvf {} \;
Dennis Handly
Acclaimed Contributor

Re: find files and tar them

>How do I use find and tar command together?

If you have a trivial number of files, you can use JRF's example. Or you use Steven's gtar example.

Otherwise you don't use tar. Instead you use pax(1):
find /path -xdev -type f -mtime +7 | pax -v -w -f /tmp/myarchive

You can use pax or tar to restore.

>Andrey: You can try -exec option of find command also.

There are lots of reasons this won't work, don't even try.
Steven Schweda
Honored Contributor

Re: find files and tar them

> There are lots of reasons this won't work,
> don't even try.

Or, if you're the fellow who made the
suggestion, then try it _before_ posting it.
James R. Ferguson
Acclaimed Contributor

Re: find files and tar them

Hi (again):

> Steven: And what is the command-line length limit these days?

Far larger than the days of HP-UX 10.20 and prior:

# getconf ARG_MAX
2048000

That said, on an "un-enhanced" HP-UX system (i.e. without some of the really nice GNU tools), I agree with Dennis --- 'pax' is probably the better choice. Oh, and your point about nasty files with embedded spaces is well taken, of course.

Regards!

...JRF...