Linux Cheat Sheet
Basic Commands
Command | Description |
ls | Lists all files and directories in the present working directory |
ls -al | Lists files and directories with detailed information like permissions, size, owner, etc. |
cd or cd ~ | Navigate to HOME directory |
cd .. | Move one level up |
cd | To change to a particular directory |
cd / | Move to the root directory |
cat > filename | Creates a new file |
cat filename | Displays the file content |
cat file1 file2 > file3 | Joins two files (file1, file2) and stores the output in a new file (file3) |
mv file "new file path" | Moves the files to the new location |
mv filename new_file_name | Renames the file to a new filename |
sudo | Allows regular users to run programs with the security privileges of the superuser or root |
rm filename | Deletes a file |
man | Gives help information on a command |
history | Gives a list of all past commands typed in the current terminal session |
clear | Clears the terminal |
mkdir directory-name | Creates a new directory in the present working directory or a at the specified path |
rmdir | Deletes a directory |
mv | Renames a directory |
pr -x | Divides the file into x columns |
pr -h | Assigns a header to the file |
pr -n | Denotes the file with Line Numbers |
lp -nc, lpr c | Prints “c” copies of the File |
lp-d lp-P | Specifies the name of the printer |
apt-get | Command used to install and update packages |
mail -s 'subject' -c 'cc-address' | |
-b 'bcc-address' 'to-address' | Command to send email |
mail -s "Subject" to-address < Filename | Command to send an email with an attachment |
ls Options
Command | Description |
-r | Reverse order |
-R | Recursive list |
-a | Show all (including hidden) |
-t | Sort by last modified |
-S | Sort by file size |
-l | Long listing format |
-1 | One file per line |
-m | Comma-separated output |
-Q | Quoted output |
System Information Commands
Command | Description |
hostname | Shows the name of the system host. |
hostid | Displays the id of the host of the system. |
apt-get | This command is used to install and add new packages. |
date | This command is used to show the current date and time. |
cal | Shows the calendar of the current month. |
whoami | This command displays the name with which you are logged in. |
whereis [options] fileName | This command is used to find the location of source/binary file of a command and manuals sections for a specified file in Linux System. This command is similar to the find command but this command is faster as it produces more accurate results by taking less time compared to the find command. There are again a number of options available. |
File Permission Commands
Command | Description |
r | read permission |
w | write permission |
x | execute permission |
-= | no permission |
chmod 777 filename | Assign full(read, write, and execute) permission to everyone |
chmod -R 777 dirname | Assign full permission to the directory and all sub-directories |
chmod 766 filename | Assign full permission to the owner, and read and write permission to group and others |
chmod -x filename | Remove the execution permission of any file |
chown username filename | Change the ownership of a file |
chown user:group filename | Change the owner and group ownership of a file |
chown -R user:group dirname | Change the owner and group ownership of the directory and all sub-directories |
Disk Management Commands
Command | Description |
fdisk -l | List all disk partitions |
fdisk /dev/sda | Create a new partition on /dev/sda device |
mkfs.ext4 /dev/sda1 | Format the partition named /dev/sda1 |
fsck.ext4 /dev/sda1 | Check and repair a filesystem for any error |
mount /dev/sda1 /mnt | Mount any partition to any directory |
df -h | Display free space of mounted file system |
df -i | Display free inodes on the filesystem |
du -hs | Display the size of your current directory |
lsblk | Display information about block devices |
lsusb -tv | Display all USB devices |
hdparm -tT /dev/sda | Perform a read speed test on disk /dev/sda |
badblocks -s /dev/sda | Test for unreadable blocks on disk /dev/sda |
top | Display and manage the top processes |
htop | Interactive process viewer (top alternative) |
mpstat 1 | Display processor related statistics |
vmstat 1 | Display virtual memory statistics |
iostat 1 | Display I/O statistics |
tail -100 /var/log/messages | Display the last 100 syslog messages (Use /var/log/syslog for Debian based systems.) |
tcpdump -i eth0 | Capture and display all packets on interface eth0 |
tcpdump -i eth0 'port 80' | Monitor all traffic on port 80 ( HTTP ) |
lsof | List all open files on the system |
lsof -u user | List files opened by user |
free -h | Display free and used memory ( -h for human readable, -m for MB, -g for GB.) |
watch df -h | Execute "df -h", showing periodic updates |
User Management Commands
Command | Description |
sudo adduser username | To add a new user |
sudo passwd -l 'username' | To change the password of a user |
sudo userdel -r 'username' | To remove a newly created user |
sudo usermod -a -G GROUPNAME USERNAME | To add a user to a group |
sudo deluser USER GROUPNAME | To remove a user from a group |
finger | Shows information of all the users logged in |
finger username | Gives information of a particular user |
File and Directory Compression Commands
Command | Description |
gzip fileName | This command is used to compress a file with gzip compression. |
gunzip fileName.gz | This command is used to unzip a file that has gzip compression. |
tar cf myDir.tar myDir | This command is used to create an uncompressed tar archive. |
tar cfz myDir.tar myDir | This command is used to create a tar archive with gzip compression. |
tar xf file | This command is used to extract the contents of any type of tar archive. |
tar -cvf filename.tar filename | Compress a file in the Tar archive |
tar -xvf filename.tar | Uncompress a Tar file |
tar -tvf filename.tar | List the content of the Tar file |
tar -xvf filename.tar file1.txt | Untar a single file from Tar file |
tar -rvf filename.tar file2.txt | Add a file to the Tar file |
zip filename.zip filename | Compress a single file to a zip |
zip filename.zip file1.txt file2.txt file3.txt | Compress multiple files to a zip |
zip -u filename.zip file4.txt | Add a file to a zip file |
zip -d filename.zip file4.txt | Delete a file from a zip file |
unzip -l filename.zip | Display the content of zip archive file |
unzip filename.zip | Unzip a file |
unzip filename.zip -d /dirname | Unzip a file to a specific directory |
Networking command
Command | Description | |
SSH username@ip-address or hostname | login into a remote Linux machine using SSH | |
Ping hostname="" or ="" | To ping and Analyzing network and host connections | |
dir | Display files in the current directory of a remote computer | |
cd "dirname" | change directory to “dirname” on a remote computer | |
put file | upload ‘file’ from local to remote computer | |
get file | Download ‘file’ from remote to local computer | |
quit | Logout |
Process command
Command | Description | |
bg | To send a process to the background | |
fg | To run a stopped process in the foreground | |
top | Details on all Active Processes | |
ps | Give the status of processes running for a user | |
ps PID | Gives the status of a particular process | |
pidof | Gives the Process ID (PID) of a process | |
kill PID | Kills a process | |
nice | Starts a process with a given priority | |
renice | Changes priority of an already running process | |
df | Gives free hard disk space on your system | |
free | Gives free RAM on your system |
Git & Github Cheat Sheet
Git configuration
Git config
Get and set configuration variables that control all facets of how Git looks and operates.
Set the name:
git config --global
user.name
"UserName"
Set the email:
git config --global
user.email
"
yourEmail@gmail.com
"
Set the default editor:
git config --global core.editor Vim
Check the setting:
git config -list
Git alias
Set up an alias for each command:
git config --global
alias.co
checkout git config --global
alias.br
branch git config --global
alias.ci
commit git config --global
alias.st
status
Starting a project
Git init
Create a local repository:
git init
Git clone
Make a local copy of the server repository.
git clone
Local changes
Git add
Add a file to staging (Index) area:
git add Filename
Add all files of a repo to staging (Index) area:
git add .
Git commit
Record or snapshots the file permanently in the version history with a message.
git commit -m " Commit Message"
Track changes
Git diff
Track the changes that have not been staged:git diff
Track the changes that have been staged but not committed:
git diff --staged
Track the changes after committing a file:
git diff HEAD
Track the changes between two commits:
git diff <commit hash 1> <commit hash 2>
Git status
Display the state of the working directory and the staging area.
git status
Git show Shows objects:
git show
Commit History
Git log
Display the most recent commits and the status of the head:
git log
Display the output as one commit per line:
git log -oneline
Displays the files that have been modified:
git log -stat
Display the modified files with location:
git log -p
Git blame
Display the modification on each line of a file:
git blame <file name>
Ignoring files
.gitignore
Specify intentionally untracked files that Git should ignore.Create .gitignore:
touch .gitignore
List the ignored files:
git ls-files -i --exclude-standard
Branching
Git branch Create branch:
git branch List Branch: git branch --list Delete a Branch: git branch -d Delete a remote Branch: git push origin -delete Rename Branch: git branch -m
Git checkout
Switch between branches in a repository.
Switch to a particular branch:
git checkout branch_name
Create a new branch and switch to it:
git checkout -b branch_name
Git stash
Switch branches without committing the current branch. Stash current work:
git stash
Saving stashes with a message:
git stash save ""
Check the stored stashes:
git stash list
Re-apply the changes that you just stashed:
git stash apply
Track the stashes and their changes:
git stash show
Re-apply the previous commits:
git stash pop
Delete a most recent stash from the queue:
git stash drop
Delete all the available stashes at once:
git stash clear
Stash work on a separate branch:
git stash branch
Git cherry pic
Apply the changes introduced by some existing commit:
git cherry-pick commit_id
Merging
Git merge
Merge the branches:
git merge
Merge the specified commit to the currently active branch:
git merge
Git rebase
Apply a sequence of commits from distinct branches into a final commit.
git rebase
Continue the rebasing process:
git rebase -continue
Abort the rebasing process:
git rebase --skip
Git interactive rebase
Allow various operations like edit, rewrite, reorder, and more on existing commits.
git rebase -i
Remote
Git remote
Check the configuration of the remote server:
git remote -v
Add a remote for the repository:
git remote add
Fetch the data from the remote server:
git fetch
Remove a remote connection from the repository:
git remote rm
Rename remote server:
git remote rename
Show additional information about a particular remote:
git remote show
Change remote:
git remote set-url
Git origin master
Push data to the remote server:
git push origin master
Pull data from remote server:
git pull origin master
Push
Git push
Transfer the commits from your local repository to a remote server.Push data to the remote server:
git push origin master
Force push data:
git push -f
Delete a remote branch by push command:
git push origin --delete branch_name
Pull
Git pull
Pull the data from the server:
git pull origin master
Pull a remote branch:
git pull
Git fetch
Download branches and tags from one or more repositories.Fetch the remote repository:
git fetch <repository Url>
Fetch a specific branch:
git fetch
Fetch all the branches simultaneously:
git fetch -all
Synchronize the local repository:
git fetch origin
Undo changes
Git revert
Undo the changes:
git revert
Revert a particular commit:
git revert <commit hash>
Git reset
Reset the changes:git reset -hard git reset -soft: git reset --mixed
Removing files
- Git rm
Remove the files from the working tree and from the index:
git rm <file Name>
Remove files from the Git But keep the files in your local repository:
git rm --cached
Thank you for reading!!
~Shreya Gupta
Great initiative by the #trainwithshubham community. Thank you Shubham Londhe
#devops #90daysofdevops #linux #git #github