I'm not very familiar with all the tricks of grep/find/awk/xargs quite yet, so be patient with me =]

I have a particular file type say *.xxx, in random places throughout a certain directory. How can I find all such files, and move them to a folder in my home directory on Unix (that may not exist yet)?


Use find with the exec option, but first create the target folder.

mkdir -p /home/somewhere/else
find /somewhere -iname "*.xxx"

This will list everything that would be moved. Now if you're sure these are the files you want to move, execute the following:

find /somewhere -iname "*.xxx" -exec mv '{}' /home/somewhere/else/ \;

In the exec line, '{}' will be substituted with the actual file name, and it will be moved to the target. Likewise, to copy, just exchange mv with cp. The \; is needed to terminate the command.

If you want a confirmation before each file operation that would overwrite an already existing file, you can add the -i option after mv or cp, respectively.

No need for grep, xargs and their likes. This would unnecessarily complicate things.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.