Cheat Sheet #day46 - mv

Cheat Sheet #day46 - mv

mv Command Cheatsheet

The mv command in Unix/Linux is used to move or rename files and directories. It stands for "move" and can be a powerful tool when used effectively. Below are the basic and advanced usages of the mv command along with practical examples.

Basic Usage

  • Move a file to another directory

      mv source_file /destination/directory/
    
  • Rename a file

      mv old_filename new_filename
    

Common Options

  • Interactive mode (prompt before overwrite)

      mv -i source_file /destination/directory/
    
  • Force move without prompting

      mv -f source_file /destination/directory/
    
  • Verbose mode (show what is being done)

      mv -v source_file /destination/directory/
    
  • Backup the destination file if it exists

      mv -b source_file /destination/directory/
    
  • Create a backup with a specific suffix

      mv --suffix=.bak source_file /destination/directory/
    
  • Update only if the source file is newer

      mv -u source_file /destination/directory/
    

Examples

  • Move a single file

      mv file.txt /home/user/Documents/
    
  • Rename a file

      mv file.txt new_file.txt
    
  • Move multiple files to a directory

      mv file1.txt file2.txt file3.txt /home/user/Documents/
    
  • Move a directory

      mv /home/user/Downloads /home/user/Documents/
    
  • Interactive move (prompt before overwrite)

      mv -i file.txt /home/user/Documents/
    
  • Verbose move

      mv -v file.txt /home/user/Documents/
    

Advanced Usage

  • Move all files with a specific extension

      mv *.txt /home/user/Documents/
    
  • Move files using a wildcard and rename

      mv *2023.log /home/user/Logs/
    
  • Move and overwrite only if the source file is newer

      mv -u file.txt /home/user/Documents/
    
  • Create a backup of files being overwritten

      mv -b file.txt /home/user/Documents/
    
  • Create a backup with a custom suffix

      mv --suffix=.old file.txt /home/user/Documents/
    

Practical Tips

  • Safety First: Use the -i option to prevent accidental overwrites.

  • Backup: Use the -b option or --suffix to keep backups of overwritten files.

  • Verbose Mode: Use -v to see exactly what mv is doing, useful for debugging scripts.

Quick Reference

  • Move a file:

      mv source_file /destination/directory/
    
  • Rename a file:

      mv old_filename new_filename
    
  • Interactive mode:

      mv -i source_file /destination/directory/
    
  • Force move:

      mv -f source_file /destination/directory/
    
  • Verbose mode:

      mv -v source_file /destination/directory/
    
  • Backup before overwriting:

      mv -b source_file /destination/directory/
    
  • Update only if newer:

      mv -u source_file /destination/directory/
    

This cheatsheet covers the essential commands and options for using mv effectively, from basic file moves and renaming to more advanced file management tasks. Adjust the commands according to your specific requirements and environment.

Did you find this article valuable?

Support Cloud Tuned by becoming a sponsor. Any amount is appreciated!