Linux Cheat Sheet

Return to TOC

Basic Commands

Command Description Example
pwd Print working directory pwd
ls List directory contents ls
cd [directory] Change directory cd /home/user/Documents
cp [source] [destination] Copy files or directories cp file.txt /home/user/
mv [source] [destination] Move or rename files or directories mv oldname.txt newname.txt
rm [file] Remove files rm file.txt
touch [file] Create a new empty file touch newfile.txt
mkdir [directory] Create new directory mkdir newdirectory
rmdir [directory] Remove empty directory rmdir olddirectory

File Viewing and Editing

Command Description Example
cat [file] Concatenate and display file content cat file.txt
less [file] View file content page by page less file.txt
more [file] View file content with scrolling more file.txt
head [file] View the first 10 lines of a file head file.txt
tail [file] View the last 10 lines of a file tail file.txt
nano [file] Edit files with Nano editor nano file.txt
vi [file] Edit files with Vi editor vi file.txt

File Permissions

Command Description Example
chmod [mode] [file] Change file permissions chmod 755 script.sh
chown [owner][:group] [file] Change file owner and group chown user:group file.txt

Searching and Finding

Command Description Example
find [directory] -name [filename] Find files by name find /home -name "*.txt"
grep [pattern] [file] Search for a pattern within files grep "search term" file.txt
locate [name] Find files by name using a database locate file.txt
which [command] Show the path of a command which python

System Information

Command Description Example
uname -a Show all system information uname -a
df -h Show disk usage in human-readable format df -h
du -sh [directory] Show directory size du -sh /home/user/
top Display real-time system processes top
free -h Show free and used memory free -h
uptime Show how long the system has been running uptime
who Show who is logged on who
dmesg Display kernel message buffer dmesg

Networking

Command Description Example
ip a Show IP addresses and network interfaces ip a
ping [host] Send ICMP ECHO_REQUEST to network hosts ping google.com
netstat -tuln Show listening ports netstat -tuln
ss -tuln Show listening ports (alternative to netstat) ss -tuln
curl [url] Transfer data from or to a server curl http://example.com
wget [url] Download files from the web wget http://example.com/file.zip
scp [source] [user@host:destination] Secure copy over SSH scp file.txt user@remotehost:/home/user/
ssh [user@host] Connect to a remote host via SSH ssh user@remotehost
su - [user] Switch User e.g. su - root su - root

Package Management

Command Description Example
apt update Update package lists (Debian/Ubuntu) apt update
apt upgrade Upgrade all packages (Debian/Ubuntu) apt upgrade
apt install [package] Install a package (Debian/Ubuntu) apt install nginx
yum update Update package lists (RHEL/CentOS) yum update
yum install [package] Install a package (RHEL/CentOS) yum install httpd
dnf update Update package lists (Fedora) dnf update
dnf install [package] Install a package (Fedora) dnf install nginx

Disk and File System Management

Command Description Example
mount [device] [mountpoint] Mount a file system mount /dev/sda1 /mnt
umount [mountpoint] Unmount a file system umount /mnt
fdisk -l List disk partitions fdisk -l
mkfs.ext4 [device] Format a partition with ext4 mkfs.ext4 /dev/sda1
fsck [device] Check and repair a file system fsck /dev/sda1

Scripting and Automation

Command Description Example
sh [script] Run a shell script sh script.sh
bash [script] Run a bash script bash script.sh
crontab -e Edit user crontab crontab -e
at [time] Schedule a job at a specific time at 5pm

Environment Variables

Command Description Example
echo $VARIABLE Print value of a variable echo $PATH
export VARIABLE=value Set an environment variable export PATH=/usr/local/bin:$PATH
env List all environment variables env

User and Group Management

Command Description Example
useradd [user] Add a new user useradd newuser
passwd [user] Change user password passwd newuser
userdel [user] Delete a user userdel olduser
groupadd [group] Add a new group groupadd newgroup
usermod -aG [group] [user] Add user to group usermod -aG sudo newuser

Archive and Compression

Command Description Example
tar -cvf [archive.tar] [directory] Create a tar archive tar -cvf archive.tar /home/user/
tar -xvf [archive.tar] Extract a tar archive tar -xvf archive.tar
gzip [file] Compress a file with gzip gzip file.txt
gunzip [file.gz] Decompress a gzip file gunzip file.txt.gz
zip [archive.zip] [file] Create a zip archive zip archive.zip file.txt
unzip [archive.zip] Extract a zip archive unzip archive.zip

Miscellaneous

Command Description Example
alias [alias_name]='[command]' Create a command alias alias ll='ls -la'
history Show command history history
!! Run the last command again !!
man [command] Show the manual for a command man ls
echo [text] Print text to the terminal echo "Hello, World!"
date Show current date and time date
cal Show calendar cal