0

I am new learner of Linux - Ubuntu. I need to rename a large number of files (keeping same extension) corresponding to a list of names in a file. I have a list of names in a text file.

For example

Current filename => New filename

GCA_001340506.1_ASM54621.fna => wax.fna

GCF_0133435.1_ASM4354653.fna => strains2213.fna

I need to rename current filename with the new filename. As we can see, I need two things first to match the corresponding filename and then rename it.

I cannot find a proper way to complete this task. Kindly help me in this regard.

Awan

7
  • but the old filename and the new filename are both in the text file ?
    – Lews
    Oct 10, 2020 at 16:33
  • @Furqan Identical question appeard a few mins ago; Such a coincidence! unix.stackexchange.com/questions/613880/… Oct 10, 2020 at 17:13
  • 1
    @Lews Yes both old and new filenames are present in the text file.
    – Furqan
    Oct 10, 2020 at 17:20
  • 1
    @WilliamMartens I don't know that person but I must appreciate he explained my query in a much better way.
    – Furqan
    Oct 10, 2020 at 17:21
  • 1
    @WilliamMartens Thanks. I am also surprised to see. Hopefully following solutions will help me that I am going to try.
    – Furqan
    Oct 10, 2020 at 17:25

3 Answers 3

2

If your file format is source.fna => target.fna you can use mmv to rename your files. By leaving out the from to arguments on the command-line, mmv will take source-target pair, one per line, on the standard input.

awk -F '=>' '{print $1 $2}' files.txt | mmv

mmv can actually handle this format directly, but it's more of a coincidence.

mmv ignores lines on the standard input that look like its own error and "done" messages, as well as all lines beginning with white space, and will accept pattern pairs with or without the intervening "->" (or "-^", "=>", or "=^").

cat file.txt | mmv
0

I created the following function. you have to use in this way:

$ furqan_mv filenames.txt

the file filenames.txt must have the following format:

oldfile => newfile

the extension is always fna. Here is the function:

furqan_mv() {
ext=".fna"
while IFS= read -r line; do
    oldfile=`echo $line | awk '{print $1}'`
    newfile=`echo $line | awk '{print $3}'`
    mv $oldfile.$ext $newfile.$ext
done < $1
}
2
  • Sorry forgive my ignorance. I saved this function in a file with .sh extension. Am I correct? I am sure this function will also be working but more flexible then the another solution suggested. Please correct me how to save this function and with which extension?
    – Furqan
    Oct 10, 2020 at 18:43
  • no, you have to copy and paste the function directly on bash. But when you exit from the shell or reboot the pc you lose the function. If you want to keep it you can paste it on your ~/.bashrc file. Before to use it make a backup of the folder where you use it.
    – Lews
    Oct 10, 2020 at 20:19
0

welcome to Askubuntu!

Would this work?


I took the names from this:

https://unix.stackexchange.com/questions/613880/rename-multiple-files-accordingly-with-names-list#613880

since you don't include the contents of the file where the new filenames are

for file in *.fna;

 do
  read line;
   
   mv -v "${file}" "${line}"; done < names.txt

Test run outputs e.g files

  • 1.pdf > Fabio.pdf
8
  • 1
    Sorry I just mentioned one more thing. How it would match the current filename with the new filename. I have a list of new names in a file. I must mention that each new file name is different.
    – Furqan
    Oct 10, 2020 at 16:29
  • Oh, that's a bit more confusing, Okay working on it, :P +Thanks for pointing the details out, it really helps and clarifies the goal Oct 10, 2020 at 16:29
  • If you highlight all the files and right click and choose 'rename' you will be presented with a very useful batch filename editor that may be of use.
    – Johnny5ive
    Oct 10, 2020 at 16:37
  • @Furqan I've updated the answer, is it satisfactory? (Added a very near identical reference to a stack overflow page) //Wishes Oct 10, 2020 at 17:09
  • 1
    @WilliamMartens. Great work. Just need to take care of the sorting order of the files and it worked like magic.
    – Furqan
    Oct 10, 2020 at 18:20

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .