I have around 180 files with the same structure: 000-aaaaaaaa.txt
.
Regular expression for a file name: /^[0-9]{3}\-[a-zA-Z]+$/gi
(3 digits + -
+ letters + .txt
).
I would like to cut of the numeral part and -
in each file name
For example
000-hello.txt
->hello.txt
001-world.txt
->world.txt
002-ubuntu.txt
->ubuntu.txt
003-linux.txt
->linux.txt
Renaming files using mmv:
You could also match digits with range match:
(recursively rename files):
;
Expands to any number of directories (same as**/
).*
Matches any char zero or more times.?
Matches any single character.[]
Matches a list or and a range of characters.#
References to the nth wildcard char in the from pattern.With Perl-based
rename
command:If the number of files is large enough to make the command exceed the shell's
ARG_MAX
, then you could use eitheror
Note that
[0-9][0-9][0-9]-*.txt
is processed by the shell and needs to be a shell glob expression rather than a regular expression.Remove the
-n
once you are happy that it is doing the right thing.Since I don't see it mentioned here yet, you can use repren. While it isn't installed by default, it does support regular expression-based file renaming. You can do just a single regular expression pattern like so:
The above example deletes the first 3 digits in all filenames if they are at the beginning thereof for all files recursively in the current directory. It does a dry run though to show you what it will do without actually doing it - remove the
--dry-run
bit once you're sure that it will do what you intend.repren
also supports pattern files, allowing you to do multiple replacements in 1 go:A pattern file looks like this:
...and so on.
Finally, it supports regular expression groups. Consider this pattern file:
The
\1
syntax inserts the contents of the first(bracketed)
group. To do this on the command-line, you'd need to use single quotes I think (correct me if I'm wrong):This is just scratching the surface of what
repren
is capable of though. It can optionally alter the contents of files too (hence the need for--rename
in all the above examples).Disclaimer: I am not affiliated with
repren
or it's development in any way, I just find it an invaluable time-saving tool.I know the question is tagged with command-line, but if a GUI tool would be useful to you, the Thunar file manager has a great bulk-rename tool. You can install Thunar directly from your package manager. Once it's installed, select the files of interest and then go to "Rename" under the Edit menu.
One big advantage of Thunar is that it gives you a preview of the new names, so you don't have to get your command or regex exactly right the first time. It can also do renaming tasks that are beyond the purview of regexes, like inserting consecutive numbers into file names.
There's more info about Thunar on the Ubuntu package website. Free Software Magazine has a nice overview article about it too, with screenshots.
I've done this often in the past using the
bulkrename
command inranger
, a terminal based file browser. The steps are as follows:v
.:bulkrename
command, this takes you to a list of all the selected file names in vim or your default editor.I generally construct a shell script to rename each file.
I put the output of ls into a file which I edit to make the script. This is trivial in
emacs
, and probably just as easy invim
.If I am still unsure of myself, I might have the files in a temporary subdirectory just in case something went wrong. I'd probably use a subdirectory if using
mmv
, as well.