mv

Move files and/or folders.

Syntax
      mv [options] source target

Options
     -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.

If the Apple Finder is used to remove the file extension from a file, it will actually set the extension to be hidden instead, which Get Info will reveal. Use mv to genuinely remove or change the file extension, but note this will not convert any file type it just changes the name.

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 their startup file, so that progress is always displayed and files do not get overwritten without a confirmation.

Examples

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

Rename all files in the current directory adding a .WAV file extension:
$ for file in *; do mv "$file" "$file.wav"; done

Rename all .txt files in the current directory to .html
$ for f in *.txt; do mv "$f" "${f%txt}html"; done

Using bash shell parameter expansion ${parameter/pattern/string}

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

Reversing this to replace underscores with spaces:
for f in *.txt; do mv "$f" "${f//_/ }";done

Rename files that end with 'copy.txt' to just .txt
for f in *copy.html; do mv "$f" "${f/copy.txt/.txt}";done

Move all .jpg files in the current directory, to the 'CA' sub-folder, and for those with "New York" in the filename, replace with "California_"

$ mkdir CA
$ for f in *.jpg; do mv "$f" "CA/${f/New York/California_}"; 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-2024 SS64.com
Some rights reserved