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.