Moving or renaming files or directory in Linux – Top 9 uses of mv command with example

Command mv is used to move files or directories from one location to another in Linux systems. In addition to moving files it can be used to rename files and directories also.

Let’s see different uses of mv command in Linux.

  1. Moving file

    Here we will use mv command to move file from one folder to another folder. Please note both locations should be different. File will be deleted from original location and moved to new location given by user.

    Syntax

     mv filename /folder/name

    Example

     mv text1 /home/test/
  2. Moving more than one file

    It is also possible to move multiple file with move command. All file names are given with space between them

    Syntax

     mv filename filename1 filename2 /folder/name

    Example

     mv text1 text2 /home/test/

    Moving similar type of files using wildcard character. These files with same extension or have some pattern in their names.
    Syntax

     mv *.mp3 filename2  /folder/name
  1. Moving directory

    To move a directory with mv command we will have to use mv command without any parameters.

    Syntax

     mv directory_name/ /folder/name/for/move
  2. Renaming files

    Another use of mv command is renaming of files. Renaming a file with mv command requires to use mv with name of file followed by change name of file. Note: location of file should be same.

    Syntax

     mv old_file_name new_file_name

    Absolute paths can also be given

     mv /folder/name/old_file_name /folder/name/new_file_name
  3. Renaming directories

    Same as renaming a file with mv command can be used for renaming of directory. For this use mv with name of directory followed by change name of directory. Note: location of directory should be same.

    Syntax

     mv old_directory_name new_directory_name
  4. Backing up when moving file

    mv command has a –b option which allows to backup file while moving it. This option is very helpful if you want to recover file, if you have made any mistakes while moving file.

    Syntax

     mv -b filename /folder/name

    Note: Backup file will be created with tilde after file name.

  5. Interactive mode for mv command

    when using interactive mode for mv command, a prompt will be given before overwriting destination file if file with similar name exists in destination folder.

    Syntax

     mv -i filename /folder/name

    When prompted for user will have to provide y to overwrite file.

  6. No overwrite

    No files will be overwritten when –n option is used with mv

    Syntax

     mv -n filename /folder/name

    If file exists will same name in destination folder move will not be done.

  7. Update file when destination file is newer

    Using update –u option with mv command will ensure file is moved only when source file newer than destination file or no file with same name exists in destination folder.

    Syntax
     mv -u filename /folder/name