Single line backups with find or cat and xargs, etc

Hi, I'm new here and this is my first post. I used command line Unix at work for 3 years... about 10 years ago! Now I can't figure out nor hunt down examples of how to do the following:
Say I built a list of file to backup like this:

find ~ -name "*.pdf" -print >> MYPDF.txt

So I am using find with a redirection operator to a plain text file. Now I want to say... cat that file and pipe it to xargs using the cp command to copy them all to a single directory so I can tar them into a tar ball... but I am screwing it up!!

mkdir NEW_PDF_DIR
cat MYPDF.txt | xargs cp NEW_PDF_DIR

then I want to do something like:

tar  -cfv PDF_ARC ./NEW_PDF_DIR

Several things going on here... trying to find to build a list to use to create a tar archive... more or less. And hey, if there is an easier/smarter way to do it, Thank you all! (I figure it can be done in 1 line at the shell prompt... but hey I am re-acquiring skills here!)

You might consider using something like this:

find find ~ -name "*.pdf" -exec  /your/script/or/command/here.sh {} \;

Example (not your entire solution)

find find ~ -name "*.pdf" -exec  cp {}  /new/directory \;

Anyway, I normally use -exec for these tasks with find and rarely (if ever) use xargs these days.

Hope this helps.

You you don't say what OS you are using some GNU options can make this task easier. If cp implementations the cp -t DIRECTORY SOURCE ... method, then this makes xargs much easier to work with:

mkdir NEW_PDF_DIR
cat MYPDF.txt | xargs --no-run-if-empty cp -t NEW_PDF_DIR

Also GNU xargs allows a --no-run-if-empty which to avoids invoking cp if no files exist to be copied.

If NEW_PDF_DIR is under your home directory, you will want to prune it to avoid trying to backup the backup files!

find ~ -path ~/NEW_PDF_DIR -prune -type f -o -name "*.pdf" -print >> MYPDF.txt

The cp -t does the trick here, because you have the target directory first.
Both xargs and find -exec want constant arguments first.

find ~ -name "*.pdf" -exec  cp -t /new/directory {} +

Normally the cp has the target directory last. But only a few find versions allow

find ~ -name "*.pdf" -exec  cp {} /new/directory \;

or

find ~ -name "*.pdf" -exec  cp {} /new/directory +

Does your tar version offer the -T option? man tar :

If yes, try

find . -iname  "*.pdf" | tar cvf PDF_ARC -T-

tar also handles

  • should you have file names with LF chars.

A long time ago I used find ... cpio to create tar archives. As it's a long time ago I can't find any examples, but using the GNU CPIO man page and evidence of how I used to copy directories with cpio (now I use rsync ) it would have been something like this:

find -depth -name 'pattern' -print | cpio --create --format=tar > archive.tar

A SysV version would probably be more like:

find -depth -name 'pattern' -print | cpio -o -H tar > archive.tar

Of course, this was in the days when you couldn't pipe find into tar to list the files you wanted to archive...

Andrew

I or we used SysV Unix flavor and I am not sure if 'find' had 'exec' or not but I never used it. I use Linux (Ubuntu) now but thankfully Bash tolerates me dropping back to SysV type stuff. I want to get my SysV chops back then migrate forward into the brave new world, LOL.
Also I will use the code wrapper from now own when notating code like...

rm -rf /*

Wait, holy ... kidding. Oh, I also related to the cpio example... did that too.
Thank you all again!!