31.6 C
Jaipur
Wednesday, June 16, 2021

Linux “mv” Command Examples – Linux Hint

Must read

The mv command is one of the basic Linux commands that is used to move files and directories from one location to another. It is also used to rename files and directories. The mv command is by default available on all Linux distributions.

In this post, we will show you the practical examples of the mv command in Linux. We will also show the command line options used with it.

mv Command Examples

The examples of the Linux mv command are as follows:

Example 1: Move Single File or Directory from One Directory to Another
To move a single file or directory from one location to another, you will need to tell mv where the file is and where to move it. Note that when you move a file to another directory where another file with the same name already exists, then it will overwrite the existing file.

To move a single file to another directory, use the following syntax:

For instance, to move a file named sample1.txt from the current directory to ~/Document directory, the command would be:

$ mv sample1.txt ~/Documents/

Similarly, to move a directory from one location to another, use the following syntax:

$ mv directory1 directory2

For instance, to move a directory named testdir from the current directory to the ~/Documents directory, the command would be:

$ mv testdir/ ~/Documents/

Example 2: Move Multiple File or Directories from One Directory to Another
To move multiple files from one directory to another, use the following syntax:

$ mv file1 file2 file3 dir1

For instance, to move the files named sample1.txt, sample2.txt, and sample3.txt from the current directory to the ~/Document directory, the command would be:

$ mv sample1.txt sample2.txt sample3.txt ~/Documents/

Similarly, to move multiple directories from one location to another, use the following syntax:

$ mv directory1 directory2 dircetory3 destination_directory

For instance, to move the directories named testdir1, testdir2, and testdir3 from the current directory to the ~/Documents directory, the command would be:

$ mv testdir1 testdir2 testdir3 ~/Documents/

Example 3: Rename File and Directory
With the mv command, you can also rename a file or directory. To rename a file, use the following syntax:

For instance, to rename a file named sample1.txt to sample2.txt, the command would be:

$ mv sample1.txt sample2.txt

If the file sample2.txt already exists, it will be overwritten by the file sample1.txt.

To rename a directory, use the following syntax:

$ mv dircetory1 directory2

For instance, to rename a directory named testdir1/ to testdir2/, the command would be:

Example 4: Prompt Before Overwriting an Existing File
When you move a file to another directory where another file with the same name already exists, then by default it overwrites the existing file at the destination directory. If you want, you can tell the mv command to ask before overwriting the existing file using the mv command -i option.

For instance, you want to move the sample.txt file to ~/Documents directory which already contains a file named sample.txt. The -i option will prompt you before overwriting the file.

$ mv -i sample.txt ~/Documents/

If you want to overwrite the file, hit y, otherwise, it will be canceled.

Example 5: Do Not Overwrite an Existing File
If you want, you can tell the mv command to never overwrite an existing file at the destination using the -n option as follows:

For instance, you want to move the sample.txt file to ~/Documents directory which already contains a file named sample.txt. If you use the -n option, it will prevent the file from being overwritten.

$ mv -n sample.txt ~/Documents/

Example 6: Move Only If Source File Is Newer Than Destination
When moving a file to another directory that already contains the same file, you can tell the mv command to update the file at the destination only if the source file is newer than the file at the destination.

For instance, we have a sample.txt file that exists in both the current directory and the ~/Documents directory. The sample.txt file existing in the current directory is newer than the sample.txt file existing in the ~/Documents directory as can be seen in the screenshot below.

Now if we use the mv command -u option, the file at the destination will be updated as the source file is more recent.

$ mv -u sample.txt ~/Documents/

Example 7: Create a Backup of Existing Destination File
To avoid the already existing destination file being overwritten, you can also create its backup at the destination directory using the mv command -b option:

For instance, we have a sample.txt file that exists in both the current directory and the ~/Documents directory. Before the sample.txt file at the destination directory gets overwritten by the source file, you can create its backup using the -b option as follows:

$ mv -b sample.txt ~/Documents/

It will create the backup file at the destination directory with the same name but with a tilde (~) appended to it.

Conclusion

The Linux mv command is a part of the GNU Core Utilities. It allows you to move or rename the files and directories in Linux. In this post, we covered how to use the mv command along with some examples. To view more information, view the mv command man page.

Source link

- Advertisement -

More articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisement -

Latest article