{"id":92,"date":"2023-12-28T08:24:48","date_gmt":"2023-12-28T08:24:48","guid":{"rendered":"https:\/\/buyandhost.in\/blog\/?p=92"},"modified":"2023-12-28T08:56:02","modified_gmt":"2023-12-28T08:56:02","slug":"9-useful-mv-command-in-linux","status":"publish","type":"post","link":"https:\/\/buyandhost.in\/blog\/9-useful-mv-command-in-linux\/","title":{"rendered":"9 Useful \u201cmv\u201d Command in Linux"},"content":{"rendered":"<p>Files and directories are the building blocks of the operating system. As regular users, we interact with the files and directories on a daily basis. Often times we\u00a0rename or move files\u00a0from one location to another for better organization. Definitely, we can perform this operation using the Graphical User Interface (GUI). However, most <a href=\"https:\/\/en.wikipedia.org\/wiki\/Linux\" target=\"_blank\" rel=\"noopener\">Linux <\/a>users prefer to use the\u00a0<strong>mv command<\/strong>\u00a0due to its rich functionality.<\/p>\n<p>In this easy-to-understand guide, we will learn the basics of the\u00a0<strong>mv command<\/strong>. As the name suggests, the\u00a0<strong>mv command<\/strong>\u00a0is used to rename or move files and directories.<\/p>\n<p>In this guide, we will learn about the\u00a0<strong>mv command<\/strong>\u00a0using practical examples. Beginners can use these examples on a day-to-day basis while working with Linux systems.<\/p>\n<p>So let\u2019s get started.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-100 size-medium aligncenter\" src=\"http:\/\/buyandhost.in\/blog\/wp-content\/uploads\/2023\/12\/mv-Command-in-Linux-with-Examples-300x225.png\" alt=\"mv command\" width=\"300\" height=\"225\" srcset=\"https:\/\/buyandhost.in\/blog\/wp-content\/uploads\/2023\/12\/mv-Command-in-Linux-with-Examples-300x225.png 300w, https:\/\/buyandhost.in\/blog\/wp-content\/uploads\/2023\/12\/mv-Command-in-Linux-with-Examples-768x576.png 768w, https:\/\/buyandhost.in\/blog\/wp-content\/uploads\/2023\/12\/mv-Command-in-Linux-with-Examples.png 1024w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/p>\n<p>[ez-toc]<\/p>\n<h3><strong>mv Command Syntax<\/strong><\/h3>\n<p>The syntax of the\u00a0<strong>mv command<\/strong>\u00a0is similar to other Linux commands. At a high level, it is divided into two parts \u2013\u00a0<strong>options<\/strong>\u00a0and\u00a0<strong>arguments<\/strong>:<\/p>\n<pre><strong>mv [OPTIONS] &lt;SOURCE&gt; &lt;DEST&gt;<\/strong><\/pre>\n<pre><strong>mv [OPTIONS] &lt;SOURCE-1&gt; &lt;SOURCE-2&gt; ... &lt;DIRECTORY&gt;<\/strong><\/pre>\n<p>In the above syntax, the square brackets\u00a0<code>([])<\/code>\u00a0represent the optional arguments whereas angular brackets\u00a0<code>(&lt;&gt;)<\/code>\u00a0represent the mandatory arguments.<\/p>\n<h3>1. How to Rename a File in Linux<\/h3>\n<p>The very basic use of the\u00a0<strong>mv command<\/strong>\u00a0is to rename a file. So let\u2019s see how to rename a file from the current directory.<\/p>\n<p>First, create a sample file using the\u00a0touch command:<\/p>\n<pre><strong>touch file-1.txt<\/strong><\/pre>\n<p>Now, let\u2019s rename the file using the\u00a0<strong>mv command<\/strong>\u00a0as follows:<\/p>\n<pre><strong>mv file-1.txt file-2.txt<\/strong><\/pre>\n<p>Finally, verify that the file has been renamed successfully using the\u00a0<a title=\"ls Command Examples in Linux\" href=\"https:\/\/buyandhost.in\/\" target=\"_blank\" rel=\"noopener\">ls command<\/a>:<\/p>\n<pre><strong>ls -1<\/strong><\/pre>\n<h3>2. Enable Verbose Mode in Mv Command<\/h3>\n<p>Sometimes, we want to know which files or directories are getting renamed. In such cases, we can use\u00a0<code>-v<\/code>\u00a0option to enable the verbose mode.<\/p>\n<p>To understand this, let\u2019s rename the file using the verbose mode:<\/p>\n<pre><strong>mv -v file-2.txt file-1.txt<\/strong><\/pre>\n<pre><strong>renamed 'file-2.txt' -&gt; 'file-1.txt'<\/strong><\/pre>\n<p>In the above output, we can see that, now the mv command shows the rename message.<\/p>\n<h3><span id=\"3_How_to_Rename_a_Directory_in_Linux\" class=\"ez-toc-section\"><\/span><strong>3. How to Rename a Directory in Linux<\/strong><\/h3>\n<p>Similar to files, we can use the\u00a0<strong>mv command<\/strong>\u00a0to rename the directory. To understand more clearly, first you need to\u00a0create a new directory\u00a0with the name\u00a0<strong>src<\/strong>:<\/p>\n<pre><strong>mkdir src<\/strong><\/pre>\n<p>Now, let\u2019s rename the directory using the following command:<\/p>\n<pre><strong>mv -v src dst<\/strong><\/pre>\n<pre><strong>renamed 'src' -&gt; 'dst'<\/strong><\/pre>\n<p>&nbsp;<\/p>\n<h3><strong>4. How to Move Multiple Files to the Directory<\/strong><\/h3>\n<p>Many times, we move files to a single directory for better organization. For example, it is very common practice to keep all audio files in a single directory.<\/p>\n<p>Certainly, we can use the\u00a0<strong>mv command<\/strong>\u00a0multiple times to achieve this. However, the process quickly becomes time-consuming as files increase in number. However, to make it time-efficient, we can use the alternative syntax of the\u00a0<strong>mv command<\/strong>.<\/p>\n<p>Let\u2019s understand by creating a few files and a new directory:<\/p>\n<pre><strong>touch 1.mp3 2.txt 3.dat<\/strong><\/pre>\n<pre><strong>mkdir misc<\/strong><\/pre>\n<p>Now, let\u2019s move all these files to the\u00a0<strong>misc<\/strong>\u00a0directory using the following command:<\/p>\n<pre><strong>mv -v 1.mp3 2.txt 3.dat misc<\/strong><\/pre>\n<pre><strong>renamed '1.mp3' -&gt; 'misc\/1.mp3'\r\nrenamed '2.txt' -&gt; 'misc\/2.txt'\r\nrenamed '3.dat' -&gt; 'misc\/3.dat'<\/strong><\/pre>\n<p>&nbsp;<\/p>\n<p>It is important to note that, to use this alternative syntax the directory must be present already and it must be the last argument of the command.<\/p>\n<h3><span id=\"5_How_to_Move_Multiple_Directories_in_Linux\" class=\"ez-toc-section\"><\/span><strong>5. How to Move Multiple Directories in Linux<\/strong><\/h3>\n<p data-inc=\"2\">Just like the files, we can use the\u00a0<strong>mv command<\/strong>\u00a0to move multiple directories at once. Let\u2019s understand this with a simple example.<\/p>\n<p>First, create a few directories using the\u00a0mkdir command:<\/p>\n<pre><strong>mkdir dir-1 dir-2 dir-3 dir-4<\/strong><\/pre>\n<p>Now, let\u2019s move all these directories to the\u00a0<strong>dir-4<\/strong>\u00a0directory:<\/p>\n<pre><strong>mv -v dir-1 dir-2 dir-3 dir-4<\/strong><\/pre>\n<p>In the above output, we can see that we were able to move all directories.<\/p>\n<h3><span id=\"6_How_to_Avoid_Overwriting_Files_in_Linux\" class=\"ez-toc-section\"><\/span><strong>6. How to Avoid Overwriting Files in Linux<\/strong><\/h3>\n<p>By default, the\u00a0<strong>mv command<\/strong>\u00a0overwrites the destination file. Sometimes, we want to disable this default behavior to avoid data loss. In such cases, we can use the\u00a0<code>-n<\/code>\u00a0option.<\/p>\n<p>To understand this, first, create a sample file:<\/p>\n<pre><strong>touch file-2.txt<\/strong><\/pre>\n<p>Now, let\u2019s try to overwrite it using the following command:<\/p>\n<pre><strong>mv -v -n file-1.txt file-2.txt<\/strong><\/pre>\n<p data-inc=\"3\">Here, we can see that verbose mode didn\u2019t show any message. This indicates that\u00a0<strong>file-2.txt<\/strong>\u00a0hasn\u2019t been overwritten.<\/p>\n<h3><span id=\"7_How_to_Overwrite_Files_Interactively_in_Linux\" class=\"ez-toc-section\"><\/span><strong>7. How to Overwrite Files Interactively in Linux<\/strong><\/h3>\n<p>In the previous example, we saw how to disable file overwriting. However, sometimes we want to overwrite files in a safe manner.<\/p>\n<p>In such cases, we can use the\u00a0<strong>mv command<\/strong>\u00a0in an interactive mode. In this mode, the\u00a0<strong>mv command<\/strong>\u00a0shows the warning message and waits for the user\u2019s confirmation before overwriting the file.<\/p>\n<p>Now, let\u2019s try to overwrite the\u00a0<strong>file-2.txt<\/strong>\u00a0file in an interactive mode:<\/p>\n<pre><strong>mv -v -i file-1.txt file-2.txt\r\n\r\nmv: overwrite \u2018file-2.txt\u2019?<\/strong><\/pre>\n<p>In the above output, we can see that the command is waiting for the user\u2019s confirmation. Just like other Linux commands, we can use\u00a0<code>'y'<\/code>\u00a0to continue or\u00a0<code>'n'<\/code>\u00a0to abort the operation.<\/p>\n<h3><span id=\"8_Overwrite_File_Only_When_the_Source_Is_Newer\" class=\"ez-toc-section\"><\/span><strong>8. Overwrite File Only When the Source Is Newer<\/strong><\/h3>\n<p>In the previous example, we saw how to overwrite files using an interactive mode, but this method is not practical when we want to overwrite a large number of files.<\/p>\n<p>However, we should not perform the overwrite operation without verification, because a user might overwrite the newer file accidentally. In such cases, we can use the\u00a0<code>-u<\/code>\u00a0option to perform the move operation only if the source is newer than the destination.<\/p>\n<p>To understand this, let\u2019s update the timestamp of the source file:<\/p>\n<pre><strong>touch -t 201912301000 file-1.txt<\/strong><\/pre>\n<pre><strong>ls -l file-1.txt<\/strong><\/pre>\n<p>In the above example, we have used the\u00a0<code>-t<\/code>\u00a0option of the\u00a0touch command\u00a0to set the older timestamp on the\u00a0<strong>file-1.txt<\/strong>\u00a0file.<\/p>\n<p>Next, let\u2019s update the timestamp of the destination file to the current time:<\/p>\n<pre><strong>touch file-2.txt<\/strong><\/pre>\n<p>Finally, let\u2019s try to perform overwrite the destination file using the\u00a0<code>-u<\/code>\u00a0option:<\/p>\n<pre><strong>mv -v -u file-1.txt file-2.txt<\/strong><\/pre>\n<p>In the above output, we can see that verbose mode didn\u2019t show any message. This indicates that\u00a0<strong>file-2.txt<\/strong>\u00a0hasn\u2019t been overwritten.<\/p>\n<h3><strong>9. How to Create a Backup Before Overwriting Files<\/strong><\/h3>\n<p>In the previous few examples, we saw how to overwrite the destination file in a safer way. The mv command provides one more option, which allows us to specify a backup policy using the\u00a0<code>--backup<\/code>\u00a0option, which takes a backup of the destination file before overwriting it.<\/p>\n<pre><strong>touch file-1.txt file-2.txt<\/strong><\/pre>\n<pre><strong>mv --backup=numbered -v file-1.txt file-2.txt<\/strong><\/pre>\n<p>Here, we have used the numbered backup policy, which uses incremental numbers in the backup file names.<\/p>\n<p>To understand this, let\u2019s execute these two commands a few more times and check the output:<\/p>\n<pre><strong>touch file-1.txt file-2.txt\r\nmv --backup=numbered -v file-1.txt file-2.txt\r\n\r\ntouch file-1.txt file-2.txt\r\nmv --backup=numbered -v file-1.txt file-2.txt\r\n\r\ntouch file-1.txt file-2.txt\r\nmv --backup=numbered -v file-1.txt file-2.txt<\/strong><\/pre>\n<p>&nbsp;<\/p>\n<p>In this beginner\u2019s guide, we discussed how to rename and move files as well as directories using the\u00a0<strong>mv command<\/strong>. Linux newbies can refer to these examples in day-to-day life while working with Linux systems.<\/p>\n<div class=\"in-content-related-posts\"><\/div>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Files and directories are the building blocks of the operating system. As regular users, we interact with the files and directories on a daily basis. Often times we\u00a0rename or move files\u00a0from one location to another for better organization. Definitely, we can perform this operation using the Graphical User Interface (GUI). However, most Linux users prefer [&hellip;]<\/p>\n","protected":false},"author":3,"featured_media":103,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-92","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-linux"],"_links":{"self":[{"href":"https:\/\/buyandhost.in\/blog\/wp-json\/wp\/v2\/posts\/92","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/buyandhost.in\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/buyandhost.in\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/buyandhost.in\/blog\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/buyandhost.in\/blog\/wp-json\/wp\/v2\/comments?post=92"}],"version-history":[{"count":13,"href":"https:\/\/buyandhost.in\/blog\/wp-json\/wp\/v2\/posts\/92\/revisions"}],"predecessor-version":[{"id":111,"href":"https:\/\/buyandhost.in\/blog\/wp-json\/wp\/v2\/posts\/92\/revisions\/111"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/buyandhost.in\/blog\/wp-json\/wp\/v2\/media\/103"}],"wp:attachment":[{"href":"https:\/\/buyandhost.in\/blog\/wp-json\/wp\/v2\/media?parent=92"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/buyandhost.in\/blog\/wp-json\/wp\/v2\/categories?post=92"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/buyandhost.in\/blog\/wp-json\/wp\/v2\/tags?post=92"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}