mv

Move or rename files or directories.

Syntax
      mv [options]... Source Dest

      mv [options]... Source... Directory

If the last argument 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.

Options

   -b
   --backup
       Make a backup of each file that would otherwise be overwritten or removed.

   -f
   --force
       Remove existing destination files and never prompt the user.

   -i
   --interactive
       Prompt whether to overwrite each existing destination file,
       regardless of its permissions.  If the response does not begin
       with 'y' or 'Y', the file is skipped.

   -S SUFFIX
   --suffix=SUFFIX
       Append SUFFIX to each backup file made with '-b'.
       The backup suffix is ~, unless set with SIMPLE_BACKUP_SUFFIX.

   -u
   --update
       Do not move a nondirectory that has an existing destination with
       the same or newer modification time.

   -v
   --verbose
       Print the name of each file before moving it.

   -V METHOD
   --version-control=METHOD'
       Change the type of backups made with '-b'. METHOD can be:
     
       t, numbered     make numbered backups
       nil, existing   numbered if numbered backups exist, simple otherwise
       never, simple   always make simple backups

    --help       Display help and exit
    --version    Output version information and exit

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.

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.

'mv' can move only regular files across filesystems.

If a destination file exists but is normally unwritable, standard input is a terminal, and the '-f' or '--force' option is not given, 'mv' prompts the user for whether to replace the file. (You might own the file, or have write permission on its directory.) If the response does not begin with 'y' or 'Y', the file is skipped.

Examples

Rename the file apple as orange.rtf:

mv apple orange.rtf

Move orange.doc to the Documents folder:

mv orange.rtf ~/Documents/orange.rtf

Rename a bunch of .txt file extensions 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 Linux commands

cp - Copy one or more files to another location.
mmv - Mass Move and rename.
mv function - prompt to edit the new filename.
Equivalent Windows command: MOVE - Move files from one folder to another.


 
Copyright © 1999-2024 SS64.com
Some rights reserved