If you want to copy all files from a folder and subfolders to a single folder in Linux using terminal you can combine the commands. This can be done by giving the output of the find to copy command which is going to copy each result to a given folder.
Feel free to ask question in the command section if you need help with this command for your needs!
Lets say that we have folders:
- Team
- team.jpg
- Joe
- cat.jpg
- dog.jpg
- readme.txt
- Jim
- fish.jpg
- monkey.jpg
- rabit
And you want to copy all the images from folders: Team, Joe, Jim to another folder. The next command will do it:
- copy all jpg files to folder /home/newteam/
find . -type f -name "*.jpg" -exec cp {} /home/newteam/ ';'
- copy text files to folder on the same level as Team
find . -type f -name "*.jpg" -exec cp {} ../newteam/ ';'
- Find text files in folder /home/pictures/ and copy them to folder on the same level as Team
find /home/pictures/ -type f -name "*.jpg" -exec cp {} ../newteam/ ';'
Explanation:
- find: invoking the find command
- .: start search from current working directory.
- /home/pictures/ - search in a given folder
- Since no depth flags are specified, this will search recursively for all subfolders
-type f -name "*.jpg"
: find files with the given extension-exec
: for the every result of find command, do a copy to another folder- {}: the result of find command will be replaced here.
- ';': it is used to separate different commands to be run after find
Note: if the folder which is used for the copies doesn't exists then error message will shown as:
cp: cannot create regular file '../../../team/': Not a directory
cp: cannot create regular file '../../../team/': Not a directory