{"id":392443,"date":"2022-01-11T14:00:00","date_gmt":"2022-01-11T11:00:00","guid":{"rendered":"https:\/\/en.buradabiliyorum.com\/how-to-use-the-rename-command-on-linux\/"},"modified":"2022-01-11T14:00:00","modified_gmt":"2022-01-11T11:00:00","slug":"how-to-use-the-rename-command-on-linux","status":"publish","type":"post","link":"https:\/\/buradabiliyorum.com\/en\/how-to-use-the-rename-command-on-linux\/","title":{"rendered":"#How to Use the rename Command on Linux"},"content":{"rendered":"<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_84 counter-hierarchy ez-toc-counter ez-toc-custom ez-toc-container-direction\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<label for=\"ez-toc-cssicon-toggle-item-6a29201d28708\" class=\"ez-toc-cssicon-toggle-label\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #dd3333;color:#dd3333\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #dd3333;color:#dd3333\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/label><input type=\"checkbox\"  id=\"ez-toc-cssicon-toggle-item-6a29201d28708\" checked aria-label=\"Toggle\" \/><nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-use-the-rename-command-on-linux\/#Whats_Wrong_With_mv\" >What\u2019s Wrong With mv?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-use-the-rename-command-on-linux\/#Renaming_a_Single_File_With_mv\" >Renaming a Single File With mv<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-use-the-rename-command-on-linux\/#Renaming_Multiple_Files_with_mv\" >Renaming Multiple Files with mv<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-use-the-rename-command-on-linux\/#What_Just_Happened\" >What Just Happened?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-use-the-rename-command-on-linux\/#There_Has_to_be_a_Simpler_Way\" >There Has to be a Simpler Way<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-use-the-rename-command-on-linux\/#Lets_Do_That_Again\" >Let\u2019s Do That Again<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-use-the-rename-command-on-linux\/#What_Happened_This_TIme\" >What Happened This TIme?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-8\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-use-the-rename-command-on-linux\/#Changing_Other_Parts_of_a_Filename\" >Changing Other Parts of a Filename<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-9\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-use-the-rename-command-on-linux\/#Deleting_Part_of_a_Filename\" >Deleting Part of a Filename<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-10\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-use-the-rename-command-on-linux\/#Limit_Changes_to_Specific_Parts_of_Filenames\" >Limit Changes to Specific Parts of Filenames<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-11\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-use-the-rename-command-on-linux\/#Searching_With_Groupings\" >Searching With Groupings<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-12\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-use-the-rename-command-on-linux\/#Using_Translations_With_rename\" >Using Translations With rename<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-13\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-use-the-rename-command-on-linux\/#You_WoDont_Learn_Perl_in_Five_Minutes\" >You (Wo|Do)n\u2019t Learn Perl in Five Minutes<\/a><\/li><\/ul><\/nav><\/div>\n<p><strong>&#8220;#How to Use the rename Command on Linux&#8221;<\/strong><\/p>\n<div>\n<!-- UNCACHED CONTENT --><\/p>\n<figure style=\"width: 1200px\" class=\"wp-caption alignnone\"><img loading=\"lazy\" decoding=\"async\" class=\"type:primaryImage size-full wp-image-778840\" data-pagespeed-lazy-srcset=\"https:\/\/www.howtogeek.com\/wp-content\/uploads\/2019\/06\/Linux-laptop-showing-a-bash-prompt.jpg?width=398&amp;trim=1,1&amp;bg-color=000&amp;pad=1,1 400w, https:\/\/www.howtogeek.com\/wp-content\/uploads\/2019\/06\/Linux-laptop-showing-a-bash-prompt.jpg?width=1198&amp;trim=1,1&amp;bg-color=000&amp;pad=1,1 1200w\" sizes=\"auto, 400w, 1200w\" data-pagespeed-lazy-src=\"https:\/\/www.howtogeek.com\/wp-content\/uploads\/2019\/06\/Linux-laptop-showing-a-bash-prompt.jpg?width=1198&amp;trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"Linux laptop showing a bash prompt\" width=\"1200\" height=\"675\" src=\"\/pagespeed_static\/1.JiBnMqyl6S.gif\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><figcaption class=\"wp-caption-text\"><span class=\"type:primaryImage imagecredit\"><a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/www.shutterstock.com\/image-vector\/linux-interface-screen-notebook-world-map-321627716\">fatmawati achmad zaenuri\/Shutterstock.com<\/a><\/span><\/figcaption><\/figure>\n<p>Get to grips with the file renaming powerhouse of the Linux world and give <code>mv<\/code>\u2014and yourself\u2014a rest. <code>Rename<\/code> is flexible, fast, and sometimes even easier.\u00a0 Here\u2019s a tutorial to this powerhouse of a command.<\/p>\n<h2 role=\"heading\" aria-level=\"2\"><span class=\"ez-toc-section\" id=\"Whats_Wrong_With_mv\"><\/span>What\u2019s Wrong With mv?<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>There\u2019s nothing wrong with <code>mv<\/code> . The command does <a rel=\"nofollow noopener\" target=\"_blank\" href=\"http:\/\/man7.org\/linux\/man-pages\/man1\/mv.1.html\">a fine a job<\/a>, and it is found on all Linux distributions, in macOS, and in other Unix-like operating systems. So it\u2019s always available.\u00a0But sometimes you just need a bulldozer, not a shovel.<\/p>\n<p>The <code>mv<\/code> command has a purpose in life, and that is to move files. It is a h<a href=\"https:\/\/buradabiliyorum.com\/en\/category\/download-scripts-themes-apps\/\" data-internallinksmanager029f6b8e52c=\"9\" title=\"Download Scripts &amp; Themes &amp; Apps\" target=\"_blank\" rel=\"noopener\">app<\/a>y side effect that it can be used to move an existing file <em>into<\/em> a new file, with a new name. The net effect is to rename the file, so we get what we want. But <code>mv<\/code> is not a dedicated file renaming tool.<\/p>\n<h2 role=\"heading\" aria-level=\"2\"><span class=\"ez-toc-section\" id=\"Renaming_a_Single_File_With_mv\"><\/span>Renaming a Single File With mv<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>To use <code>mv<\/code> to rename a file type <code>mv<\/code>, a space, the name of the file, a space, and the new name you wish the file to have. Then press Enter.<\/p>\n<p>You can use\u00a0<code>ls<\/code> to check the file has been renamed.<\/p>\n<pre>mv oldfile.txt newfile.txt<\/pre>\n<pre>ls *.txt<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-423224\" data-pagespeed-lazy-src=\"https:\/\/www.howtogeek.com\/wp-content\/uploads\/2019\/05\/rename_8.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"\" width=\"646\" height=\"132\" src=\"\/pagespeed_static\/1.JiBnMqyl6S.gif\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<h2 role=\"heading\" aria-level=\"2\"><span class=\"ez-toc-section\" id=\"Renaming_Multiple_Files_with_mv\"><\/span>Renaming Multiple Files with mv<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Things get trickier when you want to rename multiple files. <code>mv<\/code> has no capability to deal with renaming multiple files. You must resort to using some nifty Bash tricks.\u00a0That\u2019s fine if you know some medium-grade command-line fu, but the complexity of renaming multiple files with <code>mv<\/code> stands in stark contrast to the ease of using <code>mv<\/code> to rename a single file.<\/p>\n<p>Things escalate quickly.<\/p>\n<p>Let\u2019s say we\u2019ve got a directory with a variety of files in it, of differing types. Some of these files have a \u201c.prog\u201d extension. We want to rename them at the command line so that they have a \u201c.prg\u201d extension.<\/p>\n<p>How do we wrangle <code>mv<\/code> into doing that for us? Let\u2019s take a look at the files.<\/p>\n<pre>ls *.prog -l<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-423227\" data-pagespeed-lazy-src=\"https:\/\/www.howtogeek.com\/wp-content\/uploads\/2019\/05\/rename_5.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"\" width=\"646\" height=\"292\" src=\"\/pagespeed_static\/1.JiBnMqyl6S.gif\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>Here\u2019s one way to do it that doesn\u2019t resort to writing an actual Bash script file.<\/p>\n<pre>for f in *.prog; do mv -- \"$f\" \"${f%.prog}.prg\"<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-423228\" data-pagespeed-lazy-src=\"https:\/\/www.howtogeek.com\/wp-content\/uploads\/2019\/05\/rename_6.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"\" width=\"646\" height=\"77\" src=\"\/pagespeed_static\/1.JiBnMqyl6S.gif\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>DId that work? Let\u2019s check the files and see.<\/p>\n<pre>ls *.pr*<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-423229\" data-pagespeed-lazy-src=\"https:\/\/www.howtogeek.com\/wp-content\/uploads\/2019\/05\/rename_7.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"\" width=\"646\" height=\"122\" src=\"\/pagespeed_static\/1.JiBnMqyl6S.gif\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>So, yes, it worked. They\u2019re all \u201c.prg\u201d files now, and there are no \u201c.prog\u201d files in the directory.<\/p>\n<h2 role=\"heading\" aria-level=\"2\"><span class=\"ez-toc-section\" id=\"What_Just_Happened\"><\/span>What Just Happened?<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>What did that long command actually do? Let\u2019s break it down.<\/p>\n<pre>for f in *.prog; do mv -- \"$f\" \"${f%.prog}.prg\"<\/pre>\n<p>The first part starts a loop that is going to process each \u201c.prog\u201d file in the directory, in turn.<\/p>\n<p>The next part says what the processing will <em>do<\/em>.\u00a0It is using\u00a0<code>mv<\/code> to move each file to a new file. The new file is going to be named with the original file\u2019s name excluding the\u00a0 \u201c.prog\u201d part. A new extension of \u201c.prg\u201d\u00a0will be used instead.<\/p>\n<h2 role=\"heading\" aria-level=\"2\"><span class=\"ez-toc-section\" id=\"There_Has_to_be_a_Simpler_Way\"><\/span>There Has to be a Simpler Way<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Most definitely. It is the <code>rename<\/code> command.<\/p>\n<p><code>rename<\/code> is not part of a standard Linux distribution, so you will need to install it. It also has a different name in different families of Linux, but they all work the same way.\u00a0You\u2019ll just have to substitute the appropriate command name according to the Linux flavor you\u2019re using.<\/p>\n<p>in Ubuntu and Debian-derived distributions you install <code>rename<\/code> like this:<\/p>\n<pre>sudo apt-get install rename<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-423235\" data-pagespeed-lazy-src=\"https:\/\/www.howtogeek.com\/wp-content\/uploads\/2019\/05\/rename_1.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"\" width=\"646\" height=\"57\" src=\"\/pagespeed_static\/1.JiBnMqyl6S.gif\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>In Fedora and RedHat-derived distributions you install <code>prename<\/code> like this. Note the initial \u201cp,\u201d which stands for Perl.<\/p>\n<pre>sudo dnf install prename<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-423236\" data-pagespeed-lazy-src=\"https:\/\/www.howtogeek.com\/wp-content\/uploads\/2019\/05\/rename_2.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"\" width=\"643\" height=\"57\" src=\"\/pagespeed_static\/1.JiBnMqyl6S.gif\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>To install it in Manjaro Linux use the following command. Note that the renaming command is called <code>perl-rename<\/code>.<\/p>\n<pre>sudo pacman -Syu perl-rename<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-423237\" data-pagespeed-lazy-src=\"https:\/\/www.howtogeek.com\/wp-content\/uploads\/2019\/05\/rename_3.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"\" width=\"646\" height=\"57\" src=\"\/pagespeed_static\/1.JiBnMqyl6S.gif\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<h2 role=\"heading\" aria-level=\"2\"><span class=\"ez-toc-section\" id=\"Lets_Do_That_Again\"><\/span>Let\u2019s Do That Again<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>And this time we\u2019ll use <code>rename<\/code>.\u00a0We\u2019ll roll back the clock so that we have a set of \u201c.prog\u201d files.<\/p>\n<pre>ls *.prog<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-423241\" data-pagespeed-lazy-src=\"https:\/\/www.howtogeek.com\/wp-content\/uploads\/2019\/05\/rename_9.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"\" width=\"646\" height=\"122\" src=\"\/pagespeed_static\/1.JiBnMqyl6S.gif\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>Now let\u2019s use the following command to rename them. We\u2019ll then check with <code>ls<\/code> whether it worked. Remember to substitute <code>rename<\/code> with the appropriate command name for your Linux if you\u2019re not using Ubuntu or a Debian-derived Linux.<\/p>\n<pre>rename 's\/.prog\/.prg\/' *.prog<\/pre>\n<pre>ls *.pr*<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-423242\" data-pagespeed-lazy-src=\"https:\/\/www.howtogeek.com\/wp-content\/uploads\/2019\/05\/rename_10.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"\" width=\"646\" height=\"147\" src=\"\/pagespeed_static\/1.JiBnMqyl6S.gif\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>That worked, they\u2019re now all \u201c.prg\u201d files, and there are no \u201c.prog\u201d files left in the directory.<\/p>\n<h2 role=\"heading\" aria-level=\"2\"><span class=\"ez-toc-section\" id=\"What_Happened_This_TIme\"><\/span>What Happened This TIme?<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Let\u2019s explain that bit of magic, in three parts.<\/p>\n<p>The <em>first<\/em> part is the command name, <code>rename<\/code> (or <code>prename<\/code> or <code>perl-rename<\/code> , for the other distributions).<\/p>\n<p>The <em>last<\/em> part is <code>*.prog<\/code>, which tells <code>rename<\/code> to operate on all \u201c.prog\u201d files.<\/p>\n<p>The <em>middle<\/em> part defines the work we want to be done on each filename. The <code>s<\/code> means substitute. The first term (<code>.prog<\/code>) is what <code>rename<\/code> will search for in each filename and the second term (<code>.prg<\/code>)\u00a0 is what it will be substituted with.<\/p>\n<p>The middle part of the command, or central expression, is a Perl \u2018<a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/perldoc.perl.org\/perlre.html#Regular-Expressions\">regular expression<\/a>\u2018 and it is what gives the <code>rename<\/code> command its flexibility.<\/p>\n<h2 role=\"heading\" aria-level=\"2\"><span class=\"ez-toc-section\" id=\"Changing_Other_Parts_of_a_Filename\"><\/span>Changing Other Parts of a Filename<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>We\u2019ve changed filename extensions so far, let\u2019s amend other parts of the filenames.<\/p>\n<p>In the directory are a lot of C source code files. All of the filenames are prefixed with \u201cslang_\u201d. We can check this with <code>ls<\/code>.<\/p>\n<pre>ls sl*.c<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-423248\" data-pagespeed-lazy-src=\"https:\/\/www.howtogeek.com\/wp-content\/uploads\/2019\/05\/rename_11.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"\" width=\"646\" height=\"212\" src=\"\/pagespeed_static\/1.JiBnMqyl6S.gif\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>We are going to replace all occurrences of \u201cslang_\u201d with \u201csl_\u201d. The format of the command is already familiar to us. We\u2019re just changing the search term, the replacement term, and the file type.<\/p>\n<pre>rename 's\/slang_\/sl_' *.c<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-423247\" data-pagespeed-lazy-src=\"https:\/\/www.howtogeek.com\/wp-content\/uploads\/2019\/05\/rename_12.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"\" width=\"646\" height=\"77\" src=\"\/pagespeed_static\/1.JiBnMqyl6S.gif\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>This time we are looking for \u201c.c\u201d files, and searching for \u201cslang_\u201d. Whenever \u201cslang_\u201d is found in a filename it is substituted with \u201csl_\u201d.<\/p>\n<p>We can check the result of that command by repeating the <code>ls<\/code> command from above with the same parameters:<\/p>\n<pre>ls sl*.c<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-423246\" data-pagespeed-lazy-src=\"https:\/\/www.howtogeek.com\/wp-content\/uploads\/2019\/05\/rename_13.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"\" width=\"646\" height=\"197\" src=\"\/pagespeed_static\/1.JiBnMqyl6S.gif\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<h2 role=\"heading\" aria-level=\"2\"><span class=\"ez-toc-section\" id=\"Deleting_Part_of_a_Filename\"><\/span>Deleting Part of a Filename<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>We can remove a part of a filename by substituting the search term with nothing.<\/p>\n<pre>ls *.c<\/pre>\n<pre>rename 's\/sl_\/\/' *.c<\/pre>\n<pre>ls *.c<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-423251\" data-pagespeed-lazy-src=\"https:\/\/www.howtogeek.com\/wp-content\/uploads\/2019\/05\/rename_16.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"\" width=\"646\" height=\"352\" src=\"\/pagespeed_static\/1.JiBnMqyl6S.gif\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>We can see from the <code>ls<\/code> command that our \u201c.c\u201d files are all prepended with \u201csl_\u201d. Let\u2019s get rid of that altogether.<\/p>\n<p>The <code>rename<\/code> command follows the same format as before. We\u2019re going to be looking for \u201c.c\u201d files. The search term is \u201csl_\u201d, but there is no substitution term. Two backslashes without anything between them means nothing, an empty string.<\/p>\n<p><code>rename<\/code> will process each \u201c.c\u201d file in turn. It will search for \u201csl_\u201d in the filename. If it is found, it will be replaced by nothing. In other words, the search term is deleted.<\/p>\n<p>The second use of the <code>ls<\/code> command confirms that the \u201csl_\u201d prefix has been removed from every \u201c.c\u201d file.<\/p>\n<h2 role=\"heading\" aria-level=\"2\"><span class=\"ez-toc-section\" id=\"Limit_Changes_to_Specific_Parts_of_Filenames\"><\/span>Limit Changes to Specific Parts of Filenames<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Let\u2019s use <code>ls<\/code>\u00a0to look at files that have the string \u201cparam\u201d in their filename. Then we\u2019ll use <code>rename<\/code> to replace that string with the string \u201cparameter\u201d. We\u2019ll use <code>ls<\/code> once more to see the effect the <code>rename<\/code> command has had on those files.<\/p>\n<pre>ls *param*<\/pre>\n<pre>rename 's\/param\/parameter' *.c<\/pre>\n<pre>ls *param*<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-423261\" data-pagespeed-lazy-src=\"https:\/\/www.howtogeek.com\/wp-content\/uploads\/2019\/05\/rename_17.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"\" width=\"646\" height=\"197\" src=\"\/pagespeed_static\/1.JiBnMqyl6S.gif\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>Four files are found that have \u201cparam\u201d in their filename. param.c, param_one.c, and param_two.c all have \u201cparam\u201d at the <em>start<\/em> of their name. third_param.c has \u201cparam\u201d at the <em>end<\/em> of its name, just before the extension.<\/p>\n<p>The <code>rename<\/code> command is going to search for \u201cparam\u201d everywhere in the filename, and replace it with \u201cparameter\u201d in all cases.<\/p>\n<p>The second use of the\u00a0<code>ls<\/code> command shows us that that is exactly what has happened. Whether \u201cparam\u201d was at the start or at the end of the filename, it has been replaced by \u201cparameter.\u201d<\/p>\n<p>We can use Perl\u2019s metacharacters to refine the behavior of the middle expression. Metacharacters are symbols that represent positions or sequences of characters. For example, <code>^<\/code> means \u201cstart of a string,\u201d <code>$<\/code> means \u201cend of a string,\u201d and <code>.<\/code> means any single character (apart from a newline character).<\/p>\n<p>We\u2019re going to use the start of string metacharacter ( <code>^<\/code> ) to restrict our search to the start of the filenames.<\/p>\n<pre>ls *param*.c<\/pre>\n<pre>rename 's\/^parameter\/value\/' *.c<\/pre>\n<pre>ls *param*.c<\/pre>\n<pre>ls value*.c<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-423262\" data-pagespeed-lazy-src=\"https:\/\/www.howtogeek.com\/wp-content\/uploads\/2019\/05\/rename_18.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"\" width=\"646\" height=\"247\" src=\"\/pagespeed_static\/1.JiBnMqyl6S.gif\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>The files we renamed earlier are listed, and we can see the string \u201cparameter\u201d is at the start of three filenames and it is at the end of one of the filenames.<\/p>\n<p>Our <code>rename<\/code> command uses the start of line\u00a0<code>(^<\/code>)\u00a0 metacharacter before the search term \u201cparameter.\u201d This tells <code>rename<\/code> to only consider the search term to have been found if it is at the start of the filename. The search string \u201cparameter\u201d will be ignored if it is anywhere else in the filename.<\/p>\n<p>Checking with <code>ls<\/code>, we can see that the filename that had \u201cparameter\u201d at the <em>end<\/em> of the filename has not been modified, but the\u00a0three filenames that had \u201cparameter\u201d at the <em>start<\/em> of their names have had the search string replaced by the substitute term \u201cvalue.\u201d<\/p>\n<p>The power of <code>rename<\/code> lies in the power of Perl.\u00a0All of the <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/luv.asn.au\/overheads\/perl\/man.html\">power of Perl<\/a> is at your disposal.<\/p>\n<h2 role=\"heading\" aria-level=\"2\"><span class=\"ez-toc-section\" id=\"Searching_With_Groupings\"><\/span>Searching With Groupings<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p><code>rename<\/code> has yet more tricks up its sleeve. Let\u2019s consider the case where you might have files with similar strings in their names. They\u2019re not exactly the same strings, so a simple search and substitution won\u2019t work here.<\/p>\n<p>In this example we use <code>ls<\/code>\u00a0to check which files we have that start with \u201cstr\u201d. There are two of them, string.c and strangle.c. We can rename both strings at once using a technique called grouping.<\/p>\n<p>The central expression of this <code>rename<\/code> command will search for strings within filenames that have the character sequence \u201cstri\u201d <em>or<\/em> \u201cstra\u201d where those sequences are im<a href=\"https:\/\/buradabiliyorum.com\/en\/category\/social-mediaa\/\" data-internallinksmanager029f6b8e52c=\"1\" title=\"Social Media\" target=\"_blank\" rel=\"noopener\">media<\/a>tely followed by \u201cng\u201d. In other words, our search term is going to look for \u201cstring\u201d <em>and<\/em> \u201cstrang\u201d. The substitution term is \u201cbang\u201d.<\/p>\n<pre>ls str*.c<\/pre>\n<pre>rename 's\/(stri|stra)ng\/bang\/' *.c<\/pre>\n<pre>ls ban*.c<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-423280\" data-pagespeed-lazy-src=\"https:\/\/www.howtogeek.com\/wp-content\/uploads\/2019\/06\/rename_19.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"\" width=\"646\" height=\"197\" src=\"\/pagespeed_static\/1.JiBnMqyl6S.gif\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>Using\u00a0<code>ls<\/code>\u00a0a second time confirms that string.c has become bang.c and strangle.c is now bangle.c.<\/p>\n<h2 role=\"heading\" aria-level=\"2\"><span class=\"ez-toc-section\" id=\"Using_Translations_With_rename\"><\/span>Using Translations With rename<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>The <code>rename<\/code> command can perform actions on filenames called translations.\u00a0A simple example of a translation would be to force a set of filenames into uppercase.<\/p>\n<p>In the <code>rename<\/code> command below notice that we\u2019re not using an <code>s\/<\/code>\u00a0to start the central expression, we\u2019re using <code>y\/<\/code>.\u00a0This tells\u00a0<code>rename<\/code> we\u2019re not performing a substitution; we\u2019re performing a translation.<\/p>\n<p>The <code>a-z<\/code>\u00a0term is a Perl expression that means all lowercase characters in the sequence from a to z.\u00a0Similarly, the <code>A-Z<\/code>\u00a0term represents all uppercase letters in the sequence from A to Z.<\/p>\n<p>The central expression in this command could be paraphrased as \u201cif any of the lowercase letters from a to z are found in the filename, replace them with the corresponding characters from the sequence of uppercase characters from A to Z.\u201d<\/p>\n<p>To force the filenames of all \u201c.prg\u201d files to uppercase, use this command:<\/p>\n<p><span style=\"font-family: Consolas, Monaco, monospace;\">rename \u2018y\/a-z\/A-Z\/\u2019 *.prg<\/span><\/p>\n<pre>ls *.PRG<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-423258\" data-pagespeed-lazy-src=\"https:\/\/www.howtogeek.com\/wp-content\/uploads\/2019\/05\/rename_14.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"\" width=\"646\" height=\"147\" src=\"\/pagespeed_static\/1.JiBnMqyl6S.gif\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>The <code>ls<\/code> command shows us that all of the \u201c.prg\u201d filenames are now in uppercase. In fact, to be strictly accurate, they\u2019re not \u201c.prg\u201d files anymore. They\u2019re \u201c.PRG\u201d files. Linux is case sensitive.<\/p>\n<p>We can reverse that last command by reversing the position of the <code>a-z<\/code> and <code>A-Z<\/code>\u00a0terms in the central expression.<\/p>\n<p><span style=\"font-family: Consolas, Monaco, monospace;\">rename \u2018y\/A-Z\/a-z\/\u2019 *.PRG<\/span><\/p>\n<pre>ls *.prg<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-423259\" data-pagespeed-lazy-src=\"https:\/\/www.howtogeek.com\/wp-content\/uploads\/2019\/05\/rename_15.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"\" width=\"646\" height=\"147\" src=\"\/pagespeed_static\/1.JiBnMqyl6S.gif\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<h2 role=\"heading\" aria-level=\"2\"><span class=\"ez-toc-section\" id=\"You_WoDont_Learn_Perl_in_Five_Minutes\"><\/span>You (Wo|Do)n\u2019t Learn Perl in Five Minutes<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Getting to grips with Perl is time well spent. But to start using the time-saving capabilities of the <code>rename<\/code> command, you don\u2019t need to have much Perl knowledge at all to reap large benefits in power, simplicity and time.<\/p>\n<\/div>\n<p><script>\n setTimeout(function(){\n  !function(f,b,e,v,n,t,s)\n  {if(f.fbq)return;n=f.fbq=function(){n.callMethod?\n  n.callMethod.apply(n,arguments):n.queue.push(arguments)};\n  if(!f._fbq)f._fbq=n;n.push=n;n.loaded=!0;n.version='2.0';\n  n.queue=[];t=b.createElement(e);t.async=!0;\n  t.src=v;s=b.getElementsByTagName(e)[0];\n  s.parentNode.insertBefore(t,s) } (window, document,'script',\n  'https:\/\/connect.facebook.net\/en_US\/fbevents.js');\n   fbq('init', '335401813750447');\n   fbq('track', 'PageView');\n  },3000);\n<\/script><\/p>\n<blockquote><p><strong><span style=\"color: #ff6600;\">If you liked the article, do not forget to share it with your friends. Follow us on\u00a0<span style=\"color: #ff0000;\"><a style=\"color: #ff0000;\" href=\"https:\/\/news.google.com\/publications\/CAAqBwgKMLG0nwswvr63Aw\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Google News<\/a><\/span>\u00a0too, click on the star and choose us from your favorites.<\/span><\/strong><\/p><\/blockquote>\n<blockquote>\n<p style=\"text-align: center;\">For forums sites go to <span style=\"color: #ff9900;\"><a style=\"color: #ff9900;\" href=\"https:\/\/forum.buradabiliyorum.com\/\" target=\"_blank\" rel=\"noopener\">Forum.BuradaBiliyorum.Com<\/a><\/span><\/strong><\/p>\n<\/blockquote>\n<blockquote>\n<p style=\"text-align: center;\"><strong>If you want to read more like this article, you can visit our <span style=\"color: #ff9900;\"><a style=\"color: #ff9900;\" href=\"https:\/\/en.buradabiliyorum.com\/technology\/\" target=\"_blank\" rel=\"noopener\">Technology category.<\/a><\/span><\/strong><\/p>\n<\/blockquote>\n<p><span style=\"color: black;\"><a style=\"color: #ff9900;\" href=\"https:\/\/www.howtogeek.com\/423214\/how-to-use-the-rename-command-on-linux\/\" target=\"_blank\" rel=\"noopener\">Source<\/a><\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>&#8220;#How to Use the rename Command on Linux&#8221; fatmawati achmad zaenuri\/Shutterstock.com Get to grips with the file renaming powerhouse of the Linux world and give mv\u2014and yourself\u2014a rest. Rename is flexible, fast, and sometimes even easier.\u00a0 Here\u2019s a tutorial to this powerhouse of a command. What\u2019s Wrong With mv? There\u2019s nothing wrong with mv &#8230;.<\/p>\n","protected":false},"author":1,"featured_media":392444,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"fifu_image_url":"https:\/\/www.howtogeek.com\/wp-content\/uploads\/2019\/06\/Linux-laptop-showing-a-bash-prompt.jpg?height=200p&trim=2,2,2,2","fifu_image_alt":"","footnotes":""},"categories":[18],"tags":[],"class_list":["post-392443","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-technology"],"_links":{"self":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts\/392443","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/comments?post=392443"}],"version-history":[{"count":0,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts\/392443\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/media\/392444"}],"wp:attachment":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/media?parent=392443"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/categories?post=392443"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/tags?post=392443"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}