# Cheat Sheet #day47 - cp

## `cp` Command Cheatsheet

The `cp` command in Unix/Linux is used to copy files and directories. It stands for "copy" and can be utilized for various file management tasks. Below are the basic and advanced usages of the `cp` command along with practical examples.

### Basic Usage

* **Copy a file to another directory**
    
    ```bash
    cp source_file /destination/directory/
    ```
    
* **Copy and rename a file**
    
    ```bash
    cp source_file /destination/directory/new_filename
    ```
    

### Common Options

* **Interactive mode (prompt before overwrite)**
    
    ```bash
    cp -i source_file /destination/directory/
    ```
    
* **Force copy without prompting**
    
    ```bash
    cp -f source_file /destination/directory/
    ```
    
* **Verbose mode (show what is being done)**
    
    ```bash
    cp -v source_file /destination/directory/
    ```
    
* **Recursive copy (copy directories and their contents)**
    
    ```bash
    cp -r source_directory /destination/directory/
    ```
    
* **Preserve file attributes (ownership, timestamps, etc.)**
    
    ```bash
    cp -p source_file /destination/directory/
    ```
    
* **Update only if the source file is newer**
    
    ```bash
    cp -u source_file /destination/directory/
    ```
    
* **Create hard links instead of copying**
    
    ```bash
    cp -l source_file /destination/directory/
    ```
    
* **Create symbolic links instead of copying**
    
    ```bash
    cp -s source_file /destination/directory/
    ```
    

### Examples

* **Copy a single file**
    
    ```bash
    cp file.txt /home/user/Documents/
    ```
    
* **Copy and rename a file**
    
    ```bash
    cp file.txt /home/user/Documents/new_file.txt
    ```
    
* **Copy multiple files to a directory**
    
    ```bash
    cp file1.txt file2.txt file3.txt /home/user/Documents/
    ```
    
* **Copy a directory recursively**
    
    ```bash
    cp -r /home/user/Downloads /home/user/Documents/
    ```
    
* **Interactive copy (prompt before overwrite)**
    
    ```bash
    cp -i file.txt /home/user/Documents/
    ```
    
* **Verbose copy**
    
    ```bash
    cp -v file.txt /home/user/Documents/
    ```
    

### Advanced Usage

* **Copy all files with a specific extension**
    
    ```bash
    cp *.txt /home/user/Documents/
    ```
    
* **Copy files using a wildcard and rename**
    
    ```bash
    cp *2023.log /home/user/Logs/
    ```
    
* **Copy and overwrite only if the source file is newer**
    
    ```bash
    cp -u file.txt /home/user/Documents/
    ```
    
* **Preserve file attributes during copy**
    
    ```bash
    cp -p file.txt /home/user/Documents/
    ```
    
* **Create a hard link to a file**
    
    ```bash
    cp -l file.txt /home/user/Documents/
    ```
    
* **Create a symbolic link to a file**
    
    ```bash
    cp -s file.txt /home/user/Documents/
    ```
    

### Practical Tips

* **Safety First**: Use the `-i` option to prevent accidental overwrites.
    
* **Preserve Attributes**: Use the `-p` option to maintain file attributes.
    
* **Verbose Mode**: Use `-v` to see exactly what `cp` is doing, useful for debugging scripts.
    
* **Recursive Copy**: Always use `-r` when copying directories to ensure all contents are copied.
    

### Quick Reference

* **Copy a file**:
    
    ```bash
    cp source_file /destination/directory/
    ```
    
* **Copy and rename a file**:
    
    ```bash
    cp source_file /destination/directory/new_filename
    ```
    
* **Interactive mode**:
    
    ```bash
    cp -i source_file /destination/directory/
    ```
    
* **Force copy**:
    
    ```bash
    cp -f source_file /destination/directory/
    ```
    
* **Verbose mode**:
    
    ```bash
    cp -v source_file /destination/directory/
    ```
    
* **Recursive copy**:
    
    ```bash
    cp -r source_directory /destination/directory/
    ```
    
* **Preserve file attributes**:
    
    ```bash
    cp -p source_file /destination/directory/
    ```
    
* **Update only if newer**:
    
    ```bash
    cp -u source_file /destination/directory/
    ```
    
* **Create hard link**:
    
    ```bash
    cp -l source_file /destination/directory/
    ```
    
* **Create symbolic link**:
    
    ```bash
    cp -s source_file /destination/directory/
    ```
    

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