
The mv command in Linux enables you to move files and directories from one location to another. It's also known as move mv and can also be used to rename files and directories in a file system.
This article explains how to use the mv command in Linux to move and rename files.
mv CommandBelow is the basic mv command syntax:
Within the above command, source is the target file or directory to move, and destination is the new file or directory location in the file system.
mv Command Optionsmv CommandMove a file from your working directory to another directory.
The above command moves file1.txt from your active working directory to the /home/user/documents/ directory.
Output:
Rename a file.
The above command renames oldname.txt to newname.txt in your working directory.
Output:
Move multiple files to a directory.
The above command moves file1.txt and file2.txt to the /home/user/documents/ directory.
Output:
Move a directory.
The above command moves the oldfolder directory to /home/user/newfolder.
Output:
Enable overwriting prompts before replacing an existing file.
The above command displays a confirmation prompt before overwriting file1.txt in the destination directory if the file exists.
Output:
Force move a file without prompting overwrite changes.
The above command moves file1.txt to the destination directory and overwrites the existing file without a confirmation prompt.
Output:
Move files without overwriting the existing files.
The above command moves file1.txt and file2.txt only if no files exist with the same name in the destination directory.
Output:
Backup file an existing file with a new name.
The above command renames file1.txt to file2.txt to create a backup of file2.txt with a tilde character (~) appended if the file exists.
Output:
View progress of the move operation.
The above command moves file1.txt and displays the progress of the move operation.
Output:
Move files with spaces in the names.
The above command moves the filename defined with quotes and moves it to the destination directory.
Output:
Move hidden files.
The above command moves the .hiddenfile file to the /home/user/documents/ directory.
Output:
Use the mv command in scripts to automatate file management tasks as described in the following steps.
Create a new sample script file mv_script.sh using a text editor such as Nano
Add the following contents to the file.
Enable execute privileges on the file.
Run the script
The above script appends the system date to all .txt files in the working directory.
Output:
Move files based on a pattern.
The above command uses a wildcard character to move all files with the .txt extension to the /home/user/textfiles/ directory.
Output:
Enable confirmation while moving files.
The above command moves all .txt files using interactive mode and enables confirmation prompts before overwriting any existing files in the destination directory.
Output:
Move files based on the age.
This above command finds and moves all files older than 30 days from /source_directory to /destination_directory.
Output:
You have used the mv command to perform file movement and renaming tasks in Linux. For more command options, run the man mv command to view the mv manual page.
0 Comments
Be the first to comment and share your perspective with the community.