▶ Linux Cheat Sheet - Basic Commands
ls
Show all files and directories present in the working directory
ls -l
Show all files and directories along with details like ownership, size, and when they were last modified
ls -t
Sort the output of ls by the later modified date
pwd
List the path of the current working directory
cd directory
Switch to a different working directory
cd ..
Move up one directory level
cd /
Switch to the root directory
clear
Wipe out all the text from the terminal screen
history
Print out all the previously executed commands in the current session
touch filename
Create a new file
vi filename
Open a file for editing
cat filename
Display the contents of a file
hostnamectl
List system information, including kernel, release, architecture, virtualization, etc.
ifconfig
Show the network interfaces, IP addresses, and MAC addresses of the system
date
Print the current system date and time
top
List all the running processes on the system
free -m
Show memory usage stats
head filename
Display the first 10 lines of a file
tail filename
Display the last 10 lines of a file
mv file /new/file/path
Move the file to a different location
mv filename new_filename
Rename a file
cp filename new_filename
Copy a file
man command_name
List helpful information regarding a command
rm filename
Delete a file
rm –rf directory_name
Remove a directory and its contents
sudo
For regular users to run commands with elevated privileges
mkdir directory_name
Create a new directory
kill pid
Kill a process using its process ID
reboot
Restart the system
shutdown –h now
Turn off the system
Networking Commands
dig domain_name
Show the DNS related information of the given domain name
dig -x host
Perform a reverse lookup for a host
host domain_name
Perform DNS lookup of the given domain name which prints out the IP address
whois domain_name
For more information about a domain
ping IP
Check the connectivity between your host and the given IP address
ssh username@IP
To securely log in as the specified user on another server
wget file
Download a file
wget -c file
Continue a stopped download
traceroute domain_name
Trace the route a packet will take when travelling from your machine to a host
telnet domain_name port
Connect to a remote host on a specific port
netstat –pnltu
Display all currently listening ports
route
Display the routing table for your machine
arp
View the contents of the ARP (address resolution protocol) table
cat /etc/resolv.conf
See the DNS servers that your machine is currently using
tcpdump -i eth1 'port 80'
Log and monitor all incoming traffic on port 80
nmap IP
Network discovery of the given IP. Find out whether the host is up, which ports are open etc.
Permissions Commands
chmod xxx filename
Assign the specified permissions to a file
chmod –R xxx directory
Assign the specified permissions to a directory, and all its sub-directories
chmod –x filename
Remove the execution permissions from a file
chown username filename
Change the ownership of the specified file
chown username:groupname filename
Change the ownership and the group ownership of a file
chown username:groupname filename1 filename2 filename3
Change the ownership and the group ownership of multiple files
chown --from=bob alice filename
Change the ownership of a file only if it’s owned by a specific user (added after --from=) chown
chown -h usergroup symbolic_link
Forcefully change the owner and group of a symbolic link
User Management Commands
adduser username
Add a new user
userdel -r 'username'
Delete a user
passwd -l 'username'
Change the password of a user
whoami
See the currently logged in user
usermod -c 'This user will be deleted tomorrow' username
To add a comment to a user account
cat /etc/passwd
Display a list of all users with additional info
usermod -d /home/test username
Change the home directory of a user
sudo deluser username group_name
Remove a user from a group
usermod -a -G group_name username
Add a user to a group
groupadd group_name
Create a new group
groupdel group_name
Delete a group
id
Display the user ID, group ID, and groups for the current user
Archive Commands
tar -cvf archive.tar filename.txt
Compress a file into a tar archive
tar -tvf archive.tar
Display the contents of a tar archive
tar -xvf archive.tar filename.txt
Extract a single file out of a tar archive
zip
archive.zip
file1.txt file2.html file3.jpg
Create a zip archive using multiple files
zip -u
archive.zip
filename.txt
Add a file to an already zipped file
zip -d
archive.zip
filename.txt
Delete a file from a zip archive
unzip
archive.zip
Unzip a zip archive
unzip
archive.zip
-d /directory_name
Unzip a zip archive to a specific directory
tar xf archive
Extract archive of any type
gzip filename
Compress a file and add the .gz extension to it. This will delete the original file
gzip -c filename > archive.gz
Create a new compressed .gz file, preserving the original
Advanced Commands
ps –ef | grep process_name
Check whether a specific process is running
netstat –pnltu | grep port
Check whether a specific port is open
history | grep keyword
Search for something in the history
ip r
Display the IP address of the default gateway
ssh-keygen
Generate SSH keys to allow public key based secure login
scp filename user@ip:/home/location
Securely copy a file to a remote host
find / -type f -perm 777 -print -exec chmod xxx {} ;
Find all the files that have 777 permissions, and modify them to xxx
sed -i 's/CONTENT_TO_REPLACE/CONTENT_TO_REPLACE_IT_WITH/g' filename
Replace the specified content in a file with something else
tcpdump -nnvvS src SRC_IP and dst port xxxx
Monitor all traffic emerging from a certain IP and going towards a certain port
traceroute domain_name -q 5
Send 5 packets per hop while tracing the route to the given domain name
▶Git Cheat Sheet - Basic Commands
How to check your Git configuration: The command below returns a list of information about your git configuration including user name and email:
git config -l
How to setup your Git username: With the command below you can configure your user name:
git config --global
user.name
"xxxx"
How to setup your Git user email: This command lets you setup the user email address you'll use in your commits.
git config --global
user.email
"
xxxx@xxxx.com
"
How to cache your login credentials in Git: You can store login credentials in the cache so you don't have to type them in each time. Just use this command:
git config --global credential.helper cache
How to initialize a Git repo: Everything starts from here. The first step is to initialize a new Git repo locally in your project root. You can do so with the command below:
git init
How to add a file to the staging area in Git: The command below will add a file to the staging area. Just replace filename_here with the name of the file you want to add to the staging area.
git add filename_here
How to add all files in the staging area in Git If you want to add all files in your project to the staging area, you can use a wildcard . and every file will be added for you.
git add .
How to add only certain files to the staging area in Git With the asterisk in the command below, you can add all files starting with 'fil' in the staging area.
git add fil*
How to check a repository's status in Git: This command will show the status of the current repository including staged, unstaged, and untracked files.
git status
How to commit changes in the editor in Git: This command will open a text editor in the terminal where you can write a full commit message. A commit message is made up of a short summary of changes, an empty line, and a full description of the changes after it.
git commit
How to commit changes with a message in Git: You can add a commit message without opening the editor. This command lets you only specify a short summary for your commit message.
git commit -m "your commit message here"
How to commit changes (and skip the staging area) in Git: You can add and commit tracked files with a single command by using the -a and -m options.
git commit -a -m"your commit message here"
How to see your commit history in Git: This command shows the commit history for the current repository:
git log
How to see your commit history including changes in Git: This command shows the commit's history including all files and their changes:
git log -p
How to see a specific commit in Git: This command shows a specific commit. Replace commit-id with the id of the commit that you find in the commit log after the word commit.
git show commit-id
How to see log stats in Git: This command will cause the Git log to show some statistics about the changes in each commit, including line(s) changed and file names.
git log –stat
How to see changes made before committing them using "diff" in Git: You can only pass a file as a parameter to see changes on a specific file. git diff shows only unstaged changes by default. We can call diff with the --staged flag to see any staged changes.
git diff
git diff all_checks.py
git diff --staged
How to see changes using "git add -p": This command opens a prompt and asks if you want to stage changes or not and includes other options.
git add -p
How to remove tracked files from the current working tree in Git: This command expects a commit message to explain why the file was deleted.
git rm filename
How to rename files in Git: This command stages the changes, then it expects a commit message.
git mv oldfile newfile
How to ignore files in Git: Create a .gitignore
file and commit it.
How to revert unstaged changes in Git:
git checkout filename
How to revert staged changes in Git: You can use the -p option flag to specify the changes you want to reset.
git reset HEAD filename
git reset HEAD -p
How to amend the most recent commit in Git: git commit --amend allows you to modify and add changes to the most recent commit.
git commit –amend
!!Note!!: fixing up a local commit with amend is great and you can push it to a shared repository after you've fixed it. But you should avoid amending commits that have already been made public.
How to rollback the last commit in Git: git revert will create a new commit that is the opposite of everything in the given commit. We can revert the latest commit by using the head alias like this:
git revert HEAD
How to rollback an old commit in Git: You can revert an old commit using its commit id. This opens the editor so you can add a commit message.
git revert comit_id_here
How to create a new branch in Git: By default, you have one branch, the main branch. With this command, you can create a new branch. Git won't switch to it automatically – you will need to do it manually with the next command.
git branch branch_name
How to switch to a newly created branch in Git: When you want to use a different or a newly created branch you can use this command:
git checkout branch_name
How to list branches in Git: You can view all created branches using the git branch command. It will show a list of all branches, mark the current branch with an asterisk, and highlight it in green.
git branch
How to create a branch in Git and switch to it immediately: You can create and switch to a new branch in a single command immediately.
git checkout -b branch_name
How to delete a branch in Git: When you are done working with a branch and have merged it, you can delete it using the command below:
git branch -d branch_name
How to merge two branches in Git: To merge the history of the branch you are currently in with the branch_name, you will need to use the command below:
git merge branch_name
How to show the commit log as a graph in Git: We can use --graph to get the commit log to show as a graph. Also, --oneline will limit commit messages to a single line.
git log --graph --oneline
How to show the commit log as a graph of all branches in Git: Does the same as the command above, but for all branches.
git log --graph --oneline –all
How to abort a conflicting merge in Git: If you want to throw a merge away and start over, you can run the following command:
git merge –abort
How to add a remote repository in Git This command adds a remote repository to your local repository (just replace https://repo_here with your remote repo URL).
git add remote https://repo_here
How to see remote URLs in Git: You can see all remote repositories for your local repository with this command:
git remote -v
How to get more info about a remote repo in Git: Just replace origin with the name of the remote obtained by running the git remote -v command.
git remote show origin
How to push changes to a remote repo in Git: When all your work is ready to be saved on a remote repository, you can push all changes using the command below:
git push
How to pull changes from a remote repo in Git: If other team members are working on your repository, you can retrieve the latest changes made to the remote repository with the command below:
git pull
How to check remote branches that Git is tracking: This command shows the name of all remote branches that Git is tracking for the current repository:
git branch -r
How to fetch remote repo changes in Git: This command will download the changes from a remote repo but will not perform a merge on your local branch (as git pull does that instead).
git fetch
How to check the current commits log of a remote repo in Git Commit after commit, Git builds up a log. You can find out the remote repository log by using this command:
git log origin/main
How to merge a remote repo with your local repo in Git: If the remote repository has changes you want to merge with your local, then this command will do that for you:
git merge origin/main
How to get the contents of remote branches in Git without automatically merging: This lets you update the remote without merging any content into the local branches. You can call git merge or git checkout to do the merge.
git remote update
How to push a new branch to a remote repo in Git: If you want to push a branch to a remote repository you can use the command below. Just remember to add -u to create the branch upstream:
git push -u origin branch_name
How to remove a remote branch in Git: If you no longer need a remote branch you can remove it using the command below:
git push --delete origin branch_name_here
How to use Git rebase: You can transfer completed work from one branch to another using git rebase.
git rebase branch_name_here
Git Rebase can get really messy if you don't do it properly.
How to run rebase interactively in Git: You can run git rebase interactively using the -i flag. It will open the editor and present a set of commands you can use.
git rebase -i master
# p, pick = use commit
# r, reword = use commit, but edit the commit message
# e, edit = use commit, but stop amending
# s, squash = use commit, but meld into the previous commit
# f, fixup = like "squash", but discard this commit's log message
# x, exec = run command (the rest of the line) using shell
# d, drop = remove commit
How to force a push request in Git: This command will force a push request. This is usually fine for pull request branches because nobody else should have cloned them. But this isn't something that you want to do with public repos.
git push -f
Conclusion
These commands can dramatically improve your productivity in Linux and Git. You don't have to remember them all – that's why I have written this cheat sheet. Bookmark this page for future reference or print it if you like.