{"id":7420,"date":"2018-12-06T20:29:14","date_gmt":"2018-12-06T17:29:14","guid":{"rendered":"https:\/\/www.howtoforge.com\/linux-rename-command\/"},"modified":"2018-12-06T20:29:14","modified_gmt":"2018-12-06T17:29:14","slug":"linux-rename-command-tutorial-for-beginners-with-examples","status":"publish","type":"post","link":"https:\/\/afaghhosting.net\/blog\/linux-rename-command-tutorial-for-beginners-with-examples\/","title":{"rendered":"Linux rename Command Tutorial for Beginners (with Examples)"},"content":{"rendered":"<div><img decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/12\/linux-rename-command-tutorial-for-beginners-with-examples.jpg\" class=\"ff-og-image-inserted\" alt=\"\" title=\"\"><\/div>\n<p>If you work with files on the command line in Linux, renaming files is one of the most frequent tasks you may find yourself involved in. We&#8217;ve already discussed the\u00a0<a href=\"https:\/\/www.howtoforge.com\/linux-mv-command\/\" target=\"_blank\" rel=\"noopener noreferrer\">mv command<\/a> that lets you do this. And here, in this tutorial, we will discuss another such tool, dubbed rename.<\/p>\n<p>But before we start with that, it&#8217;s worth mentioning that all examples in this article have been tested on an Ubuntu 18.04 LTS machine.<\/p>\n<h2 id=\"linux-rename-command\">Linux rename command<\/h2>\n<p>As the name suggests, the <em>rename<\/em> command in Linux allows you to rename files. Following is its syntax:<\/p>\n<pre class=\"command\"><code spellcheck=\"false\">rename [options] expression replacement file...<\/code><\/pre>\n<p>And here&#8217;s how the tool&#8217;s man page explains it:<\/p>\n<pre><code spellcheck=\"false\">rename will rename the specified files by replacing the first occurrence of expression in their name by replacement.<\/code><\/pre>\n<p>Note that if you don&#8217;t have the rename command installed, you can get it using the following command:<\/p>\n<pre class=\"command\"><code spellcheck=\"false\">sudo apt install rename<\/code><\/pre>\n<p>Following are some Q&amp;A-styled examples that should give you a better idea on how the rename command works.<\/p>\n<h2 id=\"q-how-to-use-rename-command\">Q1. How to use rename command?<\/h2>\n<p>Basic usage isn&#8217;t exactly straight forward, I must say, but it&#8217;s not difficult to understand. Let&#8217;s say you have a file named &#8216;test.txt&#8217; and you want to rename it to &#8216;new.txt&#8217;. Then here&#8217;s how you use the rename command to do this.<\/p>\n<pre class=\"command\"><code spellcheck=\"false\">rename 's\/test\/new\/' test.txt<\/code><\/pre>\n<h2 id=\"q-how-rename-handles-symbolic-links\">Q2. How rename handles symbolic links?<\/h2>\n<p>By default, the rename command just renames the file which is passed as input, even if it&#8217;s a symbolic link. However, if you want the tool to not rename symbolic links, but act on their targets instead, then use the -s command line option.<\/p>\n<p>So if symlink.txt is a symbolic link that points to root.txt, then the following command will make sure the rename operation happens for root.txt:<\/p>\n<pre class=\"command\"><code spellcheck=\"false\">rename -s 's\/root\/new\/' symlink.txt<\/code><\/pre>\n<h2 id=\"q-how-to-make-a-dry-run-with-rename\">Q3. How to make a dry run with rename?<\/h2>\n<p>Suppose you only want to see the final change that&#8217;ll happen with a rename command, without actually carrying it out, then use the -n command line option. For example, the following command:<\/p>\n<pre class=\"command\"><code spellcheck=\"false\">rename -n 's\/new\/test\/' new.txt<\/code><\/pre>\n<p>produced the following output:<\/p>\n<pre><code spellcheck=\"false\">rename(new.txt, test.txt)<\/code><\/pre>\n<p>but didn&#8217;t\u00a0actually rename new.txt to test.txt.<\/p>\n<h2 id=\"q-how-rename-handles-overwriting-of-files\">Q4. How rename handles overwriting of files?<\/h2>\n<p>By default, the rename command doesn&#8217;t perform the operation if it involves overwriting an existing file. However, you can force the tool to overwrite using the -f command line option.<\/p>\n<p>For example, the following command:<\/p>\n<pre class=\"command\"><code spellcheck=\"false\">rename 's\/new\/test\/' new.txt<\/code><\/pre>\n<p>produced the following output:<\/p>\n<pre class=\"command\"><code spellcheck=\"false\">new.txt not renamed: test.txt already exists<\/code><\/pre>\n<p>But when the -f command was used:<\/p>\n<pre class=\"command\"><code spellcheck=\"false\">rename -f 's\/new\/test\/' new.txt<\/code><\/pre>\n<p>The operation completed smoothly and test.txt got overwritten.<\/p>\n<h2 id=\"conclusion\">Conclusion<\/h2>\n<p>The rename command doesn&#8217;t offer too many options, and we&#8217;ve already discussed some of the main ones here. After you&#8217;re done practicing these, head to the tool&#8217;s\u00a0<a href=\"https:\/\/linux.die.net\/man\/2\/rename\" target=\"_blank\" rel=\"noopener noreferrer\">man page<\/a> to learn more about it.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>If you work with files on the command line in Linux, renaming files is one of the most frequent tasks you may find yourself involved in. We&#8217;ve already discussed the\u00a0mv command that lets you do this. And here, in this tutorial, we will discuss another such tool, dubbed rename. But before we start with that, [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[36],"tags":[],"class_list":["post-7420","post","type-post","status-publish","format-standard","hentry","category-36"],"_links":{"self":[{"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/posts\/7420","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/comments?post=7420"}],"version-history":[{"count":0,"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/posts\/7420\/revisions"}],"wp:attachment":[{"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/media?parent=7420"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/categories?post=7420"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/tags?post=7420"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}