Moving or Renaming Files using mv Command in Linux

The linux ‘mv’ command is used to move existing file or directory from one location to another. When you want to rename a single directory or file then ‘mv’ option will be better to use.

mv also works for renaming a file:

For example:

$ mv doc document

In the above example, we have renamed file ‘doc’ into ‘document’.

Now if we want to move a file from one directory to other directory, we should use:

$ mv <path-of-source> <path-of-destination>

Note that, move accepts *, ., ..; So we can use it like:

$ mv test1.txt ..

The above example moves test1.txt to parent directory.

$ mv * ..

The above example moves all files and directories in the current directory to parent directory.

$ mv *.txt ~/Documents

The above example moves all the files with “.txt” at end, to Documents directory of users home directory.

$ mv /var/logs/* ~/logs

The above example all files in /var/logs directory to logs directory of user’s home directory.

How To Rename a Directory ?

Directories can be renamed in the same way as the files. In this case also number will remain the same.

mv example Option are show below :

  1. mv -i prompts before overwrite
  2. mv -b backs up files in the destination if exists
  3. mv -u only move those files that doesn’t exist or the source file is newer than the destination

Leave a Reply