5 uses of mv command in linux terminal
In this article, I'll show you 5 common uses of mv
command in linux. The mv
command not only used for moving files but also for renaming files. However there's more you can do with this utility command. Here are the examples:
1. Moving files using mv command
Let's say we want to move abc.md file from current directory to ./home/ash, we do it like this:
$ mv abc.md ./home/ash
To move multiple files from current directory to ./home directory.
$ mv * ./home/
# or
$ mv f1.md f2.md ./home/
2. Renaming files and directory
Renaming files or directories work just same as moving file, except you have to be in the same location.
## rename `file.txt` to `filenew.txt` in same location.
$ mv file.txt filenew.txt
## rename directory
$ mv dir dirnew
3. Don't override any existing file
The following command merge one folder to another, but don't override any existing files
$ mv -n dir1/* ./home/dir2
# only update with the new file
$ mv -u dir1/* ./home/dir2
4. Take a backup before overwriting
Using the b
option, we can take backup of destination file before overwriting existing file.
$ mv -bv *.txt ./home/dir2
5. Move the files from parent directory to one of the child.
This one is from stackoverflow answer.
From the stackoverflow, suppose you've following structure:
parent
--child1
--child2
--grandChild1
--grandChild2
--grandChild3
--grandChild4
--grandChild5
--grandChild6
You can move all the files from parent directory to the child1 with the following command:
$ cd parent
$ mv !(child1|child2) child1