mv
mv linux command cheatsheet by Thamizhiniyan C S
Introduction
This command is used to move the files and directories from the source to destination. Also used for renaming files.
Syntax
mv [OPTION]... [-T] SOURCE DEST
mv [OPTION]... SOURCE... DIRECTORY
mv [OPTION]... -t DIRECTORY SOURCE...
Important Flags
-b, --backup
Create a backup of files that will be overwritten or removed.
-f, --force
Overwrite destination files without prompting the user.
-i, --interactive
Prompt the user to confirm if the mv action should overwrite a file.
-S, --suffix=
Provide a suffix for a backup file. The default suffix is ~.
-u, --update
Perform the mv action only if the source file is newer or the destination file does not exist.
-v, --verbose
Show an output describing the action taken.
--help
Output the command help and exit.
--version
Show the command version and exit.
Examples
mv name1 name2
Rename the file name1 to name2. The command produces no output, but ls confirms the operation was successful.
mv filename /path/to/destination/
To move a file from one folder to another
mv directory_name /path/to/destination/
To move an entire directory
mv [source_file_name(s)] [Destination_file_name]
Rename a file: Rename source_file_name to Destination_file_name. Overwrites Destination_file_name if it exists.
mv filename2 filename1
Renames filename2 to filename1. Overwrites filename1 if it exists.
mv [source_file_name(s)] [Destination_path]
Move a file: Move source_file_name(s) to Destination_path.
mv filename1 /home/user/test/
Moves filename1 to /home/user/test/.
mv [source_file_name_1] [source_file_name_2] [...] [Destination_path]
Move multiple files: Move source_file_name_1, source_file_name_2, etc., to Destination_path.
mv file_1 file_2 /home/user/test/
Moves file_1 and file_2 to /home/user/test/.
mv [source_directory_name(s)] [Destination_directory_name]
Rename directory: Rename source_directory_name(s) to Destination_directory_name. Overwrites Destination_directory_name if it exists.
mv test new_file
Renames test to new_file. Overwrites new_file if it exists.
mv -i filename2 filename1
Renames filename2 to filename1, prompts for confirmation if filename1 exists.
mv -v name1 name2 name3 test-dir
Move the files name1, name2, and name3 to test-dir in the home directory and show verbose output. The output confirms successful file movement.
mv -v name1 name2
Rename the file name1 to name2 and print the output. This command produces an output listing the performed actions.
mv -v -i name1 name2
Rename name1 to name2, prompting for confirmation if name2 already exists.
mv -f file filename1
Forcefully renames file to filename1, overwriting filename1 if it exists.
mv -n oldfile newfile
Renames oldfile to newfile, does not overwrite newfile if it exists.
mv -b first_file second_file
Renames first_file to second_file, creates a backup of second_file if it exists.
mv --version
Displays the version of mv.
for file in *.24bes; do mv "$file" "${file%.24bes}"; done
Loops through all the files with .24bes extension and removes the extension `.24bes`.
Last updated
Was this helpful?