Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

batch renaming of files with perl expressions

This should be a basic question for a lot of people, but I am a biologist with no programming background, so please excuse my question.

What I am trying to do is rename about 100,000 gzipped data files that have existing name of a code (example: XG453834.fasta.gz). I'd like to name them to something easily readable and parseable by me (example: Xanthomonas_galactus_str_453.fasta.gz).

I've tried to use sed, rename, and mmv, to no avail. If I use any of those commands on a one-off script then they work fine, it's just when I try to incorporate variables into a shell script do I run into problems. I'm not getting any errors, just no names are changed, so I suspect it's an I/O error.

Here's 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's the first lines of my txt file with single space delimiter:

    cat names.txt

   #find #replace 
   code1 name1
   code2 name2
   code3 name3

I know 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 fixing this bash script and figure out what I am doing wrong. Thanks so much for any help possible.

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

I suspect I am not escaping the variable within the single tick of the perl expression, but I have poured over a lot of manuals and I can't find the way to do this.

like image 319
Josh Herr Avatar asked Sep 07 '14 15:09

Josh Herr


People also ask

How do I batch rename multiple files at once?

To rename multiple files from File Explorer, select all the files you wish to rename, then press the F2 key. The name of the last file will become highlighted. Type the new name you wish to give to every file, then press Enter.

What is the fastest way to rename files in bulk?

You can press and hold the Ctrl key and then click each file to rename. Or you can choose the first file, press and hold the Shift key, and then click the last file to select a group.

Is there a program that can rename multiple files at once?

Advanced Renamer is a program for renaming multiple files and folders at once. By configuring renaming methods the names can be manipulated in various ways. It is easy to set up a batch job using multiple methods on a large amount of files.


2 Answers

If you're absolutely sure than the filenames doesn't contain spaces of tabs, you can try the next

xargs -n2 < names.txt echo mv

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

If you want check the existence ot the target, use

xargs -n2 < names.txt echo mv -i

if you want NEVER allow overwriting of the target use

xargs -n2 < names.txt echo mv -n

again, remove the echo if youre satisfied.

like image 164
jm666 Avatar answered Sep 28 '22 17:09

jm666


I don't think that you need to be using mmv, a simple mv will do. Also, there's no need to specify the IFS, the default will work for you:

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

I have double quoted the variable names as it is generally considered good practice but in this case, a space in either of the filenames will result in read not working as you expect.

You can put an echo before the mv inside the loop to ensure that the correct command will be executed.

Note that in your file names.txt, the .fasta.gz suffix is already included, so you shouldn't be adding it inside the loop aswell. Perhaps that was your problem?

like image 36
Tom Fenech Avatar answered Sep 28 '22 17:09

Tom Fenech