mv
Command: Moving and Renaming Files in Linux
Summary
The mv
command is a fundamental Linux utility used for moving or renaming files and directories. It's a versatile tool for file management, allowing users to organize their data efficiently.
Introduction
The mv
command, short for "move," is a command-line utility that moves files or directories from one location to another or renames them in place. When moving a file, mv
essentially changes the file's location in the file system. When renaming a file, it changes the filename without altering its content or location.
Use case and Examples
Moving a File
This command movesfile.txt
from the current directory to the /home/user/documents
directory. Renaming a File
This command renamesold_file.txt
to new_file.txt
within the same directory. Moving a Directory
This moves the directorymy_directory
from the current location to the /opt
directory. Moving Multiple Files
This command moves bothfile1.txt
and file2.txt
to the /home/user/backup/
directory. Renaming while Moving
This command movesfile.txt
to /home/user/documents/
and renames it to renamed_file.txt
. Commonly used flags
Flag | Description | Example |
---|---|---|
-f or --force | Forcefully moves or renames files, overwriting existing files without prompting. | mv -f file.txt /path/to/destination |
-i or --interactive | Prompts the user before overwriting existing files. | mv -i file.txt /path/to/destination |
-n or --no-clobber | Prevents overwriting existing files. If the destination file exists, mv will not overwrite it. | mv -n file.txt /path/to/destination |
-v or --verbose | Displays verbose output, showing each file as it is moved. | mv -v file.txt /path/to/destination |
-u or --update | Moves the file only if the source file is newer than the destination file or if the destination file does not exist. | mv -u file.txt /path/to/destination |