Linux Cheat Sheet

Basic Commands
CommandDescriptionReal-World Example
pwdPrint the current working directory
Input:  pwd
Output: /home/user
        
ls List files and directories in the current location
Input:  ls
Output: Documents  Downloads  file.txt
        
cd Change the current directory
Input:  cd Documents
Output: (no output, just changes to Documents directory)
        
cp Copy files or directories
Input:  cp file.txt copy.txt
Output: (copy.txt created with same content as file.txt)
        
mv Move or rename files or directories
Input:  mv copy.txt archive/
Output: (copy.txt moved to archive/ directory)
        
rm Remove files or directories
Input:  rm file.txt
Output: (file.txt deleted)
        
touch Create a new empty file or update timestamp
Input:  touch newfile.txt
Output: (newfile.txt created)
        
mkdir Create a new directory
Input:  mkdir new_folder
Output: (new_folder created)
        
rmdir Remove an empty directory
Input:  rmdir old_folder
Output: (old_folder deleted if empty)
        
File Viewing & Editing
Command Description Real-World Example
cat file.txt Displays the full content of a file
    Input:  cat file.txt
    Output: Hello world! This is the file content.
            
less file.txtView file content with scroll (page by page)
    Input:  less file.txt
    Output: (Shows file in pager; use ↑ ↓ to scroll, q to quit)
            
more file.txtView file content page-by-page (simpler than less)
    Input:  more file.txt
    Output: (Displays first page; press space for next)
            
head file.txtDisplay the first 10 lines of a file
    Input:  head file.txt
    Output: Line 1
            Line 2
            ...
            Line 10
            
tail file.txtDisplay the last 10 lines of a file
    Input:  tail file.txt
    Output: Line 91
            ...
            Line 100
            
nano file.txtOpen file in nano text editor (simple and beginner-friendly)
    Input:  nano file.txt
    Output: (Opens nano editor; use Ctrl+X to exit)
            
vim file.txtOpen file in Vim editor (advanced and powerful)
    Input:  vim file.txt
    Output: (Opens file in Vim; press i to edit, :wq to save and quit)
            
Search & Filter
CommandDescriptionReal-World Example
grep "hello" file.txtSearch for the word "hello" in file.txt
Input:  grep "hello" file.txt
Output: hello world, this is a test
        
find / -name file.txt Search for a file named "file.txt" starting at root
Input:  find / -name file.txt
Output: /home/user/Documents/file.txt
        
locate file.txt Quickly find file using a prebuilt database
Input:  locate file.txt
Output: /home/user/Documents/file.txt
        
which python Show the path of the python executable
Input:  which python
Output: /usr/bin/python
        
whereis python Locate the binary, source, and man pages for a command
Input:  whereis python
Output: python: /usr/bin/python /usr/share/man/man1/python.1.gz
        
echo 'file1 file2' | xargs rm Run rm on each word passed via pipe
Input:  echo 'file1 file2' | xargs rm
Output: (file1 and file2 deleted if they exist)
        
File Permissions
Command Description Real-World Example
chmod 755 script.sh Change file permissions (rwxr-xr-x)
    Input:  chmod 755 script.sh
    Output: (script.sh is now executable by owner, readable/executable by others)
            
chown user:group file.txtChange the owner and group of a file
    Input:  chown alice:devs file.txt
    Output: (Ownership of file.txt changed to alice:devs)
            
chgrp devs file.txtChange group ownership of a file
    Input:  chgrp devs file.txt
    Output: (Group of file.txt changed to devs)
            
umaskShow or set default permissions for new files
    Input:  umask
    Output: 0022
            
umask 0007Set new default permissions (restricts access for others)
    Input:  umask 0007
    Output: (New files will default to 770 permissions)
            
User & Group Management
CommandDescriptionReal-World Example
adduser aliceAdd a new user named "alice"
    Input:  sudo adduser alice
    Output: Adding user `alice' ...
            
userdel aliceDelete an existing user
    Input:  sudo userdel alice
    Output: (User alice deleted)
            
usermod -aG sudo aliceAdd user to the "sudo" group
    Input:  sudo usermod -aG sudo alice
    Output: (alice can now use sudo)
            
passwd aliceChange the password for user "alice"
    Input:  sudo passwd alice
    Output: Enter new UNIX password:
            
groupadd devsCreate a new group named "devs"
    Input:  sudo groupadd devs
    Output: (Group devs created)
            
groups aliceList all groups the user "alice" belongs to
    Input:  groups alice
    Output: alice : alice sudo devs
            
id aliceDisplay user ID, group ID, and group memberships
    Input:  id alice
    Output: uid=1001(alice) gid=1001(alice) groups=1001(alice),27(sudo),1002(devs)
            
whoamiPrint the current user’s username
    Input:  whoami
    Output: alice
            
Process Management
CommandDescriptionReal-World Example
ps auxDisplay a snapshot of all running processes
    Input:  ps aux
    Output: USER   PID %CPU %MEM COMMAND
            alice 1234  0.0  0.1  bash
            
topReal-time display of running processes
    Input:  top
    Output: (Live list of processes; press q to quit)
            
htopInteractive process viewer (enhanced top)
    Input:  htop
    Output: (Colorful, interactive process viewer)
            
kill 1234Terminate a process by PID
    Input:  kill 1234
    Output: (Process with PID 1234 is terminated)
            
killall firefoxKill all processes by name
    Input:  killall firefox
    Output: (All firefox processes terminated)
            
nice -n 10 myscript.shStart a process with lower priority
    Input:  nice -n 10 ./myscript.sh
    Output: (myscript.sh runs with niceness 10)
            
renice -n -5 -p 1234Change the priority of an existing process
    Input:  renice -n -5 -p 1234
    Output: 1234 (process priority changed to -5)
            
jobsList background jobs in current shell
    Input:  jobs
    Output: [1]+  Running  ./script.sh &
            
fg %1Bring background job to foreground
    Input:  fg %1
    Output: (Brings job [1] to foreground)
            
bg %1Resume a suspended job in background
    Input:  bg %1
    Output: (Job [1] resumes in background)
            
Disk Usage & Storage
CommandDescriptionReal-World Example
df -hShow disk space usage in human-readable format
    Input:  df -h
    Output: Filesystem      Size  Used Avail Use% Mounted on
            /dev/sda1        50G   20G   28G  42% /
            
du -sh /home/aliceShow total size of a directory
    Input:  du -sh /home/alice
    Output: 2.1G    /home/alice
            
mount /dev/sdb1 /mntMount a device to a directory
    Input:  sudo mount /dev/sdb1 /mnt
    Output: (Device mounted to /mnt)
            
umount /mntUnmount a mounted device
    Input:  sudo umount /mnt
    Output: (Device unmounted from /mnt)
            
lsblkList block devices
    Input:  lsblk
    Output: NAME   MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
            sda      8:0    0  50G  0 disk /
            
blkidShow UUID and type of block devices
    Input:  sudo blkid
    Output: /dev/sda1: UUID="abcd-1234" TYPE="ext4"
            
fdisk -lList all partitions on all disks
    Input:  sudo fdisk -l
    Output: Disk /dev/sda: 50 GiB
            Device     Boot Start End Sectors Size Id Type
            /dev/sda1        2048 ...
            
parted /dev/sdaInteractive tool to manage partitions
    Input:  sudo parted /dev/sda
    Output: (parted prompt appears for interactive use)
            
Package Management
CommandDescriptionReal-World Example
apt updateUpdate package index
    Input:  sudo apt update
    Output: Hit:1 http://archive.ubuntu.com/ubuntu focal InRelease
            
apt install nginxInstall a package
    Input:  sudo apt install nginx
    Output: The following packages will be installed: nginx
            
dpkg -i package.debInstall a .deb package manually
    Input:  sudo dpkg -i package.deb
    Output: Unpacking package.deb ...
            
yum install httpdInstall a package using YUM (older Red Hat/Fedora)
    Input:  sudo yum install httpd
    Output: Installed: httpd.x86_64
            
dnf updateUpdate all packages (Fedora/RHEL 8+)
    Input:  sudo dnf update
    Output: Dependencies resolved. Upgraded: ...
            
rpm -i package.rpmInstall an RPM package manually
    Input:  sudo rpm -i package.rpm
    Output: Preparing... ################################# [100%]
            
pacman -SyuUpdate system and upgrade packages (Arch Linux)
    Input:  sudo pacman -Syu
    Output: :: Synchronizing package databases...
            
pacman -S firefoxInstall a package on Arch-based systems
    Input:  sudo pacman -S firefox
    Output: resolving dependencies...
            
Networking
CommandDescriptionReal-World Example
ip aDisplay IP address and network interfaces
    Input:  ip a
    Output: inet 192.168.1.10/24 brd 192.168.1.255 ...
            
ifconfigShow or configure a network interface (older tool)
    Input:  ifconfig
    Output: eth0: inet 192.168.1.10 netmask 255.255.255.0
            
ping google.comSend ICMP packets to test connectivity
    Input:  ping google.com
    Output: 64 bytes from google.com: icmp_seq=1 ttl=117 time=22 ms
            
netstat -tulnList all listening ports and connections
    Input:  netstat -tuln
    Output: tcp 0.0.0.0:22  LISTEN
            
ss -tulnFaster and more detailed alternative to netstat
    Input:  ss -tuln
    Output: LISTEN 0 128 0.0.0.0:22
            
curl https://example.comTransfer data from or to a server (shows content)
    Input:  curl https://example.com
    Output: <html>...</html>
            
wget https://example.com/file.txtDownload files from the web
    Input:  wget https://example.com/file.txt
    Output: Saving to: ‘file.txt’
            
scp file.txt user@192.168.1.5:/home/user/Securely copy a file to another system
    Input:  scp file.txt user@192.168.1.5:/home/user/
    Output: file.txt 100% 12KB 150.0KB/s 00:00
            
rsync -av file.txt user@192.168.1.5:/home/user/Sync files/directories between systems
    Input:  rsync -av file.txt user@192.168.1.5:/home/user/
    Output: sending incremental file list
            
ssh user@192.168.1.5Securely log into a remote machine
    Input:  ssh user@192.168.1.5
    Output: Welcome to Ubuntu 22.04 LTS
            
System Information
CommandDescriptionReal-World Example
uname -aDisplay all system information (kernel, architecture, etc.)
    Input:  uname -a
    Output: Linux myhost 5.15.0-84-generic #1 SMP ... x86_64 GNU/Linux
            
hostnameShow the system's hostname
    Input:  hostname
    Output: myhost
            
uptimeShows how long the system has been running
    Input:  uptime
    Output: 14:03:26 up 3 days,  2:17,  2 users,  load average: 0.16, 0.09, 0.07
            
free -hDisplay memory usage (human-readable)
    Input:  free -h
    Output: Mem:  7.7Gi  2.1Gi  3.4Gi  ...
            
vmstatReport system performance (memory, CPU, I/O)
    Input:  vmstat
    Output: procs -----------memory---------- ---swap-- ...
            
lscpuDisplay CPU architecture information
    Input:  lscpu
    Output: Architecture: x86_64
            CPU(s): 4
            
lsmemShow memory block information
    Input:  sudo lsmem
    Output: RANGE                                 SIZE  STATE ...
            0x0000000000000000-0x000000007fffffff  2G   online
            
dmesgView kernel ring buffer (boot and hardware messages)
    Input:  dmesg | tail
    Output: [   12.123456] eth0: link up
            
Text Processing
CommandDescriptionReal-World Example
awk '{print $1}' file.txtPrint the first column of a file or input stream
    Input:  awk '{print $1}' employees.txt
    Output: John
            Alice
            
sed 's/foo/bar/g' file.txtReplace all occurrences of "foo" with "bar"
    Input:  sed 's/foo/bar/g' notes.txt
    Output: bar is a placeholder text
            
cut -d',' -f1 names.csvCut and show the first field (using comma as delimiter)
    Input:  cut -d',' -f1 names.csv
    Output: John
            Alice
            
sort names.txtSort lines alphabetically
    Input:  sort names.txt
    Output: Alice
            John
            Mike
            
uniq sorted.txtRemove duplicate adjacent lines
    Input:  uniq sorted.txt
    Output: Alice
            John
            
tr 'a-z' 'A-Z'Translate lowercase to uppercase
    Input:  echo "hello" | tr 'a-z' 'A-Z'
    Output: HELLO
            
wc -l file.txtCount the number of lines in a file
    Input:  wc -l notes.txt
    Output: 42 notes.txt
            
tee output.txtRead from input and write to a file and stdout
    Input:  echo "Log entry" | tee log.txt
    Output: Log entry
            (and writes to log.txt)
            
split -l 10 large.txtSplit a file into pieces with 10 lines each
    Input:  split -l 10 large.txt
    Output: xaa, xab, xac...
            
paste file1 file2Merge lines from two files side-by-side
    Input:  paste names.txt ages.txt
    Output: Alice   25
            John    30
            
Compression & Archiving
CommandDescriptionReal-World Example
tar -cvf archive.tar file1 file2Create a tar archive
    Input:  tar -cvf archive.tar file1.txt file2.txt
    Output: file1.txt
            file2.txt
            
tar -xvf archive.tarExtract files from a tar archive
    Input:  tar -xvf archive.tar
    Output: file1.txt
            file2.txt
            
gzip file.txtCompress a file using gzip
    Input:  gzip report.txt
    Output: Creates report.txt.gz
            
gunzip file.txt.gzDecompress a .gz file
    Input:  gunzip report.txt.gz
    Output: Extracts report.txt
            
bzip2 file.txtCompress file using bzip2
    Input:  bzip2 data.txt
    Output: Creates data.txt.bz2
            
xz file.txtCompress file using xz
    Input:  xz manual.txt
    Output: Creates manual.txt.xz
            
zip archive.zip file1 file2Create a zip archive
    Input:  zip archive.zip file1.txt file2.txt
    Output: adding: file1.txt
            adding: file2.txt
            
unzip archive.zipExtract a zip archive
    Input:  unzip archive.zip
    Output: extracting: file1.txt
            extracting: file2.txt
            

Leave a Comment

Your email address will not be published. Required fields are marked *

Shopping Cart