Linux Commands

Linux Commands Cheat Sheet: Essential Commands & Shortcuts

File & Directory Management

ls

Lists files and directories.

ls -l

Lists files in 'long format' with detailed info, including permissions and ownership.

ls -lah

Lists files with detailed info, including hidden files, in human-readable format.

ls -a

Lists all contents, including hidden files, in the specified directory.

ls -author

Lists contents in the specified directory along with its owner.

ls -S

Sorts and lists all contents in the specified directory by size.

ls *.html

Lists only the contents in the directory of a particular format (e.g., HTML files).

ls -lS > file.txt

Copies the result of ls command (sorted by size in long format) into a text file.

pwd

Prints the current working directory.

cd /path/to/dir

Changes to a specified directory.

cd

Changes the directory to the home directory.

cd /

Changes the directory to the root directory.

cd ..

Changes the directory to its parent directory.

cd 'folder name'

Changes to a folder with spaces in its name (use quotes).

mkdir new_directory

Creates a new directory.

rmdir directory

Removes a specified empty directory.

rmdir -p directory

Removes both the parent and child directory.

rmdir -pv directory

Removes all parent and subdirectories with verbose output.

rm filename

Deletes a file.

rm -r directory

Removes a directory, including non-empty directories.

rm -rp directory

Removes non-empty directories, including parent and subdirectories.

rm -rf directory

Forcefully deletes a directory and all its contents.

cp source destination

Copies a file or directory, keeping the original in the working directory.

cp -i source destination

Copies in interactive mode; asks before overwriting files.

cp -n source destination

Does not overwrite the destination file if it exists.

cp -u source destination

Updates the destination file only if the source file is different.

cp -r source destination

Recursively copies directories, including hidden files.

cp -v source destination

Copies with verbose output; prints informative messages.

mv old_name new_name

Renames or moves a file/directory; removes it from the original location.

mv -i old_name new_name

Moves in interactive mode; asks before overwriting files.

mv -u old_name new_name

Updates the destination file only if the source file is different.

mv -v old_name new_name

Moves with verbose output; prints source and destination files.

touch filename

Creates a new file in any format.

find /path -name '*.txt'

Finds all '.txt' files in a directory.

locate filename

Finds the location of a file.

tree

Displays directories and files in a tree-like format.

File Permissions & Ownership

ls -l

Lists files with permissions and ownership details.

chmod 755 filename

Changes file permissions (e.g., rwxr-xr-x).

chmod -R 755 directory

Recursively changes directory permissions.

chmod 744 filename

Changes file permissions (e.g., rwxr--r--).

chown user:group filename

Changes file ownership to a specified user and group.

chown -R user:group directory

Recursively changes ownership of a directory and its contents.

chgrp groupname filename

Changes group ownership of a file.

umask 022

Sets default file permissions for new files.

Process Management

ps aux

Lists all running processes.

top

Displays real-time system statistics and processes.

htop

Interactive process viewer (if installed).

kill PID

Kills a process using its Process ID (PID).

pkill process_name

Kills a process by name.

killall process_name

Kills all processes with the given name.

nice -n 10 command

Runs a process with a lower priority.

renice 10 -p PID

Changes the priority of a running process.

Networking

ip a

Displays all IP addresses assigned to network interfaces.

ifconfig

Shows network interface details (deprecated, use 'ip a').

ping -c 5 google.com

Sends 5 ICMP packets to check network connectivity.

hostname

Shows the IP address and hostname.

hostname -I

Shows the IP address of the host.

wget URL

Downloads a file from a specified URL (http, https).

curl -O URL

Downloads a file using curl.

netstat -tulnp

Displays active connections and listening ports.

traceroute google.com

Traces the path to a destination.

nslookup [HOST] [SERVER]

Queries the nameserver for the IP address of a host, optionally using a specified DNS server.

telnet

Connects to a telnet server.

ssh <master's ip>

Gives remote access to the master node from a slave node.

ssh <slave's ip>

Gives remote access to the slave node from a master node.

Disk Management

df -h

Displays disk space usage in a human-readable format.

df -m

Shows disk partition space in megabytes.

df -t

Shows the types of file systems.

du -sh folder

Shows the size of a specific folder.

du

Shows disk usage of files.

mount /dev/sdX /mnt

Mounts a storage device.

umount /mnt

Unmounts a storage device.

fsck /dev/sdX

Checks and repairs a filesystem.

lsblk

Displays block devices and mount points.

Package Management

apt update

Updates package lists (Debian-based systems).

apt upgrade

Upgrades all installed packages (Debian-based systems).

apt-get install package-name

Installs a package (Debian-based systems).

yum update

Updates packages (RHEL-based systems).

yum install package-name

Installs a package (RHEL-based systems).

dnf install package

Installs a package (Fedora/RHEL).

pip install package

Installs a Python package using pip.

Text Processing

cat file.txt

Displays, modifies, or concatenates the contents of a file.

cat -b file.txt

Displays file contents with line numbers for non-blank lines.

cat -n file.txt

Displays file contents with line numbers for all lines.

cat -s file.txt

Squeezes multiple blank lines into one line.

cat -E file.txt

Shows a '$' at the end of each line.

less file.txt

Views a file page-by-page.

grep 'search_term' file.txt

Finds a specific term in a file.

grep -n 'search_term' file.txt

Finds a term and returns matching lines with their line numbers.

grep -i 'search_term' file.txt

Finds a term with case-insensitive matching.

grep -v 'search_term' file.txt

Returns lines not matching the search term.

grep -c 'search_term' file.txt

Returns the number of lines matching the search term.

awk '{print $1}' file.txt

Extracts the first column from a file.

sed 's/old/new/g' file.txt

Replaces text in a file.

sort file.txt

Sorts the contents of a file alphabetically or numerically.

sort -r file.txt

Sorts the contents in reverse order.

sort -f file.txt

Sorts with case-insensitive sorting.

sort -n file.txt

Sorts the contents in numerical order.

wc file.txt

Counts the number of words, lines, and characters in a file.

diff file1 file2

Compares the contents of two files and lists the changes needed to convert one into the other.

comm file1 file2

Compares two sorted files line by line.

cmp file1 file2

Compares two files (or file1 vs stdin if file2 is not specified).

Compression & Archiving

tar -cvf archive.tar folder

Creates a tar archive of a directory.

tar -xvf archive.tar

Extracts a tar archive.

zip -r archive.zip folder

Creates a zip archive of a directory.

unzip archive.zip

Extracts a zip archive.

System Monitoring

uptime

Shows system uptime.

free -h

Displays memory usage in a human-readable format.

free -b

Displays memory usage in bytes.

free -k

Displays memory usage in kilobytes.

free -m

Displays memory usage in megabytes.

vmstat

Shows system performance statistics.

iostat

Displays CPU and I/O statistics.

System Shutdown & Reboot

shutdown -h now

Shuts down the system immediately.

shutdown -r now

Reboots the system immediately.

reboot

Reboots the system.

halt

Stops the system.

sudo halt

Powers off the system.

sudo reboot

Reboots the system.

User & Group Management

sudo useradd username

Adds a new user.

sudo passwd username

Sets a password for a user.

sudo userdel username

Deletes a user.

sudo groupadd groupname

Adds a new group.

sudo groupdel groupname

Deletes a group.

sudo usermod -g groupname username

Adds a user to a primary group.

passwd

Changes the password for the current user.

su

Switches to the root user with superuser permissions.

su username

Switches to a different user specified by username.

sudo

Executes a command with root/superuser privileges.

Text Editors

nano

Opens the nano text editor.

vim

Opens the vim text editor.

jed

Opens the jed text editor.

Miscellaneous

clear

Clears the terminal screen.

echo

Writes its arguments to standard output.

man command

Displays the manual page for a command.

command --help

Shows help information for a command.

ctrl+c

Stops any command in the terminal safely.

ctrl+z

Force stops a command in the terminal.

exit

Exits the terminal.

date

Displays the current system date and time.

aspell

Interactive spell checker for files or standard input.