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.
Using HashiCorp Vault with LDAP
How to use HashiCorp Vault to setup an LDAP backed secret store with read-only access for users in groups and read-write access for specific users
Linux and Unix xargs command tutorial with examples
Tutorial on using xargs, a UNIX and Linux command for building and executing command lines from standard input. Examples of cutting by character, byte position, cutting based on delimiter and how to modify the output delimiter.
Copy a file in Go
How to copy a file in Go. The ioutil package does not offer a shorthand way of copying a file. Instead the os package should be used.