Move files and/or folders.

      mv [options] source target

     -n     Do not overwrite any existing file.

     -i     Prompt before moving a file that would overwrite an existing file.
            A response 'y' or 'Y', will allow the move to proceed (writes to standard error)

     -f     Force, always overwrite destination files, do not prompt for confirmation.

     -v     Verbose, show filenames.

If the last argument (target) names an existing directory, 'mv' moves each other given file into a file with the same name in that directory. Otherwise, if only two files are given, it renames the first as the second. It is an error if the last argument is not a directory and more than two files are given.

The key combination Ctrl+w (cut last word followed by Ctrl-y, Ctrl-y (Paste twice) can be useful when writing mv commands, to quickly create two copies of the path, you can then just edit the destination to generate the new name.

The mv utility now supports HFS+ Finder and Extended Attributes and resource forks. The mv utility will no longer strip resource forks from HFS files. For an alternative method, refer to cp.

The -n and -v options are non-standard and their use in scripts used across multiple unix machines is not recommended. Notwithstanding this many users find it useful to set an alias mv="mv -iv" in bashrc, so that progress is always displayed and files do not get overwritten without a confirmation.


Move all files with names ending in ".jpg" from the current folder to the Documents directory
$ mv *.jpg ~/Documents

Move the "Documents" folder to "Documents backup", quotes are needed because of the space in the folder name.
$ mv Documents "Documents backup"

Move the file Hunter.txt to the Trash. The tilde ~ indicates that the .Trash folder is located in the users home.

$ mv Hunter.txt ~/.Trash

Move all .jpg files to the CA folder, and for those with "New York" in the filename, replace with "California_"
the "${f/New York/California_}" is an application of bash parameter expansion.

$ mkdir CA
$ for f in *.jpg; do mv "$f" "CA/${f/New York/California_}"; done

Rename all files in the current directory, replacing spaces with underscores:
ls | while read f; do mv "$f" "${f// /_}";done

Rename .txt files to .html
$ for f in *.txt; do mv ./"$f" "${f%txt}htm"; done

“Any intelligent fool can make things bigger and more complex...
It takes a touch of genius - and a lot of courage to move in the opposite direction” ~ E.F. Schumacher

Related macOS commands:

cp - Copy files.
CpMac - Copy a file while preserving metadata and forks (Developer Tools).
dd - Data Duplicator - convert and copy a file.
install - Copy files and set attributes.
MvMac - Copy a filewhile preserving metadata and forks (Developer Tools).
tar - store or extract files to an archive (allows symbolic links to be copied as links).

Copyright © 1999-2021
Some rights reserved