Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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)?

share|improve this question

1 Answer 1

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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