Linux mv command is used to move files and directories from one location to another. Apart from moving the files, it can also rename a file or directory.
1. Rename a File
While renaming a file using mv command, it keeps the inode number same even after moving it to a different name. If you move the file to a different filesystem, the inode number will be different.
$ cd /tmp $ ls -i sample.txt 136407 sample.txt $ mv sample.txt sample1.txt $ ls -i sample1.txt 136407 sample1.txt
Note: ls -i displays the inode number of a file, as we discussed earlier in our ls command examples.
2. Rename a Directory
Just like renaming a file, you can rename a directory using mv command as shown below. This also keeps the inode number of the directory same after renaming.
If you just do ls -l dir, it will display the files in the directory. To display the directory properties, use -d option. -i option displays the inode number of the directory.
$ ls -ldi dir1 271365 drwxr-xr-x 2 bala bala 4096 2010-10-30 20:25 dir1 $ mv dir1 dir2 $ ls -ldi dir2 271365 drwxr-xr-x 2 bala bala 4096 2010-10-30 20:25 dir2
3. Prompt for a Confirmation Before Overwriting
By default mv command will not ask for any confirmation if the destination file exist, it simply overwrites it. To avoid this you might want to get a confirmation from move command before overwriting the destination file using -i option as shown below. You may type either ‘y’ or ‘n’ to accept or reject the move operation.
$ mv -i sample.txt sample1.txt mv: overwrite `sample1.txt'?
When the destination file permission is different than the source file, mv -i command will display the following confirmation.
$ mv -i sample.txt sample1.txt mv: try to overwrite `sample1.txt', overriding mode 0644 (rw-r--r--)? y
4. Move Multiple Files to a Specific Directory
You can move multiple files using mv command. The following example moves the content of the current directory to a different directory.
$ cd chap1 $ ls -F ex1.c ex2.c ex3.c example/ exercise/ $ mv * chap2/
5. Take a Backup of Destination Before Overwriting
Using mv –suffix option, you can take the backup of the destination file before overwriting. The original destination file will be moved with the extension specified in the -S or –suffix option.
$ ls file1 file2 $ mv --suffix=.bak file1 file2 $ ls file2 file2.bak
Note: You might want to create an alias for ‘mv –suffix’, which will take a backup automatically anytime you use mv command and if the destination file exist.
6. Move only the files that don’t exist in the destination directory
When you do mv *, it will move all the files to the destination directory. However, if you want to move only the files from the source directory that don’t exist in the destination directory, use the mv -u option as shown below.
The following command will move only the ex2 and ex2 from chap1 to chap2, as ex1 file already existing in chap2, which will not be moved.
$ ls chap1 ex1 ex2 ex3 $ ls chap2 ex1 $ mv -u chap1/* chap2/ $ ls chap1 ex1 $ ls chap2 ex1 ex2 ex3
Get the Linux Sysadmin Course Now!