# 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**
    
    ```bash
    mv source_file /destination/directory/
    ```
    
* **Rename a file**
    
    ```bash
    mv old_filename new_filename
    ```
    

### Common Options

* **Interactive mode (prompt before overwrite)**
    
    ```bash
    mv -i source_file /destination/directory/
    ```
    
* **Force move without prompting**
    
    ```bash
    mv -f source_file /destination/directory/
    ```
    
* **Verbose mode (show what is being done)**
    
    ```bash
    mv -v source_file /destination/directory/
    ```
    
* **Backup the destination file if it exists**
    
    ```bash
    mv -b source_file /destination/directory/
    ```
    
* **Create a backup with a specific suffix**
    
    ```bash
    mv --suffix=.bak source_file /destination/directory/
    ```
    
* **Update only if the source file is newer**
    
    ```bash
    mv -u source_file /destination/directory/
    ```
    

### Examples

* **Move a single file**
    
    ```bash
    mv file.txt /home/user/Documents/
    ```
    
* **Rename a file**
    
    ```bash
    mv file.txt new_file.txt
    ```
    
* **Move multiple files to a directory**
    
    ```bash
    mv file1.txt file2.txt file3.txt /home/user/Documents/
    ```
    
* **Move a directory**
    
    ```bash
    mv /home/user/Downloads /home/user/Documents/
    ```
    
* **Interactive move (prompt before overwrite)**
    
    ```bash
    mv -i file.txt /home/user/Documents/
    ```
    
* **Verbose move**
    
    ```bash
    mv -v file.txt /home/user/Documents/
    ```
    

### Advanced Usage

* **Move all files with a specific extension**
    
    ```bash
    mv *.txt /home/user/Documents/
    ```
    
* **Move files using a wildcard and rename**
    
    ```bash
    mv *2023.log /home/user/Logs/
    ```
    
* **Move and overwrite only if the source file is newer**
    
    ```bash
    mv -u file.txt /home/user/Documents/
    ```
    
* **Create a backup of files being overwritten**
    
    ```bash
    mv -b file.txt /home/user/Documents/
    ```
    
* **Create a backup with a custom suffix**
    
    ```bash
    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**:
    
    ```bash
    mv source_file /destination/directory/
    ```
    
* **Rename a file**:
    
    ```bash
    mv old_filename new_filename
    ```
    
* **Interactive mode**:
    
    ```bash
    mv -i source_file /destination/directory/
    ```
    
* **Force move**:
    
    ```bash
    mv -f source_file /destination/directory/
    ```
    
* **Verbose mode**:
    
    ```bash
    mv -v source_file /destination/directory/
    ```
    
* **Backup before overwriting**:
    
    ```bash
    mv -b source_file /destination/directory/
    ```
    
* **Update only if newer**:
    
    ```bash
    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.
