2024-02-12
At its core, mv
performs two primary functions: renaming files and moving files from one location to another.
Renaming a file:
To rename a file, simply use the command with the old name and the new name:
mv old_file_name.txt new_file_name.txt
This will rename old_file_name.txt
to new_file_name.txt
within the current directory.
Moving a file:
To move a file to a different directory, specify the source and destination paths:
mv source_file.txt /path/to/destination/directory/
This moves source_file.txt
from its current location to the /path/to/destination/directory/
. Note that the destination directory must exist. If source_file.txt
already exists in the destination directory, it will be overwritten without warning.
Example:
Let’s say you have a file named my_document.txt
in your current directory and want to move it to a directory called Documents
located in your home directory. The command would be:
mv my_document.txt ~/Documents/
mv
can efficiently handle multiple files simultaneously. You can move multiple files into a single directory using wildcards or by listing each file individually:
Using wildcards:
mv *.txt /path/to/destination/
This moves all files ending in .txt
in the current directory to /path/to/destination/
.
Listing files individually:
mv file1.txt file2.jpg file3.pdf /path/to/destination/
This moves file1.txt
, file2.jpg
, and file3.pdf
to /path/to/destination/
.
Moving directories works similarly to moving files, but requires careful consideration.
mv source_directory /path/to/destination/
This moves the entire source_directory
and its contents to /path/to/destination/
. Again, the destination directory must exist, and overwriting will occur without a prompt if a directory with the same name already exists at the destination.
-i
OptionTo prevent accidental overwriting, use the -i
(interactive) option. This will prompt you for confirmation before overwriting an existing file:
mv -i source_file.txt /path/to/destination/
Now, if source_file.txt
already exists at the destination, mv
will ask:
mv: overwrite ‘/path/to/destination/source_file.txt’?
-f
OptionThe opposite of -i
is -f
(force). This option will overwrite existing files without any confirmation:
mv -f source_file.txt /path/to/destination/
You can combine options for more control. For example, to force overwrite multiple files interactively you might use:
mv -i -f *.txt /path/to/destination/
This example is a bit counter-intuitive and not commonly used, as using -f
will override the interactive prompt of -i
. It’s generally recommended to choose either -i
or -f
rather than combining them.
mv
will return a non-zero exit status if it encounters an error, such as trying to move a file to a non-existent directory or encountering permission issues. Checking the exit status is important in scripting environments.
Ensure you have the necessary permissions to move files and directories. If you lack the appropriate permissions, the mv
command will fail. You might need sudo
privileges for operations involving system directories or files.