Batch rename files with perl expressions

This should be a basic question for many people, but I'm a biologist with no programming, so please excuse my question.

What I'm trying to do is rename about 100,000 gzipped data files that have an existing code name (example: XG453834.fasta.gz). I would like to call them easy to read and analyze by me (example: Xanthomonas_galactus_str_453.fasta.gz).

I tried to use sed , rename and mmv to no avail. If I use any of these commands in a one-time script, then they work fine, just when I try to include variables in a shell script, I run into problems. I am not getting any errors, just the names are not changed, so I suspect this is an I / O error.

This is what my files look like:

 #! /bin/bash # change a bunch of file names file=names.txt while IFS=' ' read -r r1 r2; do mmv ''$r1'.fasta.gz' ''$r2'.fasta.gz' # or I tried many versions of: sed -i 's/"$r1"/"$r2"/' *.gz # and I tried many versions of: rename -i 's/$r1/$r2/' *.gz done < "$file" 

... and here are the first lines of my txt file with a single space separator:

  cat names.txt #find #replace code1 name1 code2 name2 code3 name3 

I know that I can do this with python or perl, but since I'm stuck here working on this particular script, I want to find a simple solution to fix this bash script and find out what I am doing wrong. Thanks so much for any help.

Also, I tried the cat name file (see comment from Ashoka Lella below) and then use awk to move / rename. Some of the files have variable names (but always begin with code), so I'm looking for a search and replace option to just replace the "code" with the "name" and keep the file name structure.

I suspect that I am not avoiding the variable within the same tick of the perl expression, but I have typed a lot of guides and I cannot find a way to do this.

+5
source share
3 answers

If you are absolutely sure that the file names do not contain spaces , you can try the following

 xargs -n2 < names.txt echo mv 

This is for a DRY run (it will print only what it will do) - if you are satisfied with the result, delete echo ...

If you want to verify the existence of a target, use

 xargs -n2 < names.txt echo mv -i 

if you want to NEVER overwrite your intended use

 xargs -n2 < names.txt echo mv -n 

delete echo if you are satisfied.

+3
source

I don’t think you need to use mmv , just mv . In addition, there is no need to specify IFS , by default it will work for you:

 while read -r src dest; do mv "$src" "$dest"; done < names.txt 

I quoted variable names twice, as this is usually considered good practice, but in this case, a space in any of the file names will cause read not work as you expect.

You can put echo in front of mv inside the loop to make sure the correct command is executed.

Note that the suffix .fasta.gz already included in your names.txt file, so you should not add it inside the loop. Perhaps this was your problem?

+3
source

This should rename all the files in column 1 to column2 names.txt. If they are in the same folder as names.txt

 cat names.txt| awk '{print "mv "$1" "$2}'|sh 
+1
source

Source: https://habr.com/ru/post/1202014/


All Articles