'Recursively find files with a specific extension and copy them to a new directory with new names

I tried to find all the .xvg files generated by Gromacs inside a directory as well as its subdirectories and then copy them to a new directory, with new names for each file (the rule for naming is 'original path + name').

I used the following commands:

find . -type f -iname '*.xvg' -exec sh -c '
    path="${1%/*}"; filename="${1##*/}";
    echo cp "${1}" "test0605/${path##*/}_${filename}" ' sh_cp {} \;

The terminal seemed to work properly:

$ find . -type f -iname '*.xvg' -exec sh -c '
     path="${1%/*}"; filename="${1##*/}";
     echo cp "${1}" "test0605/${path##*/}_${filename}" ' sh_cp {} \;
cp ./Loop2/Replica5/rmsf_10ns.xvg test0605/Replica5_rmsf_10ns.xvg
cp ./Loop2/Replica5/rmsd.xvg test0605/Replica5_rmsd.xvg
cp ./Loop2/Replica5/gyrate.xvg test0605/Replica5_gyrate.xvg
cp ./Loop2/Replica4/rmsf_10ns.xvg test0605/Replica4_rmsf_10ns.xvg
cp ./Loop2/Replica4/rmsd.xvg test0605/Replica4_rmsd.xvg
cp ./Loop2/Replica4/gyrate.xvg test0605/Replica4_gyrate.xvg
cp ./Fab_fMD_ff15_n160_10ns/rmsf_10ns.xvg test0605/Fab_fMD_ff15_n160_10ns_rmsf_10ns.xvg
cp ./Fab_fMD_ff15_n160_10ns/rmsd.xvg test0605/Fab_fMD_ff15_n160_10ns_rmsd.xvg
cp ./Fab_fMD_ff15_n160_10ns/gyrate.xvg test0605/Fab_fMD_ff15_n160_10ns_gyrate.xvg
cp ./Loop1/Replica1/rmsf_10ns.xvg test0605/Replica1_rmsf_10ns.xvg
cp ./Loop1/Replica1/rmsd.xvg test0605/Replica1_rmsd.xvg
cp ./Loop1/Replica1/gyrate.xvg test0605/Replica1_gyrate.xvg
cp ./Loop1/Replica2/rmsf_10ns.xvg test0605/Replica2_rmsf_10ns.xvg
cp ./Loop1/Replica2/rmsd.xvg test0605/Replica2_rmsd.xvg
cp ./Loop1/Replica2/gyrate.xvg test0605/Replica2_gyrate.xvg
cp ./Loop1/Replica3/rmsf_10ns.xvg test0605/Replica3_rmsf_10ns.xvg
cp ./Loop1/Replica3/rmsd.xvg test0605/Replica3_rmsd.xvg
cp ./Loop1/Replica3/gyrate.xvg test0605/Replica3_gyrate.xvg

However, when I opened the test0605 folder it was empty inside, with no files actually being copied into it. So I was just wondering if anyone happens to know the reason.

Many thanks.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source