# Cheat Sheet #day51 - cat

## `cat` Command Cheatsheet

The `cat` command in Unix/Linux is used to concatenate and display the contents of files. It stands for "concatenate" and is commonly used to read and output file content. Below are the basic and advanced usages of the `cat` command along with practical examples.

### Basic Usage

* **Display the contents of a file**
    
    ```bash
    cat filename
    ```
    
* **Display the contents of multiple files**
    
    ```bash
    cat file1 file2
    ```
    
* **Concatenate multiple files and redirect the output to a new file**
    
    ```bash
    cat file1 file2 > newfile
    ```
    

### Common Options

* **Number all output lines**
    
    ```bash
    cat -n filename
    ```
    
* **Show non-printing characters (except for tabs and the end of line)**
    
    ```bash
    cat -v filename
    ```
    
* **Display line ends with a** `$` sign
    
    ```bash
    cat -E filename
    ```
    
* **Display tabs as** `^I`
    
    ```bash
    cat -T filename
    ```
    
* **Suppress repeated empty output lines**
    
    ```bash
    cat -s filename
    ```
    

### Examples

* **Display a single file**
    
    ```bash
    cat file.txt
    ```
    
* **Display multiple files**
    
    ```bash
    cat file1.txt file2.txt
    ```
    
* **Concatenate files and write to a new file**
    
    ```bash
    cat file1.txt file2.txt > newfile.txt
    ```
    
* **Number all output lines**
    
    ```bash
    cat -n file.txt
    ```
    
* **Show non-printing characters**
    
    ```bash
    cat -v file.txt
    ```
    
* **Display line ends**
    
    ```bash
    cat -E file.txt
    ```
    
* **Display tabs as** `^I`
    
    ```bash
    cat -T file.txt
    ```
    
* **Suppress repeated empty lines**
    
    ```bash
    cat -s file.txt
    ```
    

### Advanced Usage

* **Concatenate files and append to an existing file**
    
    ```bash
    cat file1.txt file2.txt >> existingfile.txt
    ```
    
* **Create a new file with** `cat` (end input with `Ctrl+D`)
    
    ```bash
    cat > newfile.txt
    This is the content of the new file.
    ```
    
* **Combine** `cat` with other commands using pipes
    
    ```bash
    cat file.txt | grep "search_term"
    ```
    

### Practical Tips

* **View Large Files**: Use `cat` to quickly view the contents of large files, but consider using `less` or `more` for easier navigation.
    
* **Combine Files**: Use `cat` to combine multiple files into a single file.
    
* **Create Files**: Use `cat` to create and quickly input content into new files.
    
* **Use with Caution**: Be careful with output redirection (`>`) to avoid accidentally overwriting important files.
    

### Quick Reference

* **Display file contents**:
    
    ```bash
    cat filename
    ```
    
* **Number all output lines**:
    
    ```bash
    cat -n filename
    ```
    
* **Show non-printing characters**:
    
    ```bash
    cat -v filename
    ```
    
* **Display line ends**:
    
    ```bash
    cat -E filename
    ```
    
* **Display tabs as** `^I`:
    
    ```bash
    cat -T filename
    ```
    
* **Suppress repeated empty lines**:
    
    ```bash
    cat -s filename
    ```
    
* **Concatenate files**:
    
    ```bash
    cat file1 file2 > newfile
    ```
    
* **Append to an existing file**:
    
    ```bash
    cat file1 file2 >> existingfile
    ```
    
* **Create a new file**:
    
    ```bash
    cat > newfile.txt
    ```
    

This cheatsheet covers the essential commands and options for using `cat` effectively, from displaying file contents to concatenating and creating new files. Adjust the commands according to your specific requirements and environment.
