Skip to content

Linux Command Line Cheat Sheet

Linux is a free-to-use and freely distributed Unix-like operating system. It is a multi-user, multi-tasking, multi-threaded, and multi-CPU operating system based on POSIX and Unix. It can run major Unix software tools, applications, and network protocols. It supports both 32-bit and 64-bit hardware. Linux inherits the network-centric design philosophy of Unix and 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 Display system reboot history
date Display current system date and time
timedatectl Query and change the system clock
cal Display the current calendar month and date
w Display users currently logged into the system
whoami Display who you are logged in as
finger username Display information about a user

Hardware

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

Users

id Display active user details (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, hidden and their permissions
pwd Show current directory path
mkdir ‘dir_name’ Create a new directory
rm file_name Delete a file
rm -f filename Force delete a file
rm -r dir_name Recursively delete a directory
rm -rf dir_name Force and recursively delete a directory
cp file1 file2 Copy content of file1 to file2
cp -r dir1 dir2 Recursively copy dir1 to dir2 (creates dir2 if not exist)
mv file1 file2 Rename file1 to file2
ln -s /path/to/file 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 file content
head file_name Display first 10 lines of file
tail file_name Display last 10 lines of 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/terminate all processes named proc
pkill proc-name Send signal to process by name
bg Resume a suspended job in the background
fg Bring a background job to the foreground
fg n Bring job n to the foreground
lsof List files opened by processes
renice 19 PID Run 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 user:group file_name Change owner and group owner of a file
chown user:group dir_name Change owner and group owner of a directory

Network

ip addr show Display IP addresses and all network interfaces
ip address add 192.168.0.1/24 dev eth0 Assign IP 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 info about a domain name
dig domain Retrieve DNS info for a domain
dig -x host Perform reverse lookup on a domain
host google.com Perform IP lookup for a domain name
hostname -i Display local IP address
wget file_url Download a file from an online source
netstat -pnltu Display all active listening ports

Compression/Packaging

tar -cf home.tar home Create archive ‘home.tar’ from ‘home’
tar -xf files.tar Extract archive ‘files.tar’
tar -zcvf home.tar.gz folder Create a compressed tar archive from folder
gzip file Compress file into .gz extension

Installing Packages

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

Install from Source (Compile)

./configure Check system for required software to build program
make Compile the source
make install Install binaries to default/modified path

Search

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

Login

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

File Transfer

scp file1.txt server2:/tmp Securely copy file1.txt to server2 in /tmp dir
rsync -a /home/apps /backup/ Sync contents of /home/apps to /backup

Disk Usage

df -h Display free space on mounted systems
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 directory tree
cd Change to $HOME directory
cd /test Change to /test directory