Skip to content

Linux Command Line Cheat Sheet

Linux is a free and open-source Unix-like operating system based on POSIX and Unix. It is a multi-user, multi-tasking, multi-threaded, and multi-CPU supporting operating system. It can run major Unix tools, applications, and network protocols, and supports both 32-bit and 64-bit hardware. Inheriting the network-centric design philosophy of Unix, Linux is a stable multi-user network operating system.

System

uname Display Linux system information
uname -r Display kernel version information
uptime Display how long the system has been running (including load average)
hostname Display system hostname
hostname -i Display the IP address of the system
last reboot Show system reboot history
date Display the current system date and time
timedatectl Query and change the system clock
cal Display the current calendar month and date
w Display currently logged-in users
whoami Display your login identity
finger username Display information about a user

Hardware

dmesg Display boot messages
cat /proc/cpuinfo Display detailed CPU information (model, core, vendor)
cat /proc/meminfo Display detailed hardware memory information (total, free)
lshw Display system hardware configuration information
lsblk Display block device related information
free -m Display free and used memory (-m for MB)
lspci -tv Display PCI devices in a tree diagram
lsusb -tv Display USB devices in a tree diagram
dmidecode Display hardware information from BIOS
hdparm -i /dev/xda Display information about disk data
hdparm -tT /dev/xda Run read speed test on device xda
badblocks -s /dev/xda Test for unreadable blocks on disk

Users

id Display details of the active user (uid, gid, groups)
last Display last logins in the system
who Display who is logged into the system
groupadd “admin” Add group “admin”
adduser “Sam” Add user Sam
userdel “Sam” Delete user Sam
usermod Change/modify user information

Files

ls -al List files - including regular and hidden files and their permissions
pwd Display current directory path
mkdir ‘directory_name’ Create a new directory
rm file_name Delete a file
rm -f filename Force delete a file
rm -r directory_name Recursively delete a directory
rm -rf directory_name Force and recursively delete a directory
cp file1 file2 Copy contents of file1 to file2
cp -r dir1 dir2 Recursively copy dir1 to dir2. Create dir2 if it doesn’t exist
mv file1 file2 Rename file1 to file2
ln -s /path/to/file_name link_name Create a symbolic link to file_name
touch file_name Create a new file
cat > file_name Create a file from keyboard input
more file_name Output the contents of a file
head file_name Display the first 10 lines of a file
tail file_name Display the last 10 lines of a file
gpg -c file_name Encrypt a file
gpg file_name.gpg Decrypt a file
wc Print byte, word, and line counts of a file
xargs Execute commands from standard input

Processes

ps Display currently active processes
ps aux | grep ’telnet' Search for process id of ’telnet'
pmap Display memory map of a process
top Display all running processes
kill pid Terminate process with given pid
killall proc Kill all processes named proc
pkill process-name Send signal to process by name
bg Resume a suspended background command
fg Bring a background command to the foreground
fg n Bring job n to the foreground
lsof List files opened by processes
renice 19 PID Run a process with very low priority
pgrep firefox Find Firefox process ID
pstree Visualize processes in a tree model

File Permissions

chmod octal filename Change file permissions to octal
chmod 777 /data/test.c Set rwx permissions for owner, group, and everyone
chmod 755 /data/test.c Set rwx for owner, r-x for group and everyone
chmod 766 /data/test.c Set rwx for owner, rw- for group and everyone
chown owner user-file Change file ownership
chown owner-user:owner-group file_name Change file owner and group owner
chown owner-user:owner-group directory Change directory owner and group owner

Network

ip addr show Display IP addresses and all network interfaces
ip address add 192.168.0.1/24 dev eth0 Assign IP address 192.168.0.1 to interface eth0
ifconfig Display IP addresses of all network interfaces
ping host Send ICMP echo requests to establish connection
whois domain Retrieve information about a domain name
dig domain Retrieve DNS information for a domain
dig -x host Perform reverse lookup for a domain
host google.com Perform IP lookup for a domain name
hostname -i Display local IP address
wget file_name Download file from online resources
netstat -pnltu Display all active listening ports

Compression / Archiving

tar -cf home.tar home Create a tar archive named home.tar for directory home
tar -xf files.tar Extract archive file “files.tar”
tar -zcvf home.tar.gz source-folder Create a compressed tar archive from source folder
gzip file Compress file with .gz extension

Package Installation

rpm -i pkg_name.rpm Install rpm package
rpm -e pkg_name Remove rpm package
dnf install pkg_name Install package using dnf tool

Installation from Source (Compiling)

./configure Check system for dependencies and build the Makefile
make Compile the program
make install Install binary files in default/modified path

Search

grep ‘pattern’ files Search for a pattern in files
grep -r pattern dir Search recursively for a pattern in a given directory
locate file Find all instances of a file
find /home/ -name “index” Find filenames starting with ‘index’ in /home
find /home -size +10000k Find files larger than 10000k in home folder

Login

ssh user@host Securely connect to host as specified user
ssh -p port_number user@host Securely connect to host using specified port
ssh host Securely connect to host via default port 22
telnet host Connect to host via default port 23

File Transfer

scp file1.txt server2/tmp Securely copy file1.txt to server2’s /tmp directory
rsync -a /home/apps /backup/ Synchronize /home/apps with /backup directory

Disk Usage

df -h Display free space on mounted filesystems
df -i Display free inodes on filesystems
fdisk -l Display disk partitions, sizes, and types
du -sh Display disk usage of current directory in human-readable format
findmnt Display target mount points for all filesystems
mount device-path mount-point Mount a device

Directory Traversal

cd .. Move up one level in the directory structure
cd Change directory to $HOME directory
cd /test Change directory to /test directory