{"id":8174,"date":"2021-07-10T12:46:07","date_gmt":"2021-07-10T12:46:07","guid":{"rendered":"https:\/\/linuxways.net\/?p=8174"},"modified":"2021-07-10T12:46:07","modified_gmt":"2021-07-10T12:46:07","slug":"how-to-rename-files-and-directories-in-ubuntu-20-04","status":"publish","type":"post","link":"https:\/\/linuxways.net\/de\/ubuntu\/how-to-rename-files-and-directories-in-ubuntu-20-04\/","title":{"rendered":"How to Rename Files and Directories in Ubuntu 20.04"},"content":{"rendered":"<p>Anyone who\u2019s worked with Ubuntu systems knows how important it is to keep your directories clean and structured, for effective and efficient access to them. Sometimes, you may need to create temp directories that might need renaming later, or you might want to rename a directory once you are done with a project.<\/p>\n<p>Contrary to popular belief, there are multiple ways to rename a directory in Ubuntu 20.04. In this post, we\u2019ll explore multiple ways, you can use to rename directories in Ubuntu. Let\u2019s begin:<\/p>\n<h2>Through Nautilus File Explorer<\/h2>\n<p>Similar to its mainstream cousin, Ubuntu now features a very user-friendly GUI, which has made it relatively easier to use compared to its earlier iteration. Like Windows, you get a file explorer in Ubuntu as well. Using it you can navigate to your desired directory, right-click on it, click on \u201cRename\u201d and enter your desired name for the directory.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"327\" height=\"183\" class=\"wp-image-8175\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/07\/word-image-296.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/07\/word-image-296.png 327w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/07\/word-image-296-300x168.png 300w\" sizes=\"auto, (max-width: 327px) 100vw, 327px\" \/><\/p>\n<h2>Renaming multiple folders through file explorer<\/h2>\n<p>If you require to rename multiple folders into a sequence, the file explorer in Ubuntu can help you greatly.<\/p>\n<p>Start by selecting all of the folders. Right-click and click on \u201cRename\u201d.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"433\" height=\"198\" class=\"wp-image-8176\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/07\/word-image-297.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/07\/word-image-297.png 433w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/07\/word-image-297-300x137.png 300w\" sizes=\"auto, (max-width: 433px) 100vw, 433px\" \/><\/p>\n<p>Type the consistent part of the name in the textbox.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"615\" height=\"531\" class=\"wp-image-8177\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/07\/word-image-298.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/07\/word-image-298.png 615w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/07\/word-image-298-300x259.png 300w\" sizes=\"auto, (max-width: 615px) 100vw, 615px\" \/><\/p>\n<p>Click on the \u201cAdd\u201d button in front of the textbox, to select your desired sequence.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"613\" height=\"364\" class=\"wp-image-8178\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/07\/word-image-299.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/07\/word-image-299.png 613w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/07\/word-image-299-300x178.png 300w\" sizes=\"auto, (max-width: 613px) 100vw, 613px\" \/><\/p>\n<p>Once you apply the sequence, you can sort the name by ascending and descending order.<\/p>\n<p>Click on rename in the upper right corner of the dialog box to apply the changes.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"641\" height=\"277\" class=\"wp-image-8179\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/07\/word-image-300.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/07\/word-image-300.png 641w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/07\/word-image-300-300x130.png 300w\" sizes=\"auto, (max-width: 641px) 100vw, 641px\" \/><\/p>\n<h2>Renaming using the \u201cmv\u201d command<\/h2>\n<p>There is no built-in rename command in Ubuntu.<\/p>\n<p>You can also use the \u201cmv\u201d command in the terminal to rename a file or directory. Open up the terminal by using the shortcut \u201cAlt + Ctrl + T\u201d. Once you have the terminal open, type the following command:<\/p>\n<pre><strong><em>$ mv &lt;original_name&gt; &lt;new_name&gt;<\/em><\/strong><\/pre>\n<p>For instance, if you want to rename a directory named \u201cTemp\u201d, located in your home directory, to \u201cDirectory\u201d in the same location, you can use the following command:<\/p>\n<p>Start by using the \u201cls\u201d command to view the current contents of your current location in the terminal.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"482\" height=\"103\" class=\"wp-image-8180\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/07\/word-image-301.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/07\/word-image-301.png 482w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/07\/word-image-301-300x64.png 300w\" sizes=\"auto, (max-width: 482px) 100vw, 482px\" \/><\/p>\n<pre>Type: <strong><em>$ mv Temp Directory<\/em><\/strong><\/pre>\n<p>Once you press enter, you won\u2019t get any prompt.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"472\" height=\"146\" class=\"wp-image-8181\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/07\/word-image-302.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/07\/word-image-302.png 472w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/07\/word-image-302-300x93.png 300w\" sizes=\"auto, (max-width: 472px) 100vw, 472px\" \/><\/p>\n<p>Now type \u201cls\u201d again to view the changes.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"479\" height=\"206\" class=\"wp-image-8182\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/07\/word-image-303.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/07\/word-image-303.png 479w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/07\/word-image-303-300x129.png 300w\" sizes=\"auto, (max-width: 479px) 100vw, 479px\" \/><\/p>\n<h2>Renaming using the \u201crename\u201d command<\/h2>\n<p>As discussed, the rename command isn\u2019t built into Ubuntu. Just a heads up, it\u2019s more advanced than the \u201cmv\u201d command as it requires you to have at least a basic understanding of regular expressions.<\/p>\n<p>You can install the command by typing the following command into your terminal:<\/p>\n<pre><strong><em>$ sudo apt install rename<\/em><\/strong><\/pre>\n<p>To use the command, you need to understand the syntax below:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"554\" height=\"86\" class=\"wp-image-8183\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/07\/word-image-304.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/07\/word-image-304.png 554w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/07\/word-image-304-300x47.png 300w\" sizes=\"auto, (max-width: 554px) 100vw, 554px\" \/><\/p>\n<p>Using the above syntax, you can rename single or multiple files to the result specified as the first argument.<\/p>\n<p>The Perl argument \u201cperlexpr\u201d is expected to modify the $_string part using Perl for at least some of the names specified. If a name is not updated using this command, running this command again will not update it. If you don\u2019t specify any file or directory names in this command, it will ask you for the names through regular input on the terminal.<\/p>\n<p>Let\u2019s explore the main options specified in the syntax.<\/p>\n<h3>Options<\/h3>\n<ul>\n<li>-V: used to show the version number.<\/li>\n<li>-h: print OPTIONS and SYNOPSIS.<\/li>\n<li>-f: forcing existing files to be overwritten.<\/li>\n<li>-n: (no action) prints the names of the files to be renamed but doesn\u2019t rename.<\/li>\n<li>-e: code for acting on file names. It can be repeated to build up code like in perl, but if there is no -e, the first argument in the command will be used as code.<\/li>\n<li>-E: similar to -e but it is terminated using \u2018;\u2019.<\/li>\n<\/ul>\n<h3>Example (changing file extension)<\/h3>\n<p>Working with code or text files, you might need to change the file extension of your code file frequently. Let\u2019s consider a \u201cfile.txt\u201d containing our code, and we need to convert it to a C\/C++ file to compile it. Open the terminal where your file is located and type the following:<\/p>\n<pre><strong><em>$ rename \u2018s\/\\.txt\/\\.c\/\u2019 file.txt<\/em><\/strong><\/pre>\n<p>You can run \u201cls\u201d to see the results.<\/p>\n<h2><img loading=\"lazy\" decoding=\"async\" width=\"751\" height=\"150\" class=\"wp-image-8184\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/07\/word-image-305.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/07\/word-image-305.png 751w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/07\/word-image-305-300x60.png 300w\" sizes=\"auto, (max-width: 751px) 100vw, 751px\" \/>Renaming using a bash script<\/h2>\n<p>If you require updating multiple files, you can write a bash script to achieve your desired result.<\/p>\n<p>Continuing from our old example, if we have multiple \u201c*.txt\u201d files that need to be converted to C\/C++ \u201c.c\u201d files. You can write the following bash script to convert the whole series.<\/p>\n<p>You can create the bash script by opening up the text editor and typing in the following code:<\/p>\n<pre><strong><em>for file in *.txt; do<\/em><\/strong>\r\n\r\n<strong><em>mv -- \"${file}\" \"${file%.txt}.c\"<\/em><\/strong>\r\n\r\n<strong><em>done<\/em><\/strong><\/pre>\n<p>Now save the file in the same directory as your text files and name it anything e.g., \u201cfileRename.sh\u201d. Following that go into the terminal and run \u201cls\u201d to confirm the availability of all the files.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"741\" height=\"163\" class=\"wp-image-8185\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/07\/word-image-306.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/07\/word-image-306.png 741w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/07\/word-image-306-300x66.png 300w\" sizes=\"auto, (max-width: 741px) 100vw, 741px\" \/><\/p>\n<p>Run your bash script using the command below:<\/p>\n<pre><strong><em>$ bash fileRename.sh<\/em><\/strong><\/pre>\n<p>Now running \u201cls\u201d will show you that all the file extensions have changed.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"741\" height=\"281\" class=\"wp-image-8186\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/07\/word-image-307.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/07\/word-image-307.png 741w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/07\/word-image-307-300x114.png 300w\" sizes=\"auto, (max-width: 741px) 100vw, 741px\" \/><\/p>\n<p>Creating a bash script may look like a tedious undertaking at the start, but once you have these scripts prepared per your requirements, running them can save you a lot of time in the long term.<\/p>","protected":false},"excerpt":{"rendered":"<p>Anyone who\u2019s worked with Ubuntu systems knows how important it is to keep your directories clean and structured, for effective and efficient access to them. Sometimes, you may&hellip;<\/p>","protected":false},"author":1,"featured_media":8267,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[47,46],"class_list":["post-8174","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-ubuntu","tag-directories","tag-files"],"_links":{"self":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/posts\/8174","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/comments?post=8174"}],"version-history":[{"count":0,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/posts\/8174\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/media\/8267"}],"wp:attachment":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/media?parent=8174"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/categories?post=8174"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/tags?post=8174"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}