# Day-14 of #90DaysOfDevOps

# **Linux & Git-GitHub Cheat Sheet for DevOps**

This cheat sheet provides a collection of essential Linux, Git, and DevOps-related commands, along with detailed explanations to help streamline your development and DevOps workflows. Whether you're new to Linux, Git, or looking to enhance your knowledge, this guide will serve as a quick reference for your day-to-day tasks.

---

## **Linux Commands**

Linux commands are vital for navigating, managing files, and performing system tasks efficiently. Below is a breakdown of some of the most frequently used commands:

### **File and Directory Management**

* `ls`  
    Lists all files and directories in the current directory.
    
    ```bash
    ls
    ```
    
* `ls -l`  
    Lists files and directories with detailed information, such as file permissions, number of links, owner, group, size, and modification date.
    
    ```bash
    ls -l
    ```
    
* `mkdir <directory>`  
    Creates a new directory. Replace `<directory>` with the name of the directory you want to create.
    
    ```bash
    mkdir my_directory
    ```
    
* `cd <directory>`  
    Changes the current working directory to the specified directory.
    
    ```bash
    cd my_directory
    ```
    
* `pwd`  
    Prints the working directory, showing the path of the current directory.
    
    ```bash
    pwd
    ```
    
* `rm <file>`  
    Removes a specified file.
    
    ```bash
    rm file.txt
    ```
    
* `rmdir <directory>`  
    Removes an empty directory.
    
    ```bash
    rmdir my_directory
    ```
    
* `rm -rf <directory>`  
    Forcefully removes a directory and all its contents, including subdirectories and files.
    
    ```bash
    rm -rf my_directory
    ```
    
* `cp -r <source> <destination>`  
    Copies files or directories recursively from the source to the destination.
    
    ```bash
    cp -r folder1/ folder2/
    ```
    
* `mv <source> <destination>`  
    Moves or renames files and directories.
    
    ```bash
    mv file.txt new_file.txt
    ```
    
* `touch <file>`  
    Creates an empty file or updates the timestamp of an existing file.
    
    ```bash
    touch file.txt
    ```
    

### **Viewing and Editing Files**

* `cat <file>`  
    Displays the content of a file.
    
    ```bash
    cat file.txt
    ```
    
* `less <file>`  
    Views a file one page at a time, allowing you to scroll through the contents.
    
    ```bash
    less file.txt
    ```
    
* `head -n <number> <file>`  
    Displays the first `<number>` lines of a file.
    
    ```bash
    head -n 10 file.txt
    ```
    
* `tail -n <number> <file>`  
    Displays the last `<number>` lines of a file.
    
    ```bash
    tail -n 10 file.txt
    ```
    
* `vim <file>`  
    Opens a file in the Vim editor for more advanced text editing.
    
    ```bash
    vim file.txt
    ```
    
* `nano <file>`  
    Opens a file in the Nano editor, which is simpler for beginners.
    
    ```bash
    nano file.txt
    ```
    
* `echo "text" > <file>`  
    Writes "text" to a file, overwriting any existing content.
    
    ```bash
    echo "Hello, World!" > file.txt
    ```
    
* `echo "text" >> <file>`  
    Appends "text" to a file without overwriting existing content.
    
    ```bash
    echo "New Line" >> file.txt
    ```
    

### **Permissions and Execution**

* `chmod <permissions> <file>`  
    Changes the permissions of a file or directory (e.g., `chmod 755 file`).
    
    ```bash
    chmod 755 file.sh
    ```
    
* `chown <owner>:<group> <file>`  
    Changes the owner and group of a file.
    
    ```bash
    chown user:group file.txt
    ```
    
* `./<script>`  
    Executes a script or program in the current directory.
    
    ```bash
    ./script.sh
    ```
    

### **System Monitoring and Information**

* `df -h`  
    Displays disk usage in a human-readable format (e.g., MB, GB).
    
    ```bash
    df -h
    ```
    
* `free -h`  
    Shows memory usage in a human-readable format.
    
    ```bash
    free -h
    ```
    
* `top`  
    Displays real-time system processes, including CPU usage and memory consumption.
    
    ```bash
    top
    ```
    
* `htop`  
    A more interactive and user-friendly version of `top`. Install it first using `sudo apt install htop`.
    
    ```bash
    htop
    ```
    
* `uname -a`  
    Shows detailed system information, including kernel version, machine architecture, and OS type.
    
    ```bash
    uname -a
    ```
    
* `uptime`  
    Displays the current system uptime.
    
    ```bash
    uptime
    ```
    
* `who`  
    Displays who is logged into the system.
    
    ```bash
    who
    ```
    
* `sudo journalctl`  
    Views system logs (requires sudo privileges).
    
    ```bash
    sudo journalctl
    ```
    

### **Networking**

* `ping <host>`  
    Checks the connectivity to a specified host.
    
    ```bash
    ping google.com
    ```
    
* `curl <url>`  
    Fetches data from a specified URL.
    
    ```bash
    curl https://example.com
    ```
    
* `wget <url>`  
    Downloads files from a specified URL.
    
    ```bash
    wget https://example.com/file.zip
    ```
    
* `ifconfig`  
    Displays or configures network interfaces.
    
    ```bash
    ifconfig
    ```
    
* `netstat -tuln`  
    Displays listening ports and associated services.
    
    ```bash
    netstat -tuln
    ```
    

---

## **Git Commands**

Git is a distributed version control system commonly used in DevOps workflows for code management. Here are some essential Git commands with explanations:

### **Configuration**

* `git config --global user.name "Your Name"`  
    Sets your global username for Git commits.
    
    ```bash
    git config --global user.name "John Doe"
    ```
    
* `git config --global user.email "your.email@example.com"`  
    Sets your global email address for Git commits.
    
    ```bash
    git config --global user.email "john.doe@example.com"
    ```
    

### **Repository Management**

* `git init`  
    Initializes a new Git repository in the current directory.
    
    ```bash
    git init
    ```
    
* `git clone <repository-url>`  
    Clones a remote repository to your local machine.
    
    ```bash
    git clone https://github.com/user/repo.git
    ```
    
* `git remote add origin <url>`  
    Adds a remote repository to your local Git configuration.
    
    ```bash
    git remote add origin https://github.com/user/repo.git
    ```
    

### **Basic Operations**

* `git status`  
    Displays the current status of your working directory, showing changes to files.
    
    ```bash
    git status
    ```
    
* `git add <file>`  
    Stages changes to a file for the next commit.
    
    ```bash
    git add file.txt
    ```
    
* `git commit -m "message"`  
    Commits the staged changes with a message describing the changes.
    
    ```bash
    git commit -m "Added new feature"
    ```
    
* `git push`  
    Pushes committed changes to a remote repository.
    
    ```bash
    git push
    ```
    
* `git pull`  
    Fetches and merges changes from a remote repository into your local repository.
    
    ```bash
    git pull
    ```
    

### **Branching and Merging**

* `git branch`  
    Lists all local branches.
    
    ```bash
    git branch
    ```
    
* `git branch <branch-name>`  
    Creates a new branch with the given name.
    
    ```bash
    git branch new-branch
    ```
    
* `git checkout <branch>`  
    Switches to the specified branch.
    
    ```bash
    git checkout new-branch
    ```
    
* `git merge <branch>`  
    Merges the changes from the specified branch into the current branch.
    
    ```bash
    git merge new-branch
    ```
    

---

### **Conclusion**

By mastering these Linux and Git commands, you'll significantly enhance your productivity as a DevOps practitioner. For a more comprehensive understanding of these commands and advanced DevOps practices, keep experimenting with them in real-world scenarios.

For additional resources, check out the [GitHub Cheat Sheet](https://education.github.com/git-cheat-sheet-education.pdf).

---

[Visit my LinkedIn Profile](https://www.linkedin.com/in/amitabh-devops/)  
[Read more on my GitHub](https://github.com/amitabh-devops)

---
