mv: Move or rename files and directories
August 9th, 2024 8:59 AM Mr. Q Categories: Command
Command: mv
Used to move or rename files and directories, including the ability to move directories and their contents recursively.
Sample Command and Output:
$ mv file1.txt /home/user/Documents/
$ ls /home/user/Documents/
file1.txt
Description:
mv file1.txt /home/user/Documents/
: Movesfile1.txt
from the current directory to/home/user/Documents/
.ls /home/user/Documents/
: Lists the contents of the destination directory to confirm thatfile1.txt
has been moved.
Sample Command and Output:
$ mv file1.txt file2.txt
$ ls
file2.txt
Description:
mv file1.txt file2.txt
: Renamesfile1.txt
tofile2.txt
in the current directory.ls
: Lists the contents of the current directory to confirm thatfile1.txt
has been renamed tofile2.txt
.
Sample Command and Output:
$ mv -r folder1/ /home/user/Backup/
$ ls /home/user/Backup/
folder1
Description:
mv -r folder1/ /home/user/Backup/
: Moves the directoryfolder1
and its contents recursively to/home/user/Backup/
. The-r
option indicates recursive operation, though it’s worth noting thatmv
handles directories by default without needing-r
.ls /home/user/Backup/
: Lists the contents of the destination directory to confirm thatfolder1
has been moved.
Additional Argument:
-i
: Prompts before overwriting files.-f
: Forces the move, overwriting files without prompting.-v
: Verbose mode, displays the files being moved.
Sample Command and Output:
$ mv -v file1.txt /home/user/Documents/
renamed 'file1.txt' -> '/home/user/Documents/file1.txt'
Description:
mv -v file1.txt /home/user/Documents/
: Movesfile1.txt
to/home/user/Documents/
and displays the move operation with a message indicating the file rename action. The-v
option is used to provide verbose output.