In a directory I have many sub-directories and each of these sub-directories have many files of different types. I want to extract all the files with a particular extension from each subdirectory and put it in a different folder. Is it possible to write a bash script to do this? If so how?
$ find <directory> -name '*.foo' -exec mv '{}' <other_directory> \;
find
does a recursive search through a directory structure and performs the given actions on any files it finds that match the search criteria.
In this case, -name '*.foo'
is the search criteria, and -exec mv '{}' <other_directory> \;
tells find
to execute mv
on any files it finds, where '{}'
is converted to the filename and \;
represents the end of the command.
If you have bash v4 and have
shopt -s globstar
in your .profile, you can use:
mv ./sourcedir/**/*.ext ./targetdir
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With