Gice

Technology and General Blog

The mv command is one of the standard Linux commands that is utilised to move files and directories from one particular spot to one more. It is also used to rename files and directories. The mv command is by default obtainable on all Linux distributions.

In this article, we will clearly show you the practical illustrations of the mv command in Linux. We will also show the command line solutions used with it.

mv Command Examples

The illustrations of the Linux mv command are as follows:

Illustration 1: Shift Single File or Listing from One Directory to Yet another
To shift a one file or listing from one particular place to another, you will need to inform mv where by the file is and exactly where to transfer it. Note that when you move a file to another directory where a further file with the same identify previously exists, then it will overwrite the current file.

To transfer a one file to yet another listing, use the pursuing syntax:

For occasion, to go a file named sample1.txt from the current listing to ~/Document listing, the command would be:

$ mv sample1.txt ~/Documents/

In the same way, to transfer a listing from one particular location to another, use the following syntax:

$ mv listing1 listing2

For instance, to shift a listing named testdir from the present listing to the ~/Documents directory, the command would be:

$ mv testdir/ ~/Paperwork/

Instance 2: Move Multiple File or Directories from A single Directory to A different
To go several information from a single directory to a different, use the next syntax:

$ mv file1 file2 file3 dir1

For instance, to go the documents named sample1.txt, sample2.txt, and sample3.txt from the latest directory to the ~/Document directory, the command would be:

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

Similarly, to transfer numerous directories from one particular area to a different, use the subsequent syntax:

$ mv directory1 listing2 dircetory3 place_directory

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

$ mv testdir1 testdir2 testdir3 ~/Documents/

Illustration 3: Rename File and Directory
With the mv command, you can also rename a file or directory. To rename a file, use the adhering to 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 currently exists, it will be overwritten by the file sample1.txt.

To rename a directory, use the adhering to syntax:

$ mv dircetory1 directory2

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

Case in point 4: Prompt Ahead of Overwriting an Present File
When you move a file to a different directory the place a different file with the very same name now exists, then by default it overwrites the current file at the location listing. If you want, you can convey to the mv command to check with just before overwriting the current file using the mv command -i possibility.

For occasion, you want to shift the sample.txt file to ~/Paperwork listing which previously includes a file named sample.txt. The -i choice will prompt you ahead of overwriting the file.

$ mv -i sample.txt ~/Documents/

If you want to overwrite the file, hit y, in any other case, it will be canceled.

Instance 5: Do Not Overwrite an Existing File
If you want, you can inform the mv command to under no circumstances overwrite an existing file at the place working with the -n possibility as follows:

For occasion, you want to transfer the sample.txt file to ~/Files listing which currently incorporates a file named sample.txt. If you use the -n possibility, it will avert the file from remaining overwritten.

$ mv -n sample.txt ~/Documents/

Instance 6: Transfer Only If Resource File Is More recent Than Vacation spot
When going a file to an additional listing that now includes the exact same file, you can inform the mv command to update the file at the desired destination only if the resource file is newer than the file at the destination.

For instance, we have a sample.txt file that exists in the two the recent directory and the ~/Paperwork directory. The sample.txt file current in the latest directory is newer than the sample.txt file existing in the ~/Documents listing as can be noticed in the screenshot beneath.

Now if we use the mv command -u possibility, the file at the desired destination will be up to date as the resource file is a lot more the latest.

$ mv -u sample.txt ~/Documents/

Example 7: Build a Backup of Present Desired destination File
To steer clear of the now present place file staying overwritten, you can also produce its backup at the vacation spot directory making use of the mv command -b possibility:

For occasion, we have a sample.txt file that exists in each the current directory and the ~/Paperwork listing. In advance of the sample.txt file at the destination listing receives overwritten by the supply file, you can make its backup working with the -b choice as follows:

$ mv -b sample.txt ~/Documents/

It will generate the backup file at the location listing with the similar title but with a tilde (~) appended to it.

Summary

The Linux mv command is a aspect of the GNU Core Utilities. It permits you to move or rename the files and directories in Linux. In this article, we coated how to use the mv command along with some illustrations. To watch extra data, watch the mv command guy site.

Leave a Reply

Your email address will not be published. Required fields are marked *