mv command tutorial in linux/unix with examples and use cases

linux mv command – move (rename) files or directories.

Linux mv command is used to rename or move a file or directory to another location.

syntax

       mv [OPTION]... [-T] SOURCE DEST
       mv [OPTION]... SOURCE... DIRECTORY
       mv [OPTION]... -t DIRECTORY SOURCE...

Rename SOURCE to DEST, or move SOURCE(s) to DIRECTORY.

options

  • –backup[=CONTROL]
    make a backup of each existing destination file
  • -b
    like –backup but does not accept an argument
  • -f, –force
    do not prompt before overwriting
  • -i, –interactive
    prompt before overwrite
  • -n, –no-clobber
    do not overwrite an existing file
  • –strip-trailing-slashes
    remove any trailing slashes from each SOURCE argument
  • -S, –suffix=SUFFIX
    override the usual backup suffix
  • -t, –target-directory=DIRECTORY
    move all SOURCE arguments into DIRECTORY
  • -T, –no-target-directory
    treat DEST as a normal file
  • -u, –update
    move only when the SOURCE file is newer than the destination file or when the destination file is missing
  • -v, –verbose
    explain what is being done

examples

Rename file or directory

➜ mv file.log fileNew.log


➜ mv dir newDir

Move multiple files

➜ mv access.log file.log newDir

OR

➜ mv *.log newDir

Move overwriting without asking (force)

➜ mv -f file.log newDir

Interactive mode move overwrite files and backup original files

If the target file does not exist, it will be created. If the target file exists, a warning will be prompted. Enter “Y” to back up the original file and overwrite it.

➜ mv -bi file.log newDir

OR

➜ mv -biS ~ file.log newDir

Move all files and folders in the current directory

➜ mv * /tmp

move just files

➜ find . -type f -exec mv {} /tmp \;

Add a Comment

Your email address will not be published. Required fields are marked *