How could I rename multiple files like,
IonXpress_049.T11014.R_2014_11_13_11_26_35_user_PR2-41-Pooling0026_3140_13112014.bam
IonXpress_050.T11114.R_2014_11_13_11_26_35_user_PR2-41-Pooling0026_3140_13112014.bam
To,
T11014.bam
T11114.bam
rename 's/.*?\.([^.]+).*/$1.bam/' *.bam -vn
Remove the -vn
when you can see it does what you want. Here's my test harness:
$ touch 34234234kh.TESTING00{1..3}.234978623478y234.bam
$ rename 's/.*?\.([^.]+).*/$1.bam/' *.bam -vn
34234234kh.TESTING001.234978623478y234.bam renamed as TESTING001.bam
34234234kh.TESTING002.234978623478y234.bam renamed as TESTING002.bam
34234234kh.TESTING003.234978623478y234.bam renamed as TESTING003.bam
There are several ways to rename from command line. Here is a one liner. Go to the directory where the .bam
files are located and try this,
for i in *.bam; do mv "$i" "$(echo $i | awk -F"." '{print $2}').bam"; done
for i in *.bam do mv source destination done
.
using awk
as,$ echo IonXpress_049.T11014.R_2014_11_13_11_26_35_user_PR2-41-Pooling0026_3140_13112014.bam | awk -F"." '{print $2}' $ T11014
One can use custom field separator using -F
option. See man awk
for more.
You can use wonderful krename
program:
sudo apt-get install krename
In this program you have a preview of result, and can, in particular, use just certain range of file-name mask:
The mask is at ➀. See the original file names at ➁ and to-be names at ➂. If it seems fine you can press finish: ➃ — only after this the files will actually be renamed.
Use find command to finding all *.bam
files and by while loop read each files one by one and finally with mv
command rename them into your desired names.
find /path/to/maindir -type f -name "*.bam" -print0 | while IFS= read -d '' -r file
do
echo mv -v "$file" /path/to/maindir/"$(awk -F'.' '{print $2".bam"}'<<< "$file")";
done
Result:
mv -v /home/USER/maindir/IonXpress_049.T11014.R_2014_11_13_11_26_35_user_PR2-41-Pooling0026_3140_13112014.bam /home/USER/maindir/T11014.bam
mv -v /home/USER/maindir/IonXpress_050.T11114.R_2014_11_13_11_26_35_user_PR2-41-Pooling0026_3140_13112014.bam /home/USER/maindir/T11114.bam
If you checked the result remove the echo
command to real rename.
Assuming that
.bam
extension is always presentThis python solution should do the job. The number of sections may vary, it would still work:
#!/usr/bin/env python3
import os
import shutil
import sys
directory = sys.argv[1]
for item in os.listdir(directory):
if not item.startswith("."):
edit = item.split("."); newname = (".").join([edit[1], edit[-1]])
if len(edit) > 2:
shutil.move(directory+"/"+item, directory+"/"+newname)
Copy it into an empty file, make it executable (to run it without the python3
prefix) and run it by the command:
</path/to/script> <directory_with_files>
So that:
monkey.banana.peanut.bam
becomes
banana.bam
and
monkey.apple.peaunt.another_string.and_alot_more.bam
becomes
apple.bam
while
something.bam
is left alone.
Another python solution through re
module.
script.py
#!/usr/bin/python3
import sys
import re
import os
import shutil
directory = sys.argv[1]
for item in os.listdir(directory):
if '.' in item:
newname = re.sub(r'^[^.]+\.([^.]*).+(\.bam)', r'\1\2', item)
shutil.move(directory+"/"+item, directory+"/"+newname)
How to run?
python3 script.py /path/to/the/directory/where/the/files/you/want/to/rename/are/stored
While all these answers are good I want to show you a method that does not involve programming (and can be much faster when you have already Sublime Text installed)
ctrl+shif+p
> Install Package > FileBrowser http://github.com/aziz/SublimeFileBrowser)Configure the FileBrowser keyboard shortcut (ctrl+shift+p
> Key Bindings User) e.g.
{
"keys": ["f1"],
"command": "dired",
"args": { "immediate": true }
}
start the FileBrowser (F1
)
ctrl+d
)shift+R
)ctrl+right
to jump between words)Enter