{"id":21315,"date":"2023-09-27T10:40:01","date_gmt":"2023-09-27T10:40:01","guid":{"rendered":"https:\/\/linuxways.net\/?p=21315"},"modified":"2023-09-27T10:40:01","modified_gmt":"2023-09-27T10:40:01","slug":"how-to-rename-files-in-ubuntu-22-04-using-the-command-line","status":"publish","type":"post","link":"https:\/\/linuxways.net\/de\/ubuntu\/how-to-rename-files-in-ubuntu-22-04-using-the-command-line\/","title":{"rendered":"How to Rename Files In Ubuntu 22.04 Using the Command Line?"},"content":{"rendered":"<p>In Linux, the Command-Line Interface makes it easy to rename our files and is more powerful. With the CLI we can run scripts and change the file names in the future for our ease and need. In Linux\/Ubuntu, we have multiple commands which we can use to rename our files.<\/p>\n<p>This article explains different commands to rename files in Ubuntu 22.04.<\/p>\n<h2><strong>How to Rename Files in Ubuntu 22.04 Using the Command Line?<\/strong><\/h2>\n<p>Linux provides us with various commands to rename our files, we will go through each of them.<\/p>\n<h2><strong>Using the \u201cmv\u201d Command to Rename Files<\/strong><\/h2>\n<p>The \u201c<strong>mv<\/strong>\u201d command in Ubuntu 22.04 is used to move or rename files, having the syntax:<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\"><span class=\"kw2\">mv<\/span> <span class=\"br0\">&#91;<\/span>options<span class=\"br0\">&#93;<\/span> <span class=\"br0\">&#91;<\/span><span class=\"kw3\">source<\/span><span class=\"br0\">&#93;<\/span> <span class=\"br0\">&#91;<\/span>destination<span class=\"br0\">&#93;<\/span><\/div><\/div>\n<p>The \u201coptions\u201d statement works as a flag and you can specify a specific use or need. You can display the names of renamed directories with the flag \u201c-v\u201d and create a backup using the flag \u201c-b\u201d. Similarly, other flags can be used. Using the \u201cmv\u201d command, if the name of the source and destination of a file are the same, the \u201cmv\u201d command renames the source file. Consider the following example where we only have one file \u201cmyFile1.txt\u201d in the current directory:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"771\" height=\"308\" class=\"wp-image-21316\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2023\/09\/word-image-21315-1.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2023\/09\/word-image-21315-1.png 771w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/09\/word-image-21315-1-300x120.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/09\/word-image-21315-1-768x307.png 768w\" sizes=\"auto, (max-width: 771px) 100vw, 771px\" \/><\/p>\n<p>To rename this file, we can use the command:<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\"><span class=\"kw2\">mv<\/span> myFile1.txt myFile2.txt<\/div><\/div>\n<p>As the destination is the same as the source, it will rename the existing \u201cmyFile1.txt\u201d file:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"771\" height=\"319\" class=\"wp-image-21317\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2023\/09\/word-image-21315-2.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2023\/09\/word-image-21315-2.png 771w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/09\/word-image-21315-2-300x124.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/09\/word-image-21315-2-768x318.png 768w\" sizes=\"auto, (max-width: 771px) 100vw, 771px\" \/><\/p>\n<h2><strong>Bonus Tip 1: Use \u201cmv\u201d Command to Rename a Directory<\/strong><\/h2>\n<p>Using the \u201cmv\u201d command, you can also rename your directory. In the example below, we have a directory \u201cfolder\u201d:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"1564\" height=\"458\" class=\"wp-image-21318\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2023\/09\/word-image-21315-3.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2023\/09\/word-image-21315-3.png 1564w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/09\/word-image-21315-3-300x88.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/09\/word-image-21315-3-1024x300.png 1024w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/09\/word-image-21315-3-768x225.png 768w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/09\/word-image-21315-3-1536x450.png 1536w\" sizes=\"auto, (max-width: 1564px) 100vw, 1564px\" \/><\/p>\n<p>To rename this directory, use the current directory name followed by the new name of the directory with the \u201cmv\u201d command:<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\"><span class=\"kw2\">mv<\/span> folder newFolder<\/div><\/div>\n<p>As it is in the same directory, it will change the directory name:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"979\" height=\"280\" class=\"wp-image-21319\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2023\/09\/word-image-21315-4.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2023\/09\/word-image-21315-4.png 979w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/09\/word-image-21315-4-300x86.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/09\/word-image-21315-4-768x220.png 768w\" sizes=\"auto, (max-width: 979px) 100vw, 979px\" \/><\/p>\n<h3><strong>Renaming Files Using \u201crename\u201d Command<\/strong><\/h3>\n<p>Files in Ubuntu can also be renamed using the \u201c<strong>rename<\/strong>\u201d command. By default, the \u201crename\u201d command is not installed in Ubuntu, so to install \u201crename\u201d, use the following \u201csudo\u201d command:<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\"><span class=\"kw2\">sudo<\/span> apt <span class=\"kw2\">install<\/span> rename<\/div><\/div>\n<p>This will download and install the necessary \u201crename\u201d packages:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"729\" height=\"406\" class=\"wp-image-21320\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2023\/09\/word-image-21315-5.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2023\/09\/word-image-21315-5.png 729w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/09\/word-image-21315-5-300x167.png 300w\" sizes=\"auto, (max-width: 729px) 100vw, 729px\" \/><\/p>\n<p>The syntax to use the \u201crename\u201d command is:<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\">rename <span class=\"br0\">&#91;<\/span>options<span class=\"br0\">&#93;<\/span> <span class=\"st_h\">'s\/[filename element] \/ [replacement]\/'<\/span> <span class=\"br0\">&#91;<\/span>filename<span class=\"br0\">&#93;<\/span><\/div><\/div>\n<p>The \u201crename\u201d command will rename the \u201cfilename element\u201d with the \u201creplacement\u201d. The \u201cs\u201d before \u201cfilename element\u201d is used for substituting whereas \u201coptions\u201d is used as an additional argument for command execution. Multiple Files can be renamed with the \u201crename\u201d command as well.<\/p>\n<h2><strong>Renaming Multiple Files Using the \u201cRename\u201d Command<\/strong><\/h2>\n<p>In the example below, we have three files in our directory and they can be seen on the terminal as well:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"807\" height=\"267\" class=\"wp-image-21321\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2023\/09\/word-image-21315-6.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2023\/09\/word-image-21315-6.png 807w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/09\/word-image-21315-6-300x99.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/09\/word-image-21315-6-768x254.png 768w\" sizes=\"auto, (max-width: 807px) 100vw, 807px\" \/><\/p>\n<p>To change the filenames of these text files, use the command:<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\">rename <span class=\"re5\">-v<\/span> <span class=\"st_h\">'s\/file\/renamed\/'<\/span> <span class=\"sy0\">*<\/span>.txt<\/div><\/div>\n<p>The above command when executed, will rename all the files having the extension \u201c.txt\u201d:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"817\" height=\"351\" class=\"wp-image-21322\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2023\/09\/word-image-21315-7.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2023\/09\/word-image-21315-7.png 817w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/09\/word-image-21315-7-300x129.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/09\/word-image-21315-7-768x330.png 768w\" sizes=\"auto, (max-width: 817px) 100vw, 817px\" \/><\/p>\n<p>The rename command can also be used for multiple purposes apart from only renaming our files. It can <strong>change the<\/strong> <strong>file extension, convert to lower and uppercase,<\/strong> and <strong>delete<\/strong> a part of our filename.<\/p>\n<h2><strong>Bonus Tip 1 &#8211; Changing the File Extension Using \u201crename\u201d <\/strong><\/h2>\n<p>To change the file extension of our files from \u201c.txt\u201d to \u201c.pdf\u201d in the above example, use the \u201crename\u201d command as follows:<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\">rename <span class=\"re5\">-v<\/span> <span class=\"st_h\">'s\/.txt\/.pdf\/'<\/span> <span class=\"sy0\">*<\/span>.txt<\/div><\/div>\n<p>The command when executed will change the file extension of our files:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"816\" height=\"413\" class=\"wp-image-21323\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2023\/09\/word-image-21315-8.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2023\/09\/word-image-21315-8.png 816w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/09\/word-image-21315-8-300x152.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/09\/word-image-21315-8-768x389.png 768w\" sizes=\"auto, (max-width: 816px) 100vw, 816px\" \/><\/p>\n<h2><strong>Bonus Tip 2 &#8211; Converting Filename to Uppercase Using \u201crename\u201d<\/strong><\/h2>\n<p>Converting File Names to uppercase involves the same syntax as above. Using our previous example, renaming to uppercase, use the command:<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\">rename <span class=\"re5\">-v<\/span> <span class=\"st_h\">'y\/a-z\/A-Z\/'<\/span> <span class=\"sy0\">*<\/span>.pdf<\/div><\/div>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"816\" height=\"306\" class=\"wp-image-21324\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2023\/09\/word-image-21315-9.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2023\/09\/word-image-21315-9.png 816w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/09\/word-image-21315-9-300x113.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/09\/word-image-21315-9-768x288.png 768w\" sizes=\"auto, (max-width: 816px) 100vw, 816px\" \/><\/p>\n<h2><strong>Bonus Tip 3 &#8211; Converting Filename to Lowercase Using \u201crename\u201d<\/strong><\/h2>\n<p>Converting File Names to lowercase involves the same syntax as above. Using our previous example, renaming to uppercase, use the command:<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\">rename <span class=\"re5\">-v<\/span> <span class=\"st_h\">'y\/A-Z\/a-z\/'<\/span> <span class=\"sy0\">*<\/span>.PDF<\/div><\/div>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"821\" height=\"305\" class=\"wp-image-21325\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2023\/09\/word-image-21315-10.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2023\/09\/word-image-21315-10.png 821w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/09\/word-image-21315-10-300x111.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/09\/word-image-21315-10-768x285.png 768w\" sizes=\"auto, (max-width: 821px) 100vw, 821px\" \/><\/p>\n<h2><strong>Bonus Tip 4 &#8211; Delete Part of a Filename Using \u201crename\u201d<\/strong><\/h2>\n<p>The rename commands also let us delete a part of our file name. Using our previous example, to delete a part of a filename, use the command:<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\">rename <span class=\"re5\">-v<\/span> <span class=\"st_h\">'s\/re\/\/'<\/span> <span class=\"sy0\">*<\/span>.pdf<\/div><\/div>\n<p>This will remove the \u201cre\u201d part from our \u201crenamed\u201d string:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"816\" height=\"291\" class=\"wp-image-21326\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2023\/09\/word-image-21315-11.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2023\/09\/word-image-21315-11.png 816w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/09\/word-image-21315-11-300x107.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/09\/word-image-21315-11-768x274.png 768w\" sizes=\"auto, (max-width: 816px) 100vw, 816px\" \/><\/p>\n<h2><strong>Bonus Tip 5 &#8211; Rename the Directory Name Using \u201crename\u201d<\/strong><\/h2>\n<p>Directories can be renamed as well using the \u201crename\u201d command. In the example below, we have a directory \u201cmyFolder\u201d:<\/p>\n<p><strong><img loading=\"lazy\" decoding=\"async\" width=\"1560\" height=\"470\" class=\"wp-image-21327\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2023\/09\/word-image-21315-12.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2023\/09\/word-image-21315-12.png 1560w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/09\/word-image-21315-12-300x90.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/09\/word-image-21315-12-1024x309.png 1024w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/09\/word-image-21315-12-768x231.png 768w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/09\/word-image-21315-12-1536x463.png 1536w\" sizes=\"auto, (max-width: 1560px) 100vw, 1560px\" \/><\/strong><\/p>\n<p>The syntax to change a directory name using the \u201crename\u201d command is:<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\">rename <span class=\"st_h\">'s\/^&lt;old-name&gt;\/&lt;new-name&gt;\/'<\/span> <span class=\"sy0\">*<\/span><\/div><\/div>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"994\" height=\"301\" class=\"wp-image-21328\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2023\/09\/word-image-21315-13.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2023\/09\/word-image-21315-13.png 994w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/09\/word-image-21315-13-300x91.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/09\/word-image-21315-13-768x233.png 768w\" sizes=\"auto, (max-width: 994px) 100vw, 994px\" \/><\/p>\n<p>It will change the directory name:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"994\" height=\"301\" class=\"wp-image-21329\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2023\/09\/word-image-21315-14.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2023\/09\/word-image-21315-14.png 994w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/09\/word-image-21315-14-300x91.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/09\/word-image-21315-14-768x233.png 768w\" sizes=\"auto, (max-width: 994px) 100vw, 994px\" \/><\/p>\n<p>That\u2019s all about renaming files in Ubuntu 22.04 using the command line.<\/p>\n<h2><strong>Conclusion<\/strong><\/h2>\n<p>The \u201c<strong>rename<\/strong>\u201d and \u201c<strong>mv<\/strong>\u201d commands are quite useful for renaming our files in Ubuntu using Command Line. Using the rename command we can also delete part of the file name, convert filenames into uppercase or lowercase, change the file extensions, and much more. This article has explained how we can use both commands in Ubuntu to change the filenames of already existing files in our directory.<\/p>","protected":false},"excerpt":{"rendered":"<p>The \u201crename\u201d and \u201cmv\u201d commands can rename our files, delete part of the name, convert filenames into uppercase or lowercase, and change the file extensions. <\/p>","protected":false},"author":110,"featured_media":21330,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-21315","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-ubuntu"],"_links":{"self":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/posts\/21315","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\/110"}],"replies":[{"embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/comments?post=21315"}],"version-history":[{"count":0,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/posts\/21315\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/media\/21330"}],"wp:attachment":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/media?parent=21315"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/categories?post=21315"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/tags?post=21315"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}