Shell script for read files recursively from subfolder and copy into different path

Hi All,

I need shell script to copy the files ‘.txt’, ‘.csv’ from one path, capture the path to create same path folders and paste the files into newly created path in linux.

example:

there are 10k files with different formats.

  1. need sheel script to copy all ‘.txt’, ‘.csv’ files from /u01/proj/sales/subject1/ path
  2. create same folder path in /u01/sales/subject1 and save those ‘.txt’ & ‘.csv’ files.

Thanks,

You can use rsync with suitable filter rules, something like this:

rsync --dry-run -av --filter='+ *.txt' --filter='+ *.csv' --filter='+ **/' --filter='- *' --prune-empty-dirs from-dir/ to-dir/

--dry-run only shows what would be copied, remove that option to actually copy. Rsync also lets you load filters from a file, check the -F option.

1 Like