Linux Commands – mv

Linux Commands – mv

Linux Commands – mv

Hello Everyone

Welcome to CloudAffaire and this is Debjeet.

In the last blog post, we have discussed rm command in Linux which is used to remove files and directories in Linux.

https://cloudaffaire.com/linux-commands-rm/

In this blog post, we will discuss mv command in Linux. mv stands for move and is used to move files and directories from one location to another. mv command is also used to rename files and directories.

Linux Commands – mv

You can move files and directories using mv command. You need to provide the full or relative path of the files/directories of source and destination.

You can move multiple files and directories at once using mv command. The last parameter will be treated as destination in this case. If the source files or directories have a common pattern, then you can also use regular expression with mv command.

You can use mv command to rename a file or directory provided the destination file or directory does not exist. In case of a file, if the destination is a file and it exists, mv will try to overwrite the destination file with the content of the source file. If the destination is a directory, mv command will move the source file inside the destination directory. In the case of the directory, if the destination directory exists, mv command will move the source directory inside the destination directory and if the destination directory does not exist, move command will rename the destination directory with the source directory name.

You can use mv -i option to prompt before overwrite.

You can use mv -f or –force option to supress prompt before overwriting

You can use mv -n or –no-clobber for not overwriting destination file/directory. If -i -n and -f options provided together with mv command only the final option gets effective.

You can take a backup of destination files before overwriting using mv -b option.

You can also use mv –backup=[control] option to take a backup of destination files which provide more flexibility.

You can overwrite the usual backup suffix with mv -S or –suffix=SUFFIX option.

You can use mv –strip-trailing-slashes option to remove any trailing slashes from each SOURCE argument.

You can also use mv -u or –update options to move only when the SOURCE file is newer than the destination file or when the destination file is missing.

You can get details of what mv command is doing using mv -v or –verbose options.

You can use mv -t or –target-directory=DIRECTORY to move all SOURCE arguments into DIRECTORY.

You can use mv -T or –no-target-directory treat DEST as a normal file. I am not aware of any use case of –no-target-directory. If you know, please write in the comment section.

Hope you have enjoyed this article. In the next blog post, we will discuss cp command in Linux.

 

Leave a Reply

Close Menu