- Find multiple files and rename them in Linux
- 13 Answers 13
- How do I easily rename multiple files using command line?
- 8 Answers 8
- Prefix
- Suffix
- Rename – A Command Line Tool For Renaming Multiple Files in Linux
- 1. A Basic Rename Command Example
- 2. Check Changes Before Running Rename Command
- 3. Print Rename Output
- 4. Convert all Lowercase to Uppercase and Vise-Versa
- 5. Capitalize First Letter of Filename
- 6. Overwrite Existing Files
Find multiple files and rename them in Linux
I am having files like a_dbg.txt, b_dbg.txt . in a Suse 10 system. I want to write a bash shell script which should rename these files by removing «_dbg» from them. Google suggested me to use rename command. So I executed the command rename _dbg.txt .txt *dbg* on the CURRENT_FOLDER My actual CURRENT_FOLDER contains the below files.
CURRENT_FOLDER/a_dbg.txt CURRENT_FOLDER/b_dbg.txt CURRENT_FOLDER/XX/c_dbg.txt CURRENT_FOLDER/YY/d_dbg.txt
CURRENT_FOLDER/a.txt CURRENT_FOLDER/b.txt CURRENT_FOLDER/XX/c_dbg.txt CURRENT_FOLDER/YY/d_dbg.txt
Its not doing recursively, how to make this command to rename files in all subdirectories. Like XX and YY I will be having so many subdirectories which name is unpredictable. And also my CURRENT_FOLDER will be having some other files also.
13 Answers 13
You can use find to find all matching files recursively:
find . -iname "*dbg*" -exec rename _dbg.txt .txt '<>' \;
EDIT: what the ‘<>‘ and \; are?
The -exec argument makes find execute rename for every matching file found. ‘<>‘ will be replaced with the path name of the file. The last token, \; is there only to mark the end of the exec expression.
All that is described nicely in the man page for find:
-exec utility [argument . ] ; True if the program named utility returns a zero value as its exit status. Optional arguments may be passed to the utility. The expression must be terminated by a semicolon (``;''). If you invoke find from a shell you may need to quote the semicolon if the shell would otherwise treat it as a control operator. If the string ``<>'' appears anywhere in the utility name or the argu- ments it is replaced by the pathname of the current file. Utility will be executed from the directory from which find was executed. Utility and arguments are not subject to the further expansion of shell patterns and constructs.
How do I easily rename multiple files using command line?
But in Ubuntu/Nautilus, I can’t tab to next file. But being on Linux, I think there must be a command line alternative. However, sometimes, I may want more control over how to rename specific files. In that case, perhaps its better to be able to tab to the next file
I think this question is not answered. What you are asking for (F2 and then jump in F2 mode to the next file) is currently not available I think in Nautilus.
8 Answers 8
I use rename all the time. It is pretty simple, but hopefully you know basic regex:
This will replace the string SEARCH with REPLACE in every file (that is, * ). The /g means global, so if you had a SEARCH_SEARCH.jpg , it would be renamed REPLACE_REPLACE.jpg . If you didn’t have /g , it would have only done substitution once, and thus now named REPLACE_SEARCH.jpg . If you want case-insensitive, add /i (that would be, /gi or /ig at the end).
With regular expressions, you can do lots more.
Note that this rename is the prename (aka Perl rename ) command, which supports complete Perl regular expressions. There is another rename which uses patterns, and is not as powerful. prename used to be installed by default on Ubuntu (along with Perl), but now you may have to do:
Prefix
Also you can remove unwanted strings. Let’s say you had 20 MP3 files named like CD RIP 01 Song.mp3 and you wanted to remove the «CD RIP» part, and you wanted to remove that from all of them with one command.
Notice the extra space in ‘^CD RIP ‘ , without the space all files would have a space as the first character of the file. Also note, this will work without the ^ character, but would match CD RIP in any part of the filename. The ^ guarantees it only removes the characters if they are the beginning of the file.
Suffix
will change Something.pdf into Something.doc . (The reason for the backslash is, . is a wildcard character in regexp so .pdf matches qPDF whereas \.pdf only matches the exact string .pdf . Also very important to note, if you are not familiar with BASH, you must put backslashes in SINGLE quotes! You may not omit quotes or use double quotes, or bash will try to translate them. To bash \. and «\.» equals . . (But double-quotes and backslashes are used, for example «\n» for a newline, but since «\.» isn’t a valid back escape sequence, it translates into . )
Actually, you can even enclose the parts of the string in quotes instead of the whole: ‘s/Search/Replace/g’ is the same as s/’Search’/’Replace’/g and s/Search/Replace/g to BASH. You just have to be careful about special characters (and spaces).
I suggest using the -n option when you are not positive you have the correct regular expressions. It shows what would be renamed, then exits without doing it. For example:
This will list all changes it would have made, had you not put the -n flag there. If it looks good, press Up to go back, then erase the -n and press Enter (or replace it with -v to output all changes it makes).
Note: Ubuntu versions above 17.04 don’t ship with rename by default, however it’s still available in the repositories. Use sudo apt install rename to install it
Rename – A Command Line Tool For Renaming Multiple Files in Linux
We often use “mv” command to rename a single file in Linux. However, renaming multiple or group of files quickly makes it very difficult task in a terminal.
Linux comes with a very powerful built-in tool called rename. The rename command is used to rename multiple or group of files, rename files to lowercase, rename files to uppercase and overwrite files using perl expressions.
The “rename” command is a part of Perl script and it resides under “/usr/bin/” on many Linux distributions. You can run “which” command to find out the location of rename command.
$ which rename /usr/bin/rename
The Basic Syntax of Rename Command
rename 's/old-name/new-name/' files
The rename command comes with few optional arguments along with mandatory perl expression that guides rename command to do actual work.
rename [ -v ] [ -n ] [ -f ] perlexpr [ files ]
- -v: Print names of files successfully renamed.
- -n: Show what files would have been renamed.
- -f: Force overwrite existing files.
- perlexpr: Perl Expression.
For better understanding of this utility, we’ve discussed few practical examples of this command in the article.
1. A Basic Rename Command Example
Suppose you’ve bunch of files with “.html” extension and you want to rename all “.html” files to “.php” at one go. For example, first do a “ls -l” to check the list of files with “.html” extension.
# [email protected]:~$ ls -l total 22532 -rw-rw-r-- 1 ravisaive ravisaive 6888896 Oct 10 12:10 cricket.html -rw-rw-r-- 1 ravisaive ravisaive 588895 Oct 10 12:10 entertainment.html -rw-rw-r-- 1 ravisaive ravisaive 6188895 Oct 10 12:10 health.html -rw-rw-r-- 1 ravisaive ravisaive 6538895 Oct 10 12:10 lifestyle.html -rw-rw-r-- 1 ravisaive ravisaive 938895 Oct 10 12:10 news.html -rw-rw-r-- 1 ravisaive ravisaive 938937 Oct 10 12:11 photos.html -rw-rw-r-- 1 ravisaive ravisaive 978137 Oct 10 12:11 sports.html
Now, you want to change the extension of all these files from “.html” to “.php“. You can use the following “rename” command with perl expression as shown below.
Note: In the above command we’ve used two arguments.
- First argument is a perl expression that substitute .html with .php.
- Second argument tells the rename command to substitute all the files with *.php.
Let’s verify whether all files are renamed to “.php” extension, doing ls -l on the prompt.
[email protected]:~$ ls -l total 22532 -rw-rw-r-- 1 ravisaive ravisaive 6888896 Oct 10 12:10 cricket.php -rw-rw-r-- 1 ravisaive ravisaive 588895 Oct 10 12:10 entertainment.php -rw-rw-r-- 1 ravisaive ravisaive 6188895 Oct 10 12:10 health.php -rw-rw-r-- 1 ravisaive ravisaive 6538895 Oct 10 12:10 lifestyle.php -rw-rw-r-- 1 ravisaive ravisaive 938895 Oct 10 12:10 news.php -rw-rw-r-- 1 ravisaive ravisaive 938937 Oct 10 12:11 photos.php -rw-rw-r-- 1 ravisaive ravisaive 978137 Oct 10 12:11 sports.php
Now you can see above that all the html files are renamed to php.
2. Check Changes Before Running Rename Command
While doing critical or major renaming tasks, you can always check the changes by running rename command with “-n” argument. The “-n” parameter will tell you exactly what changes would take place, but the changes are not done for real. Here, is the example of the command below.
[email protected]:~$ rename -n 's/\.php$/\.html/' *.php cricket.php renamed as cricket.html entertainment.php renamed as entertainment.html health.php renamed as health.html lifestyle.php renamed as lifestyle.html news.php renamed as news.html photos.php renamed as photos.html sports.php renamed as sports.html
Note: The above command output only displays changes, but in real the changes are not done, unless you run the command without “-n” switch.
3. Print Rename Output
We saw that the rename command didn’t displayed any information of changes it does. So, if you want to get the details of rename command (like we did using “-n” option), here we use “-v” option to print the complete details of all the changes done by rename command successfully.
[email protected]:~$ rename -v 's/\.php$/\.html/' *.php cricket.php renamed as cricket.html entertainment.php renamed as entertainment.html health.php renamed as health.html lifestyle.php renamed as lifestyle.html news.php renamed as news.html photos.php renamed as photos.html sports.php renamed as sports.html
4. Convert all Lowercase to Uppercase and Vise-Versa
To batch rename all files with lower case names to upper case. For example, I want to covert all these following files from lower to upper case.
Lower to Upper Case
[email protected]:~$ ls -l total 22532 -rw-rw-r-- 1 ravisaive ravisaive 6888896 Oct 10 12:10 cricket.html -rw-rw-r-- 1 ravisaive ravisaive 588895 Oct 10 12:10 entertainment.html -rw-rw-r-- 1 ravisaive ravisaive 6188895 Oct 10 12:10 health.html -rw-rw-r-- 1 ravisaive ravisaive 6538895 Oct 10 12:10 lifestyle.html -rw-rw-r-- 1 ravisaive ravisaive 938895 Oct 10 12:10 news.html -rw-rw-r-- 1 ravisaive ravisaive 938937 Oct 10 12:11 photos.html -rw-rw-r-- 1 ravisaive ravisaive 978137 Oct 10 12:11 sports.html
Just, use the following command with perl expression.
Once you’ve executed the above command, you can check the changes by doing “ls -l“.
[email protected]:~$ ls -l total 22532 -rw-rw-r-- 1 ravisaive ravisaive 6888896 Oct 10 12:10 CRICKET.HTML -rw-rw-r-- 1 ravisaive ravisaive 588895 Oct 10 12:10 ENTERTAINMENT.HTML -rw-rw-r-- 1 ravisaive ravisaive 6188895 Oct 10 12:10 HEALTH.HTML -rw-rw-r-- 1 ravisaive ravisaive 6538895 Oct 10 12:10 LIFESTYLE.HTML -rw-rw-r-- 1 ravisaive ravisaive 938895 Oct 10 12:10 NEWS.HTML -rw-rw-r-- 1 ravisaive ravisaive 938937 Oct 10 12:11 PHOTOS.HTML -rw-rw-r-- 1 ravisaive ravisaive 978137 Oct 10 12:11 SPORTS.HTML
You can see that the above command actually renamed all the lower case file names (with .HTML extension) to upper case.
Upper to Lower Case
Similarly, you can also convert all upper case characters to lower case using the following command.
[email protected]:~$ ls -l total 22532 -rw-rw-r-- 1 ravisaive ravisaive 6888896 Oct 10 12:10 cricket.html -rw-rw-r-- 1 ravisaive ravisaive 588895 Oct 10 12:10 entertainment.html -rw-rw-r-- 1 ravisaive ravisaive 6188895 Oct 10 12:10 health.html -rw-rw-r-- 1 ravisaive ravisaive 6538895 Oct 10 12:10 lifestyle.html -rw-rw-r-- 1 ravisaive ravisaive 938895 Oct 10 12:10 news.html -rw-rw-r-- 1 ravisaive ravisaive 938937 Oct 10 12:11 photos.html -rw-rw-r-- 1 ravisaive ravisaive 978137 Oct 10 12:11 sports.html
5. Capitalize First Letter of Filename
To capitalize only first letter of each filename use the following command.
6. Overwrite Existing Files
If you would like to forcefully overwrite existing files, use the “-f” option as shown below.
If you would like to know more about rename command, type the “man rename” in the terminal.
The rename command is very useful, if you are dealing with multiple or batch renaming of files from the command line. Do give a try and let me know, how far is useful in terms of renaming of files.