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.txtfrom the current directory tomyfolder.
bash mv filename.txt myfolder/ -
Rename a file: This command renames
filename.txttonewfilename.txtin the current directory.
bash mv filename.txt newfilename.txt -
Move and rename a file: This command moves
filename.txttomyfolderand renames it tonewfilename.txt.
bash mv filename.txt myfolder/newfilename.txt -
Move multiple files to a directory: This command moves
file1.txtandfile2.txttomyfolder.
bash mv file1.txt file2.txt myfolder/ -
Use the interactive mode: This command prompts for confirmation before overwriting
filename.txtinmyfolderif it already exists.
bash mv -i filename.txt myfolder/ -
Move a directory: This command moves the entire directory
myfolderto another directory callednewfolder.
bash mv myfolder/ newfolder/ -
Move all files with a specific extension: This command moves all
.txtfiles from the current directory tomyfolder.
bash mv *.txt myfolder/ -
Move files and directories recursively: This command moves
myfolderand all its contents tonewfolder.
bash mv -r myfolder/ newfolder/ -
Rename a directory: This command renames
myfoldertonewfolder.
bash mv myfolder newfolder -
Move a file and preserve attributes: This command moves
filename.txttomyfolderwhile preserving its attributes.
bash mv -p filename.txt myfolder/