{"id":13235,"date":"2019-04-01T08:57:05","date_gmt":"2019-04-01T08:57:05","guid":{"rendered":"http:\/\/www.appservgrid.com\/paw92\/?p=13235"},"modified":"2019-04-01T08:57:05","modified_gmt":"2019-04-01T08:57:05","slug":"rename-a-command-line-tool-for-renaming-multiple-files-in-linux","status":"publish","type":"post","link":"https:\/\/www.appservgrid.com\/paw92\/index.php\/2019\/04\/01\/rename-a-command-line-tool-for-renaming-multiple-files-in-linux\/","title":{"rendered":"Rename \u2013 A Command Line Tool For Renaming Multiple Files in Linux"},"content":{"rendered":"<p>We often use \u201c<strong>mv<\/strong>\u201d command to rename a single file in\u00a0<strong>Linux<\/strong>. However, renaming multiple or group of files quickly makes it very difficult task in a terminal.<\/p>\n<p><strong>Linux<\/strong>\u00a0comes with a very powerful built-in tool called\u00a0<strong>rename<\/strong>. The\u00a0<strong>rename<\/strong>\u00a0command is used to rename multiple or group of files, rename files to lowercase, rename files to uppercase and overwrite files using perl expressions.<\/p>\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2013\/10\/Rename-Commands.png\"><img decoding=\"async\" class=\"aligncenter size-medium wp-image-4395\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2013\/10\/Rename-Commands.png\" alt=\"Rename Multiple Files In Linux\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p>The \u201c<strong>rename<\/strong>\u201d command is a part of\u00a0<strong>Perl<\/strong>\u00a0script and it resides under \u201c<strong>\/usr\/bin\/<\/strong>\u201d on many Linux distributions. You can run \u201c<strong>which<\/strong>\u201d command to find out the location of rename command.<\/p>\n<pre>$ which rename\r\n\/usr\/bin\/rename<\/pre>\n<h5>The Basic Syntax of Rename Command<\/h5>\n<pre>rename 's\/old-name\/new-name\/' files<\/pre>\n<p>The rename command comes with few optional arguments along with mandatory\u00a0<strong>perl expression<\/strong>\u00a0that guides rename command to do actual work.<\/p>\n<pre>rename [ -v ] [ -n ] [ -f ] perlexpr [ files ]<\/pre>\n<ol>\n<li><strong>-v<\/strong>: Print names of files successfully renamed.<\/li>\n<li><strong>-n<\/strong>: Show what files would have been renamed.<\/li>\n<li><strong>-f<\/strong>: Force overwrite existing files.<\/li>\n<li><strong>perlexpr<\/strong>: Perl Expression.<\/li>\n<\/ol>\n<p>For better understanding of this utility, we\u2019ve discussed few practical examples of this command in the article.<\/p>\n<h3>1. A Basic Rename Command Example<\/h3>\n<p>Suppose you\u2019ve bunch of files with \u201c<strong>.html<\/strong>\u201d extension and you want to rename all \u201c<strong>.html<\/strong>\u201d files to \u201c<strong>.php<\/strong>\u201d at one go. For example, first do a \u201c<strong>ls -l<\/strong>\u201d to check the list of files with \u201c<strong>.html<\/strong>\u201d extension.<\/p>\n<pre># ravisaive@tecmint:~$ ls -l\r\ntotal 22532\r\n-rw-rw-r-- 1 ravisaive ravisaive 6888896 Oct 10 12:10 cricket.html\r\n-rw-rw-r-- 1 ravisaive ravisaive  588895 Oct 10 12:10 entertainment.html\r\n-rw-rw-r-- 1 ravisaive ravisaive 6188895 Oct 10 12:10 health.html\r\n-rw-rw-r-- 1 ravisaive ravisaive 6538895 Oct 10 12:10 lifestyle.html\r\n-rw-rw-r-- 1 ravisaive ravisaive  938895 Oct 10 12:10 news.html\r\n-rw-rw-r-- 1 ravisaive ravisaive  938937 Oct 10 12:11 photos.html\r\n-rw-rw-r-- 1 ravisaive ravisaive  978137 Oct 10 12:11 sports.html<\/pre>\n<p>Now, you want to change the extension of all these files from \u201c<strong>.html<\/strong>\u201d to \u201c<strong>.php<\/strong>\u201c. You can use the following \u201c<strong>rename<\/strong>\u201d command with\u00a0<strong>perl expression<\/strong>\u00a0as shown below.<\/p>\n<pre>ravisaive@tecmint:~$ rename 's\/\\.html$\/\\.php\/' *.html<\/pre>\n<p><strong>Note<\/strong>: In the above command we\u2019ve used two arguments.<\/p>\n<ol>\n<li>First argument is a\u00a0<strong>perl expression<\/strong>\u00a0that substitute\u00a0<strong>.html<\/strong>\u00a0with\u00a0<strong>.php<\/strong>.<\/li>\n<li>Second argument tells the rename command to substitute all the files with\u00a0<strong>*.php<\/strong>.<\/li>\n<\/ol>\n<p>Let\u2019s verify whether all files are renamed to \u201c<strong>.php<\/strong>\u201d extension, doing\u00a0<strong>ls -l<\/strong>\u00a0on the prompt.<\/p>\n<pre>ravisaive@tecmint:~$ ls -l\r\ntotal 22532\r\n-rw-rw-r-- 1 ravisaive ravisaive 6888896 Oct 10 12:10 cricket.php\r\n-rw-rw-r-- 1 ravisaive ravisaive  588895 Oct 10 12:10 entertainment.php\r\n-rw-rw-r-- 1 ravisaive ravisaive 6188895 Oct 10 12:10 health.php\r\n-rw-rw-r-- 1 ravisaive ravisaive 6538895 Oct 10 12:10 lifestyle.php\r\n-rw-rw-r-- 1 ravisaive ravisaive  938895 Oct 10 12:10 news.php\r\n-rw-rw-r-- 1 ravisaive ravisaive  938937 Oct 10 12:11 photos.php\r\n-rw-rw-r-- 1 ravisaive ravisaive  978137 Oct 10 12:11 sports.php<\/pre>\n<p>Now you can see above that all the\u00a0<strong>html<\/strong>\u00a0files are renamed to\u00a0<strong>php<\/strong>.<\/p>\n<h3>2. Check Changes Before Running Rename Command<\/h3>\n<p>While doing critical or major renaming tasks, you can always check the changes by running rename command with \u201c<strong>-n<\/strong>\u201d argument. The \u201c<strong>-n<\/strong>\u201d 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.<\/p>\n<pre>ravisaive@tecmint:~$ rename -n 's\/\\.php$\/\\.html\/' *.php\r\n\r\ncricket.php renamed as cricket.html\r\nentertainment.php renamed as entertainment.html\r\nhealth.php renamed as health.html\r\nlifestyle.php renamed as lifestyle.html\r\nnews.php renamed as news.html\r\nphotos.php renamed as photos.html\r\nsports.php renamed as sports.html<\/pre>\n<p><strong>Note<\/strong>: The above command output only displays changes, but in real the changes are not done, unless you run the command without \u201c<strong>-n<\/strong>\u201d switch.<\/p>\n<h3>3. Print Rename Output<\/h3>\n<p>We saw that the rename command didn\u2019t displayed any information of changes it does. So, if you want to get the details of rename command (like we did using \u201c<strong>-n<\/strong>\u201d option), here we use \u201c<strong>-v<\/strong>\u201d option to print the complete details of all the changes done by rename command successfully.<\/p>\n<pre>ravisaive@tecmint:~$ rename -v 's\/\\.php$\/\\.html\/' *.php\r\n\r\ncricket.php renamed as cricket.html\r\nentertainment.php renamed as entertainment.html\r\nhealth.php renamed as health.html\r\nlifestyle.php renamed as lifestyle.html\r\nnews.php renamed as news.html\r\nphotos.php renamed as photos.html\r\nsports.php renamed as sports.html<\/pre>\n<h3>4. Convert all Lowercase to Uppercase and Vise-Versa<\/h3>\n<p>To batch rename all files with\u00a0<strong>lower case<\/strong>\u00a0names to\u00a0<strong>upper case.<\/strong>\u00a0For example, I want to covert all these following files from\u00a0<strong>lower<\/strong>\u00a0to\u00a0<strong>upper<\/strong>\u00a0case.<\/p>\n<h5>Lower to Upper Case<\/h5>\n<pre>ravisaive@tecmint:~$ ls -l\r\ntotal 22532\r\n-rw-rw-r-- 1 ravisaive ravisaive 6888896 Oct 10 12:10 cricket.html\r\n-rw-rw-r-- 1 ravisaive ravisaive  588895 Oct 10 12:10 entertainment.html\r\n-rw-rw-r-- 1 ravisaive ravisaive 6188895 Oct 10 12:10 health.html\r\n-rw-rw-r-- 1 ravisaive ravisaive 6538895 Oct 10 12:10 lifestyle.html\r\n-rw-rw-r-- 1 ravisaive ravisaive  938895 Oct 10 12:10 news.html\r\n-rw-rw-r-- 1 ravisaive ravisaive  938937 Oct 10 12:11 photos.html\r\n-rw-rw-r-- 1 ravisaive ravisaive  978137 Oct 10 12:11 sports.html<\/pre>\n<p>Just, use the following command with perl expression.<\/p>\n<pre>ravisaive@tecmint:~$ rename 'y\/a-z\/A-Z\/' *.html<\/pre>\n<p>Once you\u2019ve executed the above command, you can check the changes by doing \u201c<strong>ls -l<\/strong>\u201c.<\/p>\n<pre>ravisaive@tecmint:~$ ls -l\r\ntotal 22532\r\n-rw-rw-r-- 1 ravisaive ravisaive 6888896 Oct 10 12:10 CRICKET.HTML\r\n-rw-rw-r-- 1 ravisaive ravisaive  588895 Oct 10 12:10 ENTERTAINMENT.HTML\r\n-rw-rw-r-- 1 ravisaive ravisaive 6188895 Oct 10 12:10 HEALTH.HTML\r\n-rw-rw-r-- 1 ravisaive ravisaive 6538895 Oct 10 12:10 LIFESTYLE.HTML\r\n-rw-rw-r-- 1 ravisaive ravisaive  938895 Oct 10 12:10 NEWS.HTML\r\n-rw-rw-r-- 1 ravisaive ravisaive  938937 Oct 10 12:11 PHOTOS.HTML\r\n-rw-rw-r-- 1 ravisaive ravisaive  978137 Oct 10 12:11 SPORTS.HTML<\/pre>\n<p>You can see that the above command actually renamed all the lower case file names (with\u00a0<strong>.HTML<\/strong>\u00a0extension) to upper case.<\/p>\n<h5>Upper to Lower Case<\/h5>\n<p>Similarly, you can also convert all\u00a0<strong>upper case<\/strong>\u00a0characters to\u00a0<strong>lower case<\/strong>\u00a0using the following command.<\/p>\n<pre>ravisaive@tecmint:~$ rename 'y\/A-Z\/a-z\/' *.HTML<\/pre>\n<pre>ravisaive@tecmint:~$ ls -l\r\ntotal 22532\r\n-rw-rw-r-- 1 ravisaive ravisaive 6888896 Oct 10 12:10 cricket.html\r\n-rw-rw-r-- 1 ravisaive ravisaive  588895 Oct 10 12:10 entertainment.html\r\n-rw-rw-r-- 1 ravisaive ravisaive 6188895 Oct 10 12:10 health.html\r\n-rw-rw-r-- 1 ravisaive ravisaive 6538895 Oct 10 12:10 lifestyle.html\r\n-rw-rw-r-- 1 ravisaive ravisaive  938895 Oct 10 12:10 news.html\r\n-rw-rw-r-- 1 ravisaive ravisaive  938937 Oct 10 12:11 photos.html\r\n-rw-rw-r-- 1 ravisaive ravisaive  978137 Oct 10 12:11 sports.html<\/pre>\n<h3>5. Capitalize First Letter of Filename<\/h3>\n<p>To capitalize only first letter of each filename use the following command.<\/p>\n<pre># rename 's\/\\b(\\w)\/\\U$1\/g' *.ext\r\n<\/pre>\n<div id=\"attachment_30266\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2013\/10\/Capitalize-First-Letter-Filename.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-30266\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2013\/10\/Capitalize-First-Letter-Filename.png\" alt=\"Capitalize First Letter Filename\" width=\"650\" height=\"137\" aria-describedby=\"caption-attachment-30266\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p id=\"caption-attachment-30266\" class=\"wp-caption-text\">Capitalize First Letter Filename<\/p>\n<\/div>\n<h3>6. Overwrite Existing Files<\/h3>\n<p>If you would like to forcefully overwrite existing files, use the \u201c<strong>-f<\/strong>\u201d option as shown below.<\/p>\n<pre>ravisaive@tecmint:~$ rename -f 's\/a\/b\/' *.html<\/pre>\n<p>If you would like to know more about rename command, type the \u201c<strong>man rename<\/strong>\u201d in the terminal.<\/p>\n<p>The\u00a0<strong>rename<\/strong>\u00a0command 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.<\/p>\n<p><a href=\"https:\/\/www.tecmint.com\/rename-multiple-files-in-linux\/\" target=\"_blank\" rel=\"noopener\">Source<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>We often use \u201cmv\u201d command to rename a single file in\u00a0Linux. However, renaming multiple or group of files quickly makes it very difficult task in a terminal. Linux\u00a0comes with a very powerful built-in tool called\u00a0rename. The\u00a0rename\u00a0command is used to rename multiple or group of files, rename files to lowercase, rename files to uppercase and overwrite &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.appservgrid.com\/paw92\/index.php\/2019\/04\/01\/rename-a-command-line-tool-for-renaming-multiple-files-in-linux\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Rename \u2013 A Command Line Tool For Renaming Multiple Files in Linux&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-13235","post","type-post","status-publish","format-standard","hentry","category-linux"],"_links":{"self":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/13235","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/comments?post=13235"}],"version-history":[{"count":1,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/13235\/revisions"}],"predecessor-version":[{"id":13236,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/13235\/revisions\/13236"}],"wp:attachment":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/media?parent=13235"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/categories?post=13235"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/tags?post=13235"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}