Solved [Shell] CLI search and copy assistance request

Good afternoon,

I have just set up my first FreeBSD server for a client. My client has two Windows workstations that are going to be replaced.

At issue is that I need to copy the documents, spreadsheets, PDF's, etc. from the old workstation to the new ones. Normally not an issue, except the old users have put their documents and stuff all over the HDD. I mean I found documents in c:\Program Files(x86), I see documents in the Windows system directories, etc. The old workstations are a mess.

I have performed an offline backup to the RAID on the FreeBSD server via Samba shares. So I have a complete copy of the mess on the server.

What I need now is a CLI to search through the entire backup. find /RAID-1/<usersharedfolder> -name "*.doc"

Then I need to pipe find's output into cp such as cp <find's results> <target directory>.

I am not senior enough with the CLI to know how to do this, and I have to be back at the job site tomorrow to continue to the port over of my client from Windows to FreeBSD. So I really don't have a lot of time for personal experimentation.

I do keep a record of "HowTo's" on my system that the answer to this post will be kept in, so that once I receive an answer for a "HowTo", I document it so I don't repeat the post in the future.

Will someone please assist me in constructing a CLI to do what I am after? Or point me to a utility in the ports tree that will?

Thank you for your assistance.

Sincerely and respectfully,

Dave
 
This is easy to do with find -exec, but there are security implications of -exec, so it should often be avoided. Trying to do the same thing with find(1) and xargs(1):
find /source/directory -name "*.doc" -print0 | xargs -0 -Ifname echo cp \"fname\" /destination/directory

Note the forced quoting of the filename, because spaces in Windows filenames are nearly guaranteed.

There might be special characters or paths that need additional special consideration.
 
Basically, I support the solution of wblock@, and here comes only one additional consideration. If there is a possibility that there are file name doublets in the source hierarchy, then obviously a next file with the same name will overwrite the previous one, and in any case you will stay only with one of these doublets/multiplets.

I don't know of an automatic way to circumvent this, but perhaps you won't experience too many occasions, and therefore you could add a final manual pass to the copy procedure. For this reason, you might want to add the options -i and -v to the cp command. This will result in a list of all copied files and in addition will indicate those files that were not copied because another one with the same name already existed. The following is the whole command as given by wblock@ only with the additional options for cp:

find /source/directory -name "*.doc" -print0 | xargs -0 -Ifname echo cp -iv \"fname\" /destination/directory
 
Sometimes it is useful in these pipes to use gcp -Rvu which I know from limited experience does incremental copy (from coreutils-8.23).
 
Last edited by a moderator:
Oh, and the echo in that command line is a debugging tool, removed when the output looks correct.
 
I thank you all for the replies. I appreciate it. I will be deploying this at the jobsite tomorrow!

Sincerely and respectfully,
Dave
 
Back
Top