Saturday, 15 March 2014

unix - Rename certain files within all directories -



unix - Rename certain files within all directories -

i have number of directories, of contain file named "accepted_hits.bam". i'd rename each of files "accepted_hits_id.bam", id corresponds directory name file found within. instance, directories named so:

drwxr-xr-x 3 fr2259 ac_lab 249 oct 4 04:33 0

drwxr-xr-x 4 fr2259 ac_lab 76 oct 3 20:16 1

drwxr-xr-x 4 fr2259 ac_lab 76 oct 3 20:29 10

drwxr-xr-x 4 fr2259 ac_lab 76 oct 3 20:30 11

drwxr-xr-x 3 fr2259 ac_lab 249 oct 3 20:03 12

drwxr-xr-x 4 fr2259 ac_lab 76 oct 3 20:52 13

and i'd accepted_hits files named

accepted_hits_0.bam

accepted_hits_1.bam

etc.

i suspect there's clever way on command line, far haven't proven clever. help appreciated.

right, figured out. in initial search through past stackoverflow questions, missed one: rename files in multiple directories name of directory

i used advice post write command:

for subdir in *; mv $subdir/accepted_hits.bam accepted_hits_$subdir.bam; done;

which resulted in following:

drwxrwxr-x. 2 fr2259 ac_lab 0 oct 4 12:19 0

drwxrwxr-x. 2 fr2259 ac_lab 0 oct 4 12:19 1

drwxrwxr-x. 2 fr2259 ac_lab 0 oct 4 12:19 2

drwxrwxr-x. 2 fr2259 ac_lab 0 oct 4 12:19 3

-rw-r--r--. 1 fr2259 ac_lab 4750137 oct 4 12:11 accepted_hits_0.bam

-rw-r--r--. 1 fr2259 ac_lab 95087 oct 4 12:18 accepted_hits_1.bam

-rw-r--r--. 1 fr2259 ac_lab 1095044 oct 4 12:18 accepted_hits_2.bam

-rw-r--r--. 1 fr2259 ac_lab 470320 oct 4 12:18 accepted_hits_3.bam

lesson learned; search harder. :-)

unix command-line rename

No comments:

Post a Comment