Special offer
popup-offers
English
+370 5 205 5502 sales@monovm.com

How to Move a File Directory in Linux; Understanding the mv Command

The mv command transfers data or folders from one location to another using the command line. It can transfer single files, multiple files, and whole folders. It can interrupt before overwriting and only transfer files that are newer than the destination. So, in this article, we will provide brief details to move file/directory Linux using the mv command.

31 May, 21 by Antoniy Yushkevych 4 min Read

A Short Tutorial on How to Use the mv Linux Command

 

How to Use mv Command

To rename and switch files and folders from one place to another, use the mv button. The mv command has the following syntax:

mv [OPTIONS] Destination location

You must have write permissions for both source and destination to transfer a file or directory. If you do not have the write permission, you'll get a permission denied error.

Use the command to transfer the file file2 from the current working directory to the /tmp2 directory, for example:

mv file2 /tmp2

You must state the destination file name when renaming a file:

mv des1 des2

When transferring directories, the syntax is the same as when transferring files. If the dir3 directory continues to exist, the command will pass dir1 into it in the present case. If dir3 doesn't exist, dir1 will be renamed to dir3:

mv dir1 dir3

 

How to Move Multiple Files and Directories 

Specify the files you choose to transfer as the root to move several files and folders. To copy the files file2 and file3 to the dir2 directory, for example, type:

mv file2 file3 dir2

You can also use regular expressions with the mv button. To pass all tif files from the current directory to the /folder directory, for example, use the command:

mv *.tif ~/folder

 

mv Commands Options

The mv command accepts some options that change the default actions of the command.

mv could be an acronym for the mv command with a custom collection of options in some Linux distributions. In CentOS, for example, mv is an alias for mv -i. We can use the sort command to see if mv is an alias:

mv

If mv is set to alias, the performance will look like this:

mv is aliased to `mv -i'

If the destination file still exists, it will be overwritten by default. Using the -i choice to ask for confirmation:

mv -i file1 /tmp1

mv: overwrite '/tmp1/file1'?

To overwrite the file, type y or Y.

Overwriting by Force

When you want to overwrite a read-only file, the mv command will ask if you want to overwrite it:

mv -i file1 /tmp1

mv: replace '/tmp1/file1', overriding mode 0400 (r--------)? 

Using the -f options to stop being prompted:

mv -f file1 /tmp1

When you need to overwrite several read-only files, this choice comes in handy. Remember you should not overwrite present files. The -n alternative tells mv never to overwrite a file that already exists:

mv -f file1 /tmp1

If a file1 already exists, the command above would not affect. If the file doesn't exist, the file will be sent to the /tmp1 directory.

Backing Up the Data

If the destination file already exists, the -b option may be used to build a backup:

mv -b file1 /tmp1

The backup file would be named the same as the main file but with a tilde (~). Verify that the backup was generated with this command:

ls /tmp1/file1*

Output:

/tmp1/file1 /tmp1/file1~

Verbose output

The -v option is another valuable choice. The command prints the name of each transferred file when this choice is selected:

mv -i file1 /tmp1

Output:

renamed 'file1' -> '/tmp1/file1'

 

Conclusion

Transfer and rename files and folders can be done with the help of the mv command. We also saw different functionalities of the mv command and how those functions can ease out our specific task. We hope that our given information has helped you move your files using the mv command in Linux OS. 

Antoniy Yushkevych

Master of word when it comes to technology, internet and privacy. I'm also your usual guy that always aims for the best result and takes a skateboard to work. If you need me, you will find me at the office's Counter-Strike championships on Fridays or at a.yushkevych@monovm.com