2

I want to rename multiple files in multiple directories using Bash scripting. I have 772 such directories i.e from Nishi_001 to Nishi_772. Each directory has different number of files in it but in all directories, the naming of the files is similar as follows:

sample_001samout
sample_002samout
sample_003samout
sample_004samout
.
.
sample_00Nsamout  N=maximum 18

I want to rename the files in each directory to a particular name specific for each directory e.g., for Nishi_001 it contains the following files:

sample_001samout
sample_002samout
sample_003samout

to be renamed to the following format:

130906_Hiseq3A_l3_017_Dr_Nishikawa_AAGGTACA_L003_R1_001samout
130906_Hiseq3A_l3_017_Dr_Nishikawa_AAGGTACA_L003_R1_002samout
130906_Hiseq3A_l3_017_Dr_Nishikawa_AAGGTACA_L003_R1_003samout

The new name is different for each of the 772 directories. I have a file containing a list of the new names e.g

Nishi_003="130906_Hiseq3B_l7_003_Dr_Nishikawa_ATGCCTAA_L007_R1"
Nishi_004="130906_Hiseq3B_l7_004_Dr_Nishikawa_AGTGGTCA_L007_R1"
Nishi_005="130906_Hiseq3B_l7_005_Dr_Nishikawa_ACCACTGT_L007_R1"

All the files in a specific directory will all be renamed with the same name that will identify where the file came from. this new name will replace the phrase "sample" in all the files contained in that specific directory.

for example in the directory Nishi_001 the phrase "sample" in all the files should be replaced with

"130906_Hiseq3A_l3_017_Dr_Nishikawa_AAGGTACA_L003_R1" 

which is the new name specific for Nishi_001 directory

  • Are all of the Nishi directories contained in one parent directory? – Arronical Mar 31 '16 at 8:29
  • What OS are you using? You had tagged with Linux, but which one? Please edit your question to answer and also give us an example of your "names_to_replace", I can't understand what you need to rename to what. – terdon Mar 31 '16 at 8:45
  • Yes. All of the Nishi directories contained in one parent directory I am using Linux 2.6.32-504.3.3.el6.x86_64 – alex kiarie Mar 31 '16 at 8:57
  • The new name is different for each of the 772 directories. I have a file containing a list of the new names. All the files in a specific directory will all be renamed with the same name that will identify where the file came from. this new name will replace the phrase "sample" in all the files contained in that specific directory. – alex kiarie Mar 31 '16 at 9:49
4

You need to iterate over your map file, reading the directory and name pattern into two variables, and using them in the mv command:

sed 's/"//g' map.txt | 
    while IFS="=" read dir name; do 
        for file in "$dir"/*; do 
            mv "$file" "${file/sample/$name}"
        done
    done 

Explanation

  • sed 's/"//g' map.txt : remove the quotes around the target names.
  • while IFS="=" read dir name; do : $IFS="=" tells your shell to split input on =, so read dir name will read the target directory into the variable $dir and the name into $name. Since this is run on the output of the sed command, this will same the $name without the quotes.
  • for file in "$dir"/*; do : for each file in each $dir.
  • mv "$file" "${file/sample/$name}" : the syntax ${var/pattern/replacement} print $var with the string pattern replaced by replacement. So, here, it will print the file name with sample replaced by whatever the current value of $name.
  • beat me to it. my version is almost identical except i used sed rather than ${file/sample/$name}, and i used $prefix rather than $name. +1 – cas Apr 4 '16 at 9:02
1
#!/bin/bash
for file in sample_00*
do
    replace_name=$(echo $file | sed "s/sample/130906_Hiseq3A_l3_017_Dr_Nishikawa_AAGGTACA_L003_R1/g" | cat )
    mv $file $replace_name
done

reference : How to rename multiple files in single command or script in Unix?

  • This works if i do this for one directory at a time. The new name is different for each of the 772 directories. – alex kiarie Mar 31 '16 at 9:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.