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.ls
ls -l
Lists files and directories with detailed information, such as file permissions, number of links, owner, group, size, and modification date.ls -l
mkdir <directory>
Creates a new directory. Replace<directory>
with the name of the directory you want to create.mkdir my_directory
cd <directory>
Changes the current working directory to the specified directory.cd my_directory
pwd
Prints the working directory, showing the path of the current directory.pwd
rm <file>
Removes a specified file.rm file.txt
rmdir <directory>
Removes an empty directory.rmdir my_directory
rm -rf <directory>
Forcefully removes a directory and all its contents, including subdirectories and files.rm -rf my_directory
cp -r <source> <destination>
Copies files or directories recursively from the source to the destination.cp -r folder1/ folder2/
mv <source> <destination>
Moves or renames files and directories.mv file.txt new_file.txt
touch <file>
Creates an empty file or updates the timestamp of an existing file.touch file.txt
Viewing and Editing Files
cat <file>
Displays the content of a file.cat file.txt
less <file>
Views a file one page at a time, allowing you to scroll through the contents.less file.txt
head -n <number> <file>
Displays the first<number>
lines of a file.head -n 10 file.txt
tail -n <number> <file>
Displays the last<number>
lines of a file.tail -n 10 file.txt
vim <file>
Opens a file in the Vim editor for more advanced text editing.vim file.txt
nano <file>
Opens a file in the Nano editor, which is simpler for beginners.nano file.txt
echo "text" > <file>
Writes "text" to a file, overwriting any existing content.echo "Hello, World!" > file.txt
echo "text" >> <file>
Appends "text" to a file without overwriting existing content.echo "New Line" >> file.txt
Permissions and Execution
chmod <permissions> <file>
Changes the permissions of a file or directory (e.g.,chmod 755 file
).chmod 755 file.sh
chown <owner>:<group> <file>
Changes the owner and group of a file.chown user:group file.txt
./<script>
Executes a script or program in the current directory../script.sh
System Monitoring and Information
df -h
Displays disk usage in a human-readable format (e.g., MB, GB).df -h
free -h
Shows memory usage in a human-readable format.free -h
top
Displays real-time system processes, including CPU usage and memory consumption.top
htop
A more interactive and user-friendly version oftop
. Install it first usingsudo apt install htop
.htop
uname -a
Shows detailed system information, including kernel version, machine architecture, and OS type.uname -a
uptime
Displays the current system uptime.uptime
who
Displays who is logged into the system.who
sudo journalctl
Views system logs (requires sudo privileges).sudo journalctl
Networking
ping <host>
Checks the connectivity to a specified host.ping google.com
curl <url>
Fetches data from a specified URL.curl https://example.com
wget <url>
Downloads files from a specified URL.wget https://example.com/file.zip
ifconfig
Displays or configures network interfaces.ifconfig
netstat -tuln
Displays listening ports and associated services.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.git config --global user.name "John Doe"
git config --global user.email "your.email@example.com"
Sets your global email address for Git commits.git config --global user.email "john.doe@example.com"
Repository Management
git init
Initializes a new Git repository in the current directory.git init
git clone <repository-url>
Clones a remote repository to your local machine.git clone https://github.com/user/repo.git
git remote add origin <url>
Adds a remote repository to your local Git configuration.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.git status
git add <file>
Stages changes to a file for the next commit.git add file.txt
git commit -m "message"
Commits the staged changes with a message describing the changes.git commit -m "Added new feature"
git push
Pushes committed changes to a remote repository.git push
git pull
Fetches and merges changes from a remote repository into your local repository.git pull
Branching and Merging
git branch
Lists all local branches.git branch
git branch <branch-name>
Creates a new branch with the given name.git branch new-branch
git checkout <branch>
Switches to the specified branch.git checkout new-branch
git merge <branch>
Merges the changes from the specified branch into the current branch.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.
Visit my LinkedIn Profile
Read more on my GitHub