Looking for Shell Script to find '.txt', '.csv' files from many recursive folders/subfolders, copy the files and recreate the same folders path to copy those files in new path.

Example: we got around 20k files and saved in many parent folders and subfolders.

Source: u01/temp/proj/sales/john/.csv, .xls files
u01/temp/proj/sales/mike/.txt, .csv, .xls files
u01/temp/proj/sales/geebee/sale/.csv, .xls files

need shell script to copy only '.txt', '.csv' files(exclude xls , other files) and recreate same folder path then copy above files as below

u01/perm/proj/sales/john/.csv files
u01/perm/proj/sales/mike/.txt, .csv files
u01/perm/proj/sales/geebee/sale/.csv files

thank you.

  • 2
    Use rsync, you will be a lot happier than trying to do it by yourself in a script.
    – user232326
    Apr 9, 2021 at 4:36

1 Answer 1


You can do this with rsync. If you wanted to just take .txt and .csv files from a source directory, you can copy only those files along with their associated directory structure to the target.

For example:

rsync -av --include="*/" --include="*.csv" --include="*.txt"  --exclude="*" u01/temp/ u01/perm/

If you wanted to do the inverse and do something like copy all files except `.json` and `.xlsx`, you would use `--exclude` in your command invocation like so:
rsync -avz --exclude="*.json" --exclude="*.xls" --exclude="*.xlsx" u01/temp/ u01/perm/
  • Thank you for your reply. can you please share rsync code to copy all type files except '.JSON' and 'Excel' format files to /u01/perm path including folders.
    – cg369
    Apr 10, 2021 at 19:02
  • @cg369 If this solves your issue, please consider accepting the answer.
    – Kusalananda
    Apr 18, 2021 at 17:17

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .