6 mv Command Examples to Move or Rename Linux File and Directory

by Balakrishnan Mariyappan on December 7, 2010

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

Linux Sysadmin Course Linux provides several powerful administrative tools and utilities which will help you to manage your systems effectively. If you don’t know what these tools are and how to use them, you could be spending lot of time trying to perform even the basic administrative tasks. The focus of this course is to help you understand system administration tools, which will help you to become an effective Linux system administrator.
Get the Linux Sysadmin Course Now!

If you enjoyed this article, you might also like..

  1. 50 Linux Sysadmin Tutorials
  2. 50 Most Frequently Used Linux Commands (With Examples)
  3. Top 25 Best Linux Performance Monitoring and Debugging Tools
  4. Mommy, I found it! – 15 Practical Linux Find Command Examples
  5. Linux 101 Hacks 2nd Edition eBook Linux 101 Hacks Book

Bash 101 Hacks Book Sed and Awk 101 Hacks Book Nagios Core 3 Book Vim 101 Hacks Book

{ 5 comments… read them below or add one }

1 Saffo December 7, 2010 at 3:50 am

thanx for the e-mails they `re very helpful, esp to some of us who`re new to unix

2 Jaxx December 7, 2010 at 3:50 am

Thanks Ramesh,

As usuall very informative. I can really use these. Everybody in *nix environment know the mv command but I have not thought about checking the other options.

3 Zoresvit December 7, 2010 at 12:04 pm

In part 6 of the article there is a misprint in line
> The following command will move only the ex2 and ex2 from chap1 to chap2, as ex1 file

it should be “ex2 and ex3 from chap1″

And thanks for the tips!

4 ben December 8, 2010 at 1:50 am

Example 4 variation:
mv –target-directory=/my/destination/directory /my/file/to/move1.txt /my/file/to/move2.txt /my/otherfile/to/move.txt

“–target-directory” argument works the same for cp by the way.

5 Veerendra Jonnalagadda June 21, 2012 at 12:40 am

there any flag for mv to create directory and then move..
I mean I am moving *.c to src/ but src does not exists. I want in such case it should create directory called src

Leave a Comment

Previous post:

Next post: