Click to See Complete Forum and Search --> : FIND piped into CP command help
plasma2002
01-01-2007, 07:28 PM
hey guys, i need some help with a command.... im not up on my shell usage like i should be :)
heres what i need to do.... i need to search the drive for all pdf's, then i need to COPY all those found pdf's to a folder using the same heirarchy structure.
I can do it manually just fine....
find / -name *.pdf > pdf_list.txt
that gives me a list, then i can examine the list.... but what i want to do is pipe the output into the cp command so that i can copy all the found files automatically. Im just asuming that there would have to be some use of sed... but i have absolutely NO idea how to use that, haha.
So yea.... can anybody help me out here? :)
tucolino
01-01-2007, 07:36 PM
find /path/to/look -iname "*.pdf" -exec cp '{}' /some/destination/path \;
something like this?
plasma2002
01-01-2007, 08:01 PM
hmm.... seems that im getting nothing but 'no such file or directory' error messages on every file it find and tries to copy.....
i tweaked the line to this:
find C:\ -iname "*.pdf" -exec cp "{}" 'C:\test\' \ ;
(yes im on a win32 structure using the GNU win32 ports of find and copy... they use the same structure and formats as far as ive been able to tell so far)
it seems that its further than ive gotten though..... i havent really been aware of the -exec attribute..... any other ideas?
Sepero
01-01-2007, 08:44 PM
I think rsync might work much better for this task, but I have little experience with rsync.
ernieg
01-01-2007, 08:59 PM
I used to do this all the time, but with cpio. Here's what I do:
find . -name *.pdf |cpio -pdmv /newdir
Take a look at the man pages for cpio for more info.
Happy New Year! :)
Sepero
01-01-2007, 11:27 PM
Good one, ernieg. Something like that could actually be done with tar also.
plasma2002
01-02-2007, 03:27 PM
hmm.... havent used cpio before, and until i get back home to my nix box, i cant mess with it since theres no win32 port that doesnt requiring installing dependicies, which just wont work for the application i need.
But if i could figure out a way to use tar, that could work..... how could i pipe the found results into tar? ive been tinkering with it with no luck
tucolino
01-02-2007, 05:15 PM
hmm.... seems that im getting nothing but 'no such file or directory' error messages on every file it find and tries to copy.....
i tweaked the line to this:
find C:\ -iname "*.pdf" -exec cp "{}" 'C:\test\' \ ;
(yes im on a win32 structure using the GNU win32 ports of find and copy... they use the same structure and formats as far as ive been able to tell so far)
it seems that its further than ive gotten though..... i havent really been aware of the -exec attribute..... any other ideas?
you need to use single quotes for '{}'
also... are you sure you are using gnu find? windows has a find command you can invoke from dos. it does not take the same parameters, hence why you might be getting the error message.
are you on cygwin? you can substitute c:\ with /cygdrive/c
hope that helps.
tuco
bwkaz
01-02-2007, 08:21 PM
Actually you don't need quotes around {} in find, because find already replaces {} with the filename as a single word (a single entry in the argv[] array that it passes to exec() when it starts its child processes). Even if the filename has spaces or newlines in it, it still works, without extra quotes. In fact, if you put quotes there, cp will barf, because it will think the quotes are part of the filename. You don't get a shell when you use find's -exec command.
(However, I'm not so sure how well it works on Windows, because Windows basically says "screw you, C standard", and passes all the arguments on to child processes as a single long string. So each process has to either duplicate command-line parsing code, or call CommandLineToArgvW and get it wrong. (See the MSDN docs for that function, and note its retarded unquoting rules, to see what I mean by "wrong".) So I'm not sure that find can pass the filename to its child cp process as one argument on Windows; I'd hope Cygwin worked around this somehow, but I'm not sure. In short: The quotes may still be required on Windows, but they certainly aren't on Linux.)
plasma2002
01-11-2007, 08:03 AM
just a followup in case anybody else is having the same issue i was (i hate googleing and finding the same problem but no answer, haha....).....
i was in fact using gnu find, and not MS find, but the thing that was screwing me up was just the reverse slashes for the win32 file structure. So what i did was save the output to a text file, then run some find/replaces on the text file, then import the text file into a tar command and tar'd all the files :)
Heres my completed batch file if anybody is interested (you can download the win32 ports from gnu)
@ECHO OFF
echo -----------
echo Searching...
find C:\ -iname "*.dat" > found.txt
echo Found PDFs.
echo -----------
echo Formatting Entry List...
gsar -s\ -r/ -o found.txt
gsar -sC: -r -i -o found.txt
echo Formatted.
echo -----------
echo Compiling...
tar -c -T found.txt -f found.tar
echo Compiled.
echo -----------
echo Creating Directory...
IF EXIST output DEL output
md output
echo Created.
echo -----------
echo Extracting....
tar -x -C output -f found.tar
echo Extracted.
echo -----------
echo Deleting Temporary files...
del found.txt
del found.tar
echo Deleted.
echo -----------
pause
just save the file as whatever.bat, stick it on a flash drive along with the required tar, find, and gsar exe's and you have an instant PDF swiper... err.... copier. haha (it is actually being used to collaborate mounds of scanned images from our office all together)
My version also untars as you probably noticed... if you like nice and neat tar'd files, just delete those last couple lines from the batch :)
btw, thanx for ur guys' help