List of content you will read in this article:
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 also interrupt before overwriting and only transfer files that are newer than the destination. Through this post, you will get brief details about the Linux move file (Linux mv command)/directory using the mv command.
MV Command in Linux [Linux Move File]
To rename and switch files and folders from one place to another, use the mv Linux command. 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 following command to transfer 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 in Linux
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 Linux mv command. To pass all tif format files from the current directory to the /folder directory, for example, use the command:
mv *.tif ~/folder
Move Command in Linux Options
The mv linux command accepts some options that change the default actions of the command.
mv could be an acronym for the move 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.
How to Overwrite a File 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 move command 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.
How to Back 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 in Linux. We also saw different functionalities of the move command and how those functions can ease out our specific task. We hope that our given information has helped you to understand the concept from basics about Linux move command in Linux operating system.
People Are Also Reading: