Issue
I have a list of files in a directory with the following pattern:
my-file-1-xxxyy-all.txt
file-2-xx-all.txt
ThirdFile-xxxx-all.txt
I would like to rename the files by replacing the last occurrence of the pattern "-*-" by "-":
So the desired result would be
my-file-1-all.txt
file-2-all.txt
ThirdFile-all.txt
What would be the best way to achieve this under unix?
Thanks much
Solution
Use this :
rename -n 's/-[^-]+-all\./-all./' *txt
Remove -n
switch when the output looks good.
There are other tools with the same name which may or may not be able to do this, so be careful.
If you run the following command (GNU
)
$ file "$(readlink -f "$(type -p rename)")"
and you have a result that contains Perl script, ASCII text executable
and not containing ELF
, then this seems to be the right tool =)
If not, to make it the default (usually already the case) on Debian
and derivative like Ubuntu
:
$ sudo update-alternatives --set rename /path/to/rename
Replace /path/to/rename
to the path of your perl rename
executable.
If you don't have this command, search your package manager to install it or do it manually (no deps...)
This tool was originally written by Larry Wall, the Perl's dad.
Answered By - Gilles Quenot Answer Checked By - Willingham (WPSolving Volunteer)