mv command is a command line utility that moves files or directories from one place to another . It supports moving single files, multiple files and directories. It can prompt before overwriting and has an option to only move files that are new than the destination.
To move a file using the
mv command pass the name of the file and then the new name for the file. In the following example the file
foo.txt is renamed to
ls foo.txt mv foo.txt bar.txt ls bar.txt
To move a file into a directory using the
mv command pass the name of the file and then the directory. In the following example the file
foo.txt is moved into the directory
tree -F . . ├── bar/ └── foo.txt mv foo.txt bar tree -F . └── bar/ └── foo.txt
To move multiple files using the
mv command pass the names of the files or a pattern followed by the destination.
mv file1.txt file.2.txt file3.txt folder
The following example is the same as above but uses pattern matching to move all files with a
mv *.txt folder
To move a directory using the
mv command pass the name of the directory to move followed by the destination.
ls -F foo/ mv foo bar ls -F bar/
By default the
mv command will overwrite an existing file. Consider the following example.
ls foo.txt bar.txt mv foo.txt bar.txt ls bar.txt
In this example
bar.txt is overwritten by the file
file.txt. At some point when running
mv a file that was not meant to be overwritten probably will be. To prompt before overwriting a file the
-i option can be used.
ls foo.txt bar.txt mv -i foo.txt bar.txt mv: overwrite 'bar.txt'? n
To prevent an existing file from being overwritten pass the
-n option. This causes the
mv command to ignore anything that would overwrite an existing file. In the following example the effect is for nothing to happen as a file would be overwritten.
ls foo.txt bar.txt mv -n foo.txt bar.txt ls foo.txt bar.txt
To only move files that are newer than the destination pass the
-u option. If a file is not newer than a file with the same name in the destination it will not be moved.
tree -F . . ├── foo/ │ ├── bar.txt │ └── foo.txt ├── bar.txt └── foo.txt
Looking at the timestamps of these files we can see that the file
foo.txt in the current directory is newer than the file
foo.txt in the directory
ls -l total 4 drwxr-xr-x 2 george users 4096 Oct 11 20:18 bar -rw-r--r-- 1 george users 0 Oct 11 20:18 bar.txt -rw-r--r-- 1 george users 0 Oct 11 20:20 foo.txt ls -l bar total 0 -rw-r--r-- 1 george users 0 Oct 11 20:18 bar.txt -rw-r--r-- 1 george users 0 Oct 11 20:23 foo.txt
-u option means that only newer files will be moved.
mv foo.txt bar.txt bar ls bar foo.txt
foo.txt is not moved as it is older than the file in the destination folder.
To take a backup of an existing file that will be overwritten as a result of the
mv command pass the
-b option. This will create a backup file with the tilde character appended to it.
ls foo.txt bar.txt mv -b foo.txt bar.txt ls bar.txt bar.txt~
To change the backup suffix the
-S option may be used.
ls foo.txt bar.txt mv -S .bak -b foo.txt bar.txt ls bar.txt bar.txt.bak
Have an update or suggestion for this article? You can edit it here and send me a pull request.
Listening to BBC Radio with mpv
The BBC publishes high quality 320 kbps HLS AAC streams that can be used to listen to radio from the command-line using mpv. Here are the URLs and some aliases to start listening quickly.
Vim: So long Pathogen, hello native package loading
Vim 8 added a native way to load third-party packages. This means, for me at least, there is no longer any need to use a third party plugin helper like Pathogen.
Linux and Unix more command tutorial with examples
Tutorial on using more, a UNIX and Linux command for viewing the contents of a file or files one screen at a time. Examples of viewing a file, viewing multiple files, searching using regular expressions and opening the file in a text editor.