I would probably use awk
, but that's just a matter of taste, not of right and wrong:
find . -name accepted_hits.bam | awk '{ print "cp -pi \"" $0 "\" \"" $0 "\"_" NR "; mv -i \"" $0 "\"_" NR " ./bam-files/" }' | tcsh -f
where you need to create a directory ./bam-files/
before calling above line, or adjust the directory name. I have intentionally used -p
for preserving file times and -i
for avoiding overwrites, just in case. So, before calling the commands for a 2nd time please clean the target directory.
If you wish to remove the original files please replace the cp -pi
by mv -i
.
The script leaves out calls to basename
in the hope to run into less trouble if the pathname contains special characters (spaces and most special characters are handled).
PS: You could also do a dry-run before executing by leaving out the | tcsh -cf
at the end. If you do not have tcsh
installed (yes, there are Linuxes without it) feel free to use sh
or bash
.