In this article, you will learn 5 common uses of
mv command in Linux. The
mv command is not only used for moving files but also for renaming files. However there’s more that you can do with this utility command.
Here are the examples:
1. Moving files using mv command
If you need to move file abc.txt for example in another directory, lets say /home/sonik/Desktop/ here is how it’s done:
mv abc.txt /home/sonik/Desktop/
2. Take a backup before overwriting
Using the –
b option, you can take backup of destination file before overwriting the existing file.
mv -bv *.txt ./home/sonik/Desktop/
3. Renaming files and directories
Renaming files or directories works just as same as moving a file, except you have to be in the same location.
Code language: CSS (css)
# How to rename file.txt to filenew.txt in same the location. mv file.txt filenew.txt # How to rename a directory mv dir dirnew
4. Don’t override any existing files
The following command merges one folder to another, but doesn’t override any existing files
mv -n dir1/* ./home/dir2 # only update with the new file mv -u dir1/* ./home/dir2
5. Move the files from parent directory to one of the child
You can move all the files from parent directory to the child directory with the following command:
Code language: PHP (php)
cd parent mv !(child1|child2) child1