Mv command linux file move rename
Command definition
The mv
command in Linux is used to move files and directories from one location to another, and it can also be used to rename files and directories. When you use mv
, the original file is removed from its initial location and placed in the new specified location. If the destination is a different name, the file will be renamed; if it’s the same name, it will overwrite the existing file without warning unless the -i
option is used.
Usage examples
-
Move a file to a different directory: This command moves
filename.txt
from the current directory tomyfolder
.
bash mv filename.txt myfolder/
-
Rename a file: This command renames
filename.txt
tonewfilename.txt
in the current directory.
bash mv filename.txt newfilename.txt
-
Move and rename a file: This command moves
filename.txt
tomyfolder
and renames it tonewfilename.txt
.
bash mv filename.txt myfolder/newfilename.txt
-
Move multiple files to a directory: This command moves
file1.txt
andfile2.txt
tomyfolder
.
bash mv file1.txt file2.txt myfolder/
-
Use the interactive mode: This command prompts for confirmation before overwriting
filename.txt
inmyfolder
if it already exists.
bash mv -i filename.txt myfolder/
-
Move a directory: This command moves the entire directory
myfolder
to another directory callednewfolder
.
bash mv myfolder/ newfolder/
-
Move all files with a specific extension: This command moves all
.txt
files from the current directory tomyfolder
.
bash mv *.txt myfolder/
-
Move files and directories recursively: This command moves
myfolder
and all its contents tonewfolder
.
bash mv -r myfolder/ newfolder/
-
Rename a directory: This command renames
myfolder
tonewfolder
.
bash mv myfolder newfolder
-
Move a file and preserve attributes: This command moves
filename.txt
tomyfolder
while preserving its attributes.
bash mv -p filename.txt myfolder/