Renaming files that contain different numbers using rename command-Linux
3
0
Entering edit mode
2.6 years ago
tenten • 0

I have multiple files that have the following format: (note that file corresponds to each file name which is not common).

File1_S20.tab
File2_S25.tab
File3_S40.tab

etc

I want to rename them all so they become:

File1
File2
File3

etc

Basically removing the _S$$.tab part from all the files.

For renaming files, I usually use the rename command as follows: rename # somethingelse *.tab (replace # by somethingelse). But the only trouble I'm have is each file has different number after the S.

linux coding • 3.1k views
ADD COMMENT
3
Entering edit mode

You can use bash parameter expansion/substring removal to do this: https://wiki.bash-hackers.org/syntax/pe#substring_removal

mv ${f} ${f%*_or_something_like_that}
ADD REPLY
1
Entering edit mode
2.6 years ago
GenoMax 141k

Following should work:

$ for i in *.tab; do name=$(echo ${i} | cut -f1 -d "_" ); echo ${name} ; echo mv ${i} ${name}; done
File1
mv File1_S20.tab File1
File2
mv File2_S25.tab File2
File3
mv File3_S40.tab File3

Remove echo before mv when all looks good to actually change the names.

ADD COMMENT
1
Entering edit mode
2.6 years ago

Try brename, safe and powerful.

brename -p "_S.*"
ADD COMMENT
0
Entering edit mode

Both rename and mv are available with most linux distros. Is there any compelling reason to switch to brename?

ADD REPLY
2
Entering edit mode

Here are three main reasons I recommend the tools I wrote.

  1. Brename supports dry run, by showing which files are going to be renamed and what the new names are. This makes users feel safe.
  2. Besides, it detects potential overwriting conflicts before renaming, which helps avoid overwriting important files like raw FASTQ reads.
  3. At last, it can undo the last operation like the ctrl + z shortcut. This gives users a chance to rescue the wrong move, which is extremely useful when newly renamed containing less information to recover the original names.

In short, it's safe for batch renaming lots of important files.

ADD REPLY
1
Entering edit mode

I like the undo option. Can you creae a Tool type post advertising this utility? I have more questions!

ADD REPLY
0
Entering edit mode

It looks like @shenwei356 is one of the authors. It's definitely a useful tool just from the short amount of browsing I did on their Github repository.

ADD REPLY
0
Entering edit mode

I understand that they're the author. I'm looking for a compelling reason to switch to a third party app for something as simple as renaming files. There cannot be significant performance improvements - are there short-hands that could possibly become so convenient that they become indispensable? (zsh's anywhere-in-filename autocomplete is one such feature). If not, there's no reason to switch.

ADD REPLY
0
Entering edit mode
2.6 years ago

try:

rename -n 's/^(\w+)_.*/$1/' *.tab or rename -n 's/_.*//' *.tab. Remove -n if you are okay with dummy run.

ADD COMMENT

Login before adding your answer.

Traffic: 2629 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