Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
15 views

Linux Commands CheatSheet - PDF

Uploaded by

abhikumbhar1908
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

Linux Commands CheatSheet - PDF

Uploaded by

abhikumbhar1908
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 34

Alphabet Command name Details Comment

awk '{print $2}' file_name (it will print 2 column


content)
awk '{print $NF}' (it will print last column)
awk '/string/ {print}' file_name
awk used for data extraction from a file, mostly use for column extraction from a file
awk '{if($3 >300) print $0}' file_name
awk '{if($3==333){$1="Sonu"} print $0 }' text
awk 'length($0)>15' text
awk -F, '{print $7}' country.txt (csv file , as delimeter)
after this
command,
alias d="ls -ltr | awk '{print \$9}'" next time
alias to create shortcut to the command or task alias -p (to check the existing alias) you just
need to
execute 'd'
at 06:43 (then it will stuck in editing mode where you can
define task like
commands or script name and CTRL+D to finish)

at 09:00 AM SUN
at command used to schedule a task once at 09:00 AM AUG 30
at 09:00 AM 06.22.2024
at 09:00 AM tomorrow

atq to list existing jobs


atrm <id> to remove the jobs
A arch to check linux is 64 or 32 bit
bc binary calculator bc
bash to run a bash script bash script.sh
basename /usr/local/bin/script.sh
basename Strips directory and suffix from filenames. Outputs script.sh.
sleep 50
Ctrl + z to stop
bg to send a stopped job in bg to run jobs to see this job
B bg (it will start job in background)
show content of file cat file_name
cat cat file1 file2 > file3
concatenate two files
cat /proc/cpuinfo gives you info about your CPU resources
cat /proc/meminfo gives you infor about infor and its usage in details
cat /etc/redhat-release to check the linux version
cal
cal show you calendar cal 1993 (full calculator of that year)
cal -1 1993 (to show month in 1993)
cd change directory
chmod o-r file_name ( - to remove permission)
chmod change mode, to modify permissions of files chmod g+rw file_name. ( + to add permission)
chmod u+x file_name. ( u - user, g - group, o - other, a = all)
chown change ownership chown root/user_name folder/file_name
chage -l paul (show all he details about account like password
expires etc)
chage We can use this command to chnage the password expiry
chage -E 0 user_anme (To disable a account)
chage -E -1 user_name (To enable a account)
chgrp chnage group chgrp root/user_name folder/file_name
chrontab -e (for editing or creating new file_
to run a command or script on given schedule (cant use as root) Example of a simple cron job is:
crontab To generate cron job http://www.cronmaker.com/ * * * * * cd /home/paul/scripts && ./create_file.sh
To set timezone use CRON_TZ=America/New_York
chrontab -l (to show all the cron jobs)
cmp compare the content of files byte by byte cmp file1 file2
to check if the webpage is up or not, will result into the webpage sourcecode curl www.google.com
curl curl -O <download link>
we can also download the software on linux
cut -c2 file_name
cut -c1-4 file_name
to filter out specific characters from the result of output or file, also useful when you want to
cut cut -c2,3 file_name
see only one or two column of a file cut -d: -f 3 file_name (will show 3rd column separated
with : delimiter
cp source_file destination_file
cp use to copy a file from one location to another cp -p source_file destination_file (if we use -p then it will
copy the file with original time when it was last modified)
C createrepo
date
date to see the current date and time of system date -s "28 Jun 2021 23:06:00 IST"
date +%F-%N (result: 2022-07-01-011018377)
dmesg print or control the kernel ring buffer dmesg
diff compare the content of files line by line diff file1.txt file2.txt
df report file system disk space usage df -h (h for human readable)
dmidecode print system hardware info dmidecode (only as root)
dnf Package manager for RPM-based distributions, successor to yum. (new version of linux) sudo dnf install httpd
dig DNS lookup utility dig www.google.com
dirname /usr/local/bin/script.sh
dirname Strips the last component from a file name.
Outputs /usr/local/bin
du <file or folder location> (It will show all files size inside
that locaion)
du file usage, disk utilization du -sh <file or folder location> (-h is human readable and only
D show the size of given folder)
egrep to search for a particular string/keyword from a file egrep "key1|key2" filename
echo Print something on terminal echo "hello"
to show env variables on linux

to set variable for the current user - .bashrc To set the environement variable you need to use export command
env
to set variable for globally - /etc/bashrc or /etc/profile

then you need to fire 'source' command

E
ethtool print NIC info ethtool enps03
exit Exits the shell or a script. Close the current terminal (logout)
export TEST=1
export used to set env vairable temporary to check, echo $TEST
E
find /tmp/ -name folder_name

find /path/to/search -type f -size +100M (find file based on


find to search for a directory or file size)
find /path/to/search -mtime -7 (find file based on
modified time, 7 days)

file to check the type of file file <file_name>


free to check the free space free -h
fdisk give info about the disk availability fdisk -l
fg to bring the running job in foreground fg
echo "ABC" | fold -w1 (w is how much width of each row)
output is:
fold to fold a line into a column A
B
C
Example: fgrep 'text' file.txt
ftp File Transfer Protocol client. Usage: Searches for the fixed string text in file.txt. Useful
for beginners to search for exact matches in files.
fmt Simple text formatter. fmt myfile.txt
Example: sudo fsck /dev/sda1
fsck Filesystem consistency check and repair. Usage: Checks and repairs the filesystem on /dev/sda1. Useful
for beginners to maintain filesystem integrity.
firewall-cmd --add-port=8080/tcp --permanent
firewall-cmd --zone=public --add-service=http --permanent
firewall-cmd firewall settings firewall-cmd --reload

F firewall-config it will open GUI to configure firewall settings


grep keyword filename
grep -i keyword (to ignore the upper or lower cases)
grep to search for a particular string/keyword from a file grep -v keyword (search other than given keyword)

groupadd mygroup
groupadd to add group check in /etc/group file
groupdel to delete group
Example: groups username
groups Shows the groups a user is a member of. Usage: Lists all the groups that username belongs to. Useful for
beginners to check group memberships.
Example: getenforce
getenforce Displays the current SELinux mode. Usage: Shows whether SELinux is enforcing, permissive, or
disabled. Useful for beginners to check SELinux status.
Example: getfacl filename
getfacl Gets file access control lists (if installed). Usage: Displays the ACLs of filename. Useful for beginners to
view detailed file permissions.

G
gzip name.tar
gzip, gunzip compressing and decompressing, usually we apply this on tar file gunzip name.tar.gz or gzip -d name.tar.gz
G
head gives you starting lines head -2 file_name
hostname show your hostname hostname
hostnamectl to control and change the hostname hostnamectl set-hostname <new_hostname>
history Lists all the commands that have been entered in the current shell session. $history
htop An interactive process viewer (if installed).
help Help to show you how to use a command help cd
H halt Stops the system.
ip addr show ip address and network related info
ifconfig show ip address and network related info
Report Central Processing Unit (CPU) statistics and input/output statistics for devices and
iostat
partitions.
Usage: Shuts down the system (runlevel 0). Beginners should use
init initi 0-6, 0-6 are different level at which we need to reboot this with caution as it affects the system's state.
I id show group and user detail of a user id user_name
jobs to see the current jobs and its state jobs
Usage: Displays the most recent log entries with detailed
journalctl Queries and displays logs from systemd's journal. information. Useful for beginners to troubleshoot system issues
J and view log files.
kill PID
kill to kill any process kill -9 PID
Example: killall httpd
killall Kills all processes by the specified name. Usage: Terminates all processes named httpd.
K
last show listing of last logged users last
less it show you file nicely manner page vise less file_name
ls
ls -ltr
ls to list the file ls -la (to see hidden files)
ls -i (to see the inode)
ln
ln to create hard or soft link ln -s source_file name_of_link
lscpu to check the linux machine sepec
lsblk Displays a tree of block devices.
lsmod Lists all currently loaded kernel modules.
L locate its like find command but its search in its own db locate filename (to search this file on system)
mkdir to create a directory mkdir folder_name
more it gives you result one page at a time (in case if the output list is very big) ls -ltr | more
man to read more about a command (man cd) will give manual on cd command
Example: sudo mount /dev/sda1 /mnt
mount Mounts a filesystem. Usage: Mounts the filesystem on /dev/sda1 to the /mnt directory.
Useful for beginners to access filesystems and external drives.
M mv to move or rename a file mv filename newname
nohup sleep 50
nohup to run a process in backgroup even if user close his terminal nohup sleep 50 > /dev/null 2>&1 &
nice to assign a process with priority nice -n 5 process_name (-19 to 20 will be priority)4
nano text editor to create, edit your files nano file_name
Example: nslookup example.com
nslookup Queries Internet name servers interactively and DNS record Usage: Retrieves the DNS records for example.com.
Example: nmcli device status
nmcli Command-line client for NetworkManager. Usage: Shows the status of network devices. Useful for beginners
to manage network connections.
Shows the number of processing units available. Example: nproc
nproc Usage: Displays the number of CPU cores available. Useful for
beginners to understand system resources.
netstat -rnv

netstat -tunlp (t-tcp, u-udp, n-no.of port, l-listening port, p-


Print network connections, routing tables, interface statistics, masquerade connections, and PID programname)
netstat
multicast memberships netstat -4tunlp (will only show TCPv4 and exclude the TCPv6)

To check no. of connection from a specific IP


N netstat -an | grep <IP>
Example: od -c file.txt
od Dumps files in octal and other formats. Usage: Displays the content of file.txt in ASCII characters.
Example: openssl genpkey -algorithm RSA -out private_key.pem
openssl A toolkit for the Transport Layer Security (TLS) and Secure Sockets Layer (SSL) protocols. Usage: Generates a private key using RSA. Useful for beginners
O to manage SSL/TLS certificates and keys.
pwd print working dir pwd
passwd to change password of a user passwd userid
ps to check if a given process is running or not ps -ef | grep <service/process_name>
printenv to show the env variables
Example: sudo pskill firefox
pskill Terminates processes by name Usage: Kills all processes with the name firefox. Useful for
beginners to terminate specific applications.
Example: pv file.txt | gzip > file.txt.gz
pv Monitors the progress of data through a pipeline (if installed). Usage: Shows progress of compressing file.txt to file.txt.gz.
Useful for beginners to visualize data processing.
Example: sudo parted /dev/sda
parted Manages disk partitions. Usage: Opens the partition editor for the disk /dev/sda. Useful
for beginners to create, delete, and modify partitions.
ping ip
ping used to check the network connectivity ping -c 1 ip (only try once)
P
Q quota Displays disk usage and limits.
Scans a filesystem for disk usage, creates, and updates quota files.
quotacheck
rm file_name
rm to remove/delete folder/file rm -rf folder_name
rpm -qa (to list all the installed packages)
rpm reh hat package manager, used to list, install or uninstall packages rpm -e <package_name> (to uninstall any package)
rsync -avz source destination
rsync to synchronized and transfer the data on local system or between local and remote system rsync -avzh destination source (from remote)
renice To change the priority of already running program renice -n 5 process_name
runlevel to check the current system run-level
Example: rename 's/.txt/.bak/' *.txt
rename Renames multiple files. Usage: Renames all .txt files to .bak. Useful for beginners to
rename files in bulk.
Example: route -n
route Shows/manipulates the IP routing table (deprecated, use ip route instead). Usage: Displays the routing table. Useful for beginners to view
network routes.
R reboot halt, poweroff, reboot - Halt, power-off or reboot the machine
to record your activity until you exit script <file_name>
script it will store all the activity a user perform and store in a file so you can keep ther record of the and start your activity
over all activity once done, hit exit Ctrl+D
sed 's/<string_to_change>/<new_string>/g' file_name (only
show change)
sed -i 's/Prashant/Potty/g' file_name (change in file)
sed -i '5!s/Prashant/Potty/g' file_name (change everywhere
except line 5)
sed -i 's/Prashant//g' file_name (just to remove word)
sed replace string, find/delete line, remove empty lines sed '1d' file_name (to delete first line)
sed '1,2d' file_name
sed '/s\t/ /g' (to replace tab with space)
sed -n '3,5p' text (to show only 3 to 5 lines)
sed -n '3,4d' text (to delete 3 to 5 lines and show)
sed -n '$d' file_name ($ is last line)
sed -i '/^$/d' text (to remove empty lines)
generate report sos report
sosreport
collect and diagnostic support data and package, it will create a zip file under /var/tmp
split split any files into different small files based on given line no. split -l 300 file_name
sort file (sort based on starting no. or letter) Example of
sort -r file (Reverse sort) sort -h
sort -k2 file (k is column)
99K
If you are sorting number (not based on starting no. but 8M
weightage) 96K
sort sort the conent of file sort -n file_name
Sorting will
sort -h (human redable no.) be
96K
To sort only unique no. 99K
sort -un file 8M
sudo -iu paul
sudo to switch the user or execute a command which need superuser access sudo yum install nginx
A
B
C

shuf to shuffle each line within a file or stdin cat file | shuf

B
C

ssh Secure Shell, used to connect to remote servers. Example: ssh user@hostname
Example: ssh-keygen -t rsa -b 2048
ssh-keygen Generates, manages, and converts authentication keys for SSH. Usage: Generates an RSA key pair with 2048-bit encryption.
Useful for beginners to set up SSH key-based authentication.
Example: scp file.txt user@remote_host:/path/to/destination
scp Secure copy, used to copy files to remote servers Usage: Copies file.txt to remote_host under
/path/to/destination.
shutdown shutdown your server, Halt, power-off or reboot the machine
Example: strings binaryfile
strings Displays printable strings in files. Usage: Extracts and displays printable strings from binaryfile.
Useful for beginners to find readable text in binary files.
systemctl start <service_name> like chronyd
systemctl Control the systemd system and service manager systemctl status chronyd
S
tail -2 file_ame
tail gives you last few lines tail -f file_name (will keep update the logs)
tar cvf name.tar file_path
tar xvf name.tar

tar -tf name.tar (will show or list the content of tar file)

tar grouping of files -----------------------


tar and zip at the same time
tar -zcvf name.tar.gz file_path
tar -zxvf name.tar.gz

NOTE: tar.gz and tgz is the same thing


echo "hello" | tr [:lower:] [:upper:]
delete the lowercase: | -d [:upper:]
tr space with tab: | [:space:] "\t"
to translate or delete
delete or change digits: | -d [:digit:]
[:punct:]
ls -ltr | tee dir.txt
tee to store and display the output of a command at the same time ls -ltr | tee -a dir.txt (to append)
telnet to check connectivity from your linux server to outside telnet ip port
To create
touch file-name
touch to create a file touch -d "Thu, 1 Mar 2018 12:30:00" <file_name>
file with
given date
truncate to reduce the file of the size, but your data will be lost (its not like compressing) truncate -s 50 file_name
tcpdump to capture the incoming and outgoing traffic over the network interface tcpdump -i <interface_name>

T
Example: traceroute example.com
traceroute Traces the route packets take to a network host. Usage: Displays the route packets take to reach example.com.
Useful for beginners to diagnose network paths and issues.
T top to monitor the server including CPU, storage usage and process runnning etc top
uname
uname show name of system uname -a
uptime show how many users logged in, average load on cpu and active time of your server uptime
sort file | uniq
Identify duplicate lines
uniq NOTE: To use the uniq, the date should be sorted first as it compare line with it's previous to To count/find the no. of instances of duplicates (ex: how many
find the duplicate times Prashant arrived in list)
sort file | uniq -c
umask u+rw,g+r,o-rwx
to change the default permissions for newly created files
umask just doing this will be temporary
normally the permission is picked from bashrc of user and etc when you only execute 'umask' it will give result like 0022
so it means default permission will be 0777-0022 = 0755 (r-x)
Example: unzip archive.zip
Extracts compressed files from a ZIP archive. Usage: Extracts the contents of archive.zip into the current
unzip
directory.
Example: sudo updatedb
Usage: Refreshes the database for the locate command, allowing
updatedb Updates the database used by locate. it to find files more efficiently. Useful for beginners to keep
the file database up-to-date.
useradd <name_of_user>
useradd add user useradd -g superheores -s /bin/bash -c "thor charac" -m -d
/home/thor thor
userdel <name_of_user>
userdel del user userdel -r (will remove home directory)
userdel -f (force delete even if the user is logged in)
U usermod modify user usermod -G superheores hulk (to add user to a new group)
vi vim text editor to edit a file or creat a file vi file_name
Example: vmstat 2 5
Usage: Displays system performance statistics every 2 seconds
vmstat Reports virtual memory statistics. for 5 iterations. Useful for beginners to monitor system
performance, including CPU, memory, and I/O.
Example: vgs
vgs Displays information about volume groups. Usage: Lists all volume groups and their attributes. Useful for
beginners to manage LVM (Logical Volume Manager) storage.
V visudo used to change the user roles and permissins by editing /etc/sudoers file as root only visudo
w show who is logged on what the user is doing w
who show who is logged on to your linux server who
which It will show the path of executable file which pwd
wc -l/w/c file
wc word counter, helps you find no. of words, words present in how many lines etc grep "key" file | wc -l
write to wirte a message to a user, mostly use duing maintenance write user_name
wall to broadcast your message wall
W
W wget to download something in linux wget <download_link_of_file>
Example: echo "file1 file2 file3" | xargs rm
Usage: Removes file1, file2, and file3. Useful for beginners to
xargs Builds and executes command lines from standard input. construct command lines from input.
X
yum install package_name
yum update -y
yum to install packages, downlaod and update yum remove nginx
yum search nginx
Y yum list installed
zcat To read content of compressed file Example: zcat file.gz
Example: zip archive.zip file1 file2
Usage: Creates a ZIP archive named archive.zip containing file1
zip To compress and file2. Useful for beginners to compress files.

zipinfo List detailed information about the contents of a ZIP file. Example: zipinfo archive.zip
Example: zgrep "pattern" file.gz
zgrep Search compressed files for a pattern using grep. Usage: Searches for pattern in file.gz. Useful for beginners to
Z search within compressed files.

You might also like