0

I have a number of directories, all of which contain a file named "accepted_hits.bam". I'd like to rename each of those files as "accepted_hits_ID.bam", where ID corresponds to the directory name that the file is found within. For instance, the directories are named like 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 like the accepted_hits files to be named

accepted_hits_0.bam

accepted_hits_1.bam

etc.

I suspect that there's a clever way to do this on the command line, but so far I haven't proven that clever. Any help is appreciated.

Forest
  • 721
  • 1
  • 8
  • 14

1 Answers1

0

Right, figured it out. In my initial search through past stackoverflow questions, I missed this one: Rename files in multiple directories to the name of the directory

I used the advice from that post to write the command:

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

which resulted in the 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. :-)

Community
  • 1
  • 1
Forest
  • 721
  • 1
  • 8
  • 14