Enhancing Skills

mv: Move or rename files and directories

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/: Moves file1.txt from the current directory to /home/user/Documents/.
  • ls /home/user/Documents/: Lists the contents of the destination directory to confirm that file1.txt has been moved.

Sample Command and Output:

$ mv file1.txt file2.txt
$ ls
file2.txt

Description:

  • mv file1.txt file2.txt: Renames file1.txt to file2.txt in the current directory.
  • ls: Lists the contents of the current directory to confirm that file1.txt has been renamed to file2.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 directory folder1 and its contents recursively to /home/user/Backup/. The -r option indicates recursive operation, though it’s worth noting that mv handles directories by default without needing -r.
  • ls /home/user/Backup/: Lists the contents of the destination directory to confirm that folder1 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/: Moves file1.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.


Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.