Copy multiple files with same name from different directories

Thursday, May 30, 2013 , , , , 1 Comments

I regularly do this. So I automated it.
Below is a small simple script for copying files which are present in two different directories but without same names into a single directory with having name collision.

#!/usr/bin/sh

for i in `find <source_dir_path> -type f`
do
new=`echo $i|nawk -F"/" '{split($NF,a,".");print "<target_dir_path>"a[1]"_"$(NF-1)"."a[2]}'`
cp $i $new
done

1 comment:

  1. I have the files spm_2013Jun05.ps a few levels deep within each subfolder. I have been trying to move the files into /home/alistair/Documents/spmresults/. But only two itterations of the same filename have actually copied:
    -/home/alistair/Documents/spmresults/spm_2013Jun05.ps
    -/home/alistair/Documents/spmresults/spm_2013Jun05_1.ps

    Do you have any idea to correct this?

    for i in $(find ./ -name spm_2013May30.ps -type f)
    do
    new=`echo $i|nawk -F"/" '{split($NF,a,".");print"/home/alistair/Documents/spmresults/"a[1]"_"$(NF-1)"."a[2]}'`
    cp $i $new
    done

    ReplyDelete