Mass renaming files is no possible with the standard linux command
mv, but it’s possible to achieve this goal in many different ways, from some
bash magic, to programs that do exactly this, in this article I’ll work with both the terminal and with graphical tools.
First example, you have these files:
myconf.sh myfile2.txt anotherfile.sh test.sh
And you want to add to all the files that end with .sh a .bak, these are some possible solutions
rename perlexpr [ files ]
rename is a perl script which can be used to mass rename files according to a regular expression.
The perlexpr argument is a Perl expression which is expected to modify the $_ string in Perl for at least some of the filenames specified. If a given filename is not modified by the expression, it will not be renamed. If no filenames are given on the command line, filenames will be read via standard input.
So in our case we could use
rename 's/.sh/.sh.bak/g' *.sh
/.sh/=string to search for substitution.
/.sh.bak/ = new string that will be put in place.
g=global, does this for every occurrence of the string.
This will substitute the string .sh to .sh.bak, so we’ll get exactly what we want:
anotherfile.sh.bak myconf.sh.bak myfile2.txt test.sh.bak
mmv, is a program to move/copy/append/link multiple files according to a set of wildcard patterns. This multiple action is performed safely, i.e. without any unexpected deletion of files due to collisions of target names with existing filenames or with other target names. Furthermore, before doing anything, mmv attempts to detect any errors that would result from the entire set of actions specified and gives the user the choice of either aborting before beginning, or proceeding by avoiding the offending parts.
This command has many options that can do different things, like moving in sub-directories and do a safe copy of every file they touch, but for our small example you could simply use this command:
mmv \*.sh \#1.sh.bak
With bash commands you can do almost anything in many different ways, so I’ll just show 1 simple way to rename multiple files using the standard
for i in *.sh; do mv $i `basename $i sh`sh.bak; done
This small example uses the command
basename, that print the name of the file given as argument with any leading directory components removed and in this case removes the trailing suffix, to add to the file another one.
Use Midnight Commander to Rename Multiple Files
GNU Midnight Commander (also known as MC) is a free cross-platform orthodox file manager and a clone of Norton Commander using it you can rename multiple files as explained below.
- Select the required files using regular expression. Press
+which will ask the regex to select files. For example, giving *.sh will highlight all the files with .sh extension.
- Rename all the selected files using regex. Press
F6which will ask for the source and destination regex, doing so will change the file names. For this example, give *.sh in source and *.sh.bak in destination which will rename do what we need.
Thunar mass rename
Thunar is a file manager for Linux and other Unix-like systems, written using the GTK+ 2 toolkit, and shipped with Xfce version 4.4 RC1 and later. To rename file with Thunar just follow these simple instructions:
Open thunar on the directory where your files are located and select all of them
Now press F2 or go in Edit -> Rename to start the Bulk Renamer dialog box that has many different options:
Audio Tags – Lets you modify file names based on the ID3 tags
Insert Date/Time – To insert in your files the date and time.
Insert/Overwrite – Allows you to put a piece of information somewhere in the title of the file, or replace part of the file name with entirely new text.
Numbering – To insert a number at the start of every file
Remove Characters – Allows you to get rid characters, useful to remove spaces or underscore.
Search & Replace – This is the most powerful tool and you can use regexp with it.
Uppercase/Lowercase – This option allow you to “normalize” a text, so you could make it all lowercase.
Choose Search & Replace, search for .sh and replace with .sh.bak, all done !
Thunar is just one of the graphical tool that you can use, it’s possible to use also Nautilus and specific tools such as:
KRename is a powerful batch renamer for KDE. It allows you to easily rename hundreds or even more files in one go.
GPRename is a complete batch renamer for files and directories
- An introduction to systemd for CentOS 7
- Linux Games: Sanctum 2
- What goes around comes around: nearly half of DDoS attacked companies are hit twice or more
- Linux Terminal: An lsof Primer
- How to check if you are vulnerable to shellshock
Find me on Google+