Wednesday, January 23, 2013

Copy specific files from all subfolders of a folder to another folder in Unix

If you want to copy all the .txt files for example from the subfolders of the current folder to another destination folder do the following:

find . -name *.txt -type f -exec cp -v {} ./all_txt_files/ \;

This will copy all the txt files in a current folder and in all subfolders of a current folder to the folder all_txt_files located in the current folder.

However, note that this will only look in the subfolders and not sub-subfolders and so on.

To look in the sub-subfolders as well simplyput the name of the file you are searching in parenthesis:

find . -name "*.txt" -type f -exec cp -v {} ./all_txt_files/ \; 

And also if you have the files with the same name they will overwrite each other,  so only the last file copied remains.

No comments:

Post a Comment