If you want to move all the files inside a directory hierarchy to a single destination directory, in bash ≥4 (put shopt -s globstar
in your ~/.bashrc
) or zsh:
mkdir ~/new-directory
mv -i **/*.xxx ~/new-directory
In other shells:
mkdir ~/new-directory
find . -name '*.xxx' -exec mv -i {} ~/new-directory \;
How to read this find
command:
.
: traverse the current directory
-name '*.xxx'
: act on files whose name matches this pattern
-exec … \;
: perform this command on each file, replacing {}
by the path to the file
There's a more complex command using find
that's faster if you have many files as it doesn't need to invoke a separate mv
process for each file:
find . -name '*.xxx' -exec sh -c 'mv -i "$@" "$0"' ~/new-directory {} +
The +
at the end tells find
to invoke the command on multiple files at a time. Since find
can only put the file names at the end of the command line, and mv
needs to have the destination directory last, we use an intermediate shell to rearrange the arguments ("$0"
is the first argument to the shell, ~/new-directory
, and "$@"
are the subsequent arguments coming from find
's expansion of {}
).