Batch Renaming Files
2
0
Entering edit mode
6.2 years ago
Tastulek • 0

Hello BioStar Community,

I would like to rename multiple files in a folder using R script (or scripts written in Python or Perl).

The files in the folder look like:

  1. ABCD123QW.txt
  2. ABCD345XB.txt
  3. ....

And I have a text file which contains Old Names and corresponding New Names:

  1. Old Name New Name
  2. ABCD123QW.txt AL001.txt
  3. ABCD345XB.txt AL002.txt
  4. ... ...

I am a novice in the field of bioinformatics, could anyone help with the file renaming issue?

R Python Perl SNP genome • 4.2k views
ADD COMMENT
0
Entering edit mode

This is a screenshot after executing the bash script two times.

The 1st time, only one file was renamed, but no error message. The 2nd time, an error message popped up, as shown in the screenshot, which is expectable

ADD REPLY
1
Entering edit mode

I don't know why it didn't work in your computer here I attached a GIF Animation.

ADD REPLY
0
Entering edit mode

The Screenshot:

  1. MacBook-Pro:BashCode altaic$ ls
  2. ABCD123QW.txt ABCD345XB.txt input.txt script.sh
  3. MacBook-Pro:BashCode altaic$ bash script.sh input.txt
  4. MacBook-Pro:BashCode altaic$ ls
  5. ABCD345XB.txt AL001.txt input.txt script.sh
  6. MacBook-Pro:BashCode altaic$ bash script.sh input.txt
  7. mv: ABCD123QW.txt: No such file or directory
  8. MacBook-Pro:BashCode altaic$
ADD REPLY
0
Entering edit mode

Please use ADD COMMENT/ADD REPLY when responding to existing posts to keep threads logically organized.

All of your comments regarding bash script should have gone under @Arup's answer.

ADD REPLY
6
Entering edit mode
6.2 years ago

Here's a solution in Python. It will work with different operating systems (Windows, Linux, MacOS, etc.). Make sure the script is in the same directory as the files you want to rename.

Python code (script.py)

import os
import sys

with open(sys.argv[1]) as fh:
    for line in fh:
        sl = line.strip().split(",")
        old_name = sl[0]
        new_name = sl[1]
        os.rename(old_name, new_name)

How to use

python script.py input.txt

Input.txt

ABCD123QW.txt,AL001.txt
ABCD345XB.txt,AL002.txt

Demo

Look at the screenshot of my terminal on Mac. First I display all files that are in the directory (ABCD123QW.txt, ABCD345XB.txt, input.txt, script.py). Then, I run the script and list files again. You can see that two text files were renamed to AL001.txt and AL002.txt.

enter image description here

ADD COMMENT
0
Entering edit mode

Thank you for the Python script! But I am having problems with executing the script...

>>> os.listdir()
['ABCD345XB.txt', 'input.txt', 'ABCD123QW.txt', 'script.py']
>>> python script.py input.txt
SyntaxError: invalid syntax
>>>
ADD REPLY
0
Entering edit mode

Python scripts need to be executed from the command prompt (under Windows) or terminal (under Linux/MacOs). The prompt >>> indicates that you are now in an interactive Python interpeter session, also called the “Python shell”. This is different from the normal terminal command prompt.

ADD REPLY
0
Entering edit mode

Thank you for the note!

I switched to the terminal (Mac), and ran the script, but still there was an error message:

>>> pprint(dirlist)
['ABCD345XB.txt', 'input.txt', 'ABCD123QW.txt', 'script.py']
>>>  python script.py input.txt
  File "<stdin>", line 1
    python script.py input.txt
    ^
IndentationError: unexpected indent
>>>
ADD REPLY
1
Entering edit mode

I updated my answer by adding a screenshot of my terminal on Mac.

ADD REPLY
0
Entering edit mode

Thank you very much, the python code script.py) worked well!

By the way, why does the python code work in Mac terminal [--- -bash --- 80 x 24], but not in python [--- python --- 80 x 24]?

ADD REPLY
1
Entering edit mode
6.2 years ago

I have a bash script to do the same thing but it requires comma-separated file as input.

#!/bin/bash
INPUT="$1"
IFS=,
[ ! -f "$INPUT" ] && { echo "$INPUT file not found"; exit 99; }
while read old_name new_name
do
rename "$old_name" "$new_name" # mv or rename
done < "$INPUT"

How to use:

bash script.sh input.txt

Input.txt structure:

Do not include any header.

ABCD123QW.txt,AL001.txt
ABCD345XB.txt,AL002.txt
ADD COMMENT
0
Entering edit mode

Arup, Thank you for the bash script!

ADD REPLY
1
Entering edit mode

If an answer was helpful you should upvote it, if the answer resolved your question you should mark it as accepted. You can accept multiple answers as correct.
Upvote|Bookmark|Accept

ADD REPLY
0
Entering edit mode

I have executed the bash script, and it worked! But unfortunately, only the name of the first file was changed to AL001, but not the other files...

Any solutions for this?

ADD REPLY
0
Entering edit mode

Can you share error you are getting or first few lines of the csv file you are using, the script working perfectly fine in my computer with multiple file name as input.

ADD REPLY

Login before adding your answer.

Traffic: 1532 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6