Both rename and mv are available with most linux distros. Is there any compelling reason to switch to brename?
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.
3 answers
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.
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.
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.
Here are three main reasons I recommend the tools I wrote.
- Brename supports dry run, by showing which files are going to be renamed and what the new names are. This makes users feel safe.
- Besides, it detects potential overwriting conflicts before renaming, which helps avoid overwriting important files like raw FASTQ reads.
- At last, it can undo the last operation like the
ctrl + zshortcut. 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.
try:
rename -n 's/^(\w+)_.*/$1/' *.tab or rename -n 's/_.*//' *.tab. Remove -n if you are okay with dummy run.
Log in to answer this question.
You can use bash parameter expansion/substring removal to do this: https://wiki.bash-hackers.org/syntax/pe#substring_removal