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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
######################### ## Linux Commands | mv ## ######################### ## Prerequisites: One Unix/Linux/POSIX-compliant operating system with bash shell ##--- ## mv ##--- ## mv [option]... [-T] source destination ## mv [option]... source... directory ## mv [option]... -t directory source... mkdir mydir{1..4} ## create four file and directory touch myfile{1..4} ls ## returns mydir1 mydir2 mydir3 mydir4 myfile1 myfile2 myfile3 myfile4 mv myfile1 mydir1 ## moves myfile1 inside mydir1 ls mydir1 ## returns myfile1 mv mydir3 mydir4 ## moves mydir3 inside mydir4 ls mydir4 ## returns mydir3 mv mydir1/myfile1 mydir4/mydir3 ## moves myfile1 from mydir1 to mydir3 ls mydir4/mydir3 ## returns myfile1 rm -r my* |
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.
1 2 3 4 5 6 7 8 |
mkdir mydir{1..3} ## create some files and directories touch myfile{1..3} mkdir ourdir ls ## returns mydir1 mydir2 mydir3 ourdir myfile1 myfile2 myfile3 mv mydir1 mydir2 mydir3 ourdir ## move multiple directories at once, mv mydir* ourdir will also work mv myfile* ourdir ## uses regular expression * ls ourdir ## returns mydir1 mydir2 mydir3 myfile1 myfile2 myfile3 rm -r ourdir |
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.
1 2 3 4 5 6 |
mkdir mydir && touch myfile ## create one file and directory ls ## returns mydir myfile mv mydir mynewdir ## renames mydir to mynewdir mv myfile mynewfile ## renames myfile to mynewfile ls ## returns mynewdir mynewfile rm -r my* |
You can use mv -i option to prompt before overwrite.
1 2 3 4 5 6 7 8 9 10 11 |
mkdir -p mydir1/mydir2 mydir2 ## create some files and directories echo "one" > myfile1 echo "two" > mydir1/myfile1 echo "three" > mydir2/myfile2 mv -i myfile1 mydir1/ ## move myfile1 inside mydir1 (a file named myfile1 already exist inside mydir1) ## mv: overwrite ‘mydir1/myfile1’? y cat mydir1/myfile1 ## returns one (overwrites mydir1/myfile1 with myfile1) mv -i mydir2 mydir1/ ## move mydir2 inside mydir1 (a directory named mydir2 already exist inside mydir1) ## mv: overwrite ‘mydir1/mydir2’? y ls mydir1/mydir2 ## returns myfile2 (overwrites mydir1/mydir2 with mydir2) rm -r my* |
You can use mv -f or –force option to supress prompt before overwriting
1 2 3 4 5 6 7 8 |
mkdir mydir ## create some files and directory echo "one" > myfile echo "two" > mydir/myfile chmod 444 mydir/myfile ## make the file readonly mv myfile mydir/myfile ## mv: try to overwrite ‘mydir/myfile’, overriding mode 0444 (r--r--r--)? n mv -f myfile mydir/myfile ## no prompt this time rm -rf my* |
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.
1 2 3 4 5 6 |
mkdir mydir1 mydir2 ## create some files and directory echo "one" > mydir1/myfile1 echo "two" > mydir2/myfile1 mv -n mydir1/myfile1 mydir2 ## overwriting not allowed, does nothing cat mydir2/myfile1 ## returns two rm -r my* |
You can take a backup of destination files before overwriting using mv -b option.
1 2 3 4 5 6 |
mkdir mydir1 mydir2 ## create some files and directory echo "one" > mydir1/myfile1 echo "two" > mydir2/myfile1 mv -b mydir1/myfile1 mydir2 ## creates a backup (myfile1~) of myfile1 before overwriting cat mydir2/myfile1~ ## returns one cat mydir2/myfile1 ## returns two |
You can also use mv –backup=[control] option to take a backup of destination files which provide more flexibility.
1 2 3 4 5 6 7 8 9 10 |
mkdir mydir1 ## create some files and directory echo "one" > mydir1/myfile1 echo "two" > myfile1 mv --backup=t myfile1 mydir1 ## creates a backup (myfile1.~1~) of myfile1 before overwriting echo "three" > myfile1 mv --backup=t myfile1 mydir1 ## creates a backup (myfile1.~1~) of myfile1 before overwriting cat mydir1/myfile1 ## returns three cat mydir1/myfile1.~1~ ## returns one cat mydir1/myfile1.~2~ ## returns two rm -r my* |
You can overwrite the usual backup suffix with mv -S or –suffix=SUFFIX option.
1 2 3 4 5 6 7 |
mkdir mydir1 ## create some files and directory echo "one" > mydir1/myfile1 echo "two" > myfile1 mv --suffix=.bak myfile1 mydir1 ## creates a backup (myfile1.bak) of myfile1 before overwriting cat mydir1/myfile1 ## returns two cat mydir1/myfile1.bak ## returns one rm -r my* |
You can use mv –strip-trailing-slashes option to remove any trailing slashes from each SOURCE argument.
1 2 3 4 5 6 7 |
mkdir mydir{1,2} ## create some files and directory echo "one" > mydir1/myfile1 mv --strip-trailing-slashes mydir1/ mydir2/ ## moves the entire mydir1 along with myfile1 inside mydir2 ## without the option, would have moved only mydir1 contents (myfile1) inside mydir2 ## mv mydir1/ mydir2/ rm -r my* |
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.
1 2 3 4 5 6 7 8 |
mkdir mydir{1,2} ## create some files and directories echo "one" > mydir1/myfile1 cp mydir1/myfile1 mydir2/ mv -u mydir1/myfile1 mydir2/myfile1 ## myfile1 will not be moved as both are same ls -R . mv mydir1/myfile1 mydir2/myfile1 ## myfile1 will be moved from mydir1 to mydir2 ls -R . rm -r my* |
You can get details of what mv command is doing using mv -v or –verbose options.
1 2 3 4 5 6 7 8 |
touch myfile{1..3} ## create some files and directories mkdir mydir mv -v myfile* mydir ## returns ## ‘myfile1’ -> ‘mydir/myfile1’ ## ‘myfile2’ -> ‘mydir/myfile2’ ## ‘myfile3’ -> ‘mydir/myfile3’ rm -r my* |
You can use mv -t or –target-directory=DIRECTORY to move all SOURCE arguments into DIRECTORY.
1 2 3 4 5 6 |
mkdir mydir{1,2} touch mydir1/myfile.{sh,txt,zip,tar} find mydir1/ -type f -name "*.t*" | xargs mv mydir2 ## does not works ## mv: target ‘mydir1/myfile.tar’ is not a directory find mydir1/ -type f -name "*.t*" | xargs mv -t mydir2 ## works with -t option rm -r my* |
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.
1 2 3 4 5 6 7 |
echo "one" > myfile1 echo "two" > mydir1 mkdir -p mydir/mydir1 mv -T myfile1 mydir/mydir1 ## mydir1 is treated as a file ## mv: cannot overwrite directory ‘mydir/mydir1’ with non-directory mv myfile1 mydir/mydir1 ## mydir1 is treated as a directory rm -r my* |
Hope you have enjoyed this article. In the next blog post, we will discuss cp command in Linux.