Linux Cheat Sheet
Linux is a family of open-source Unix-like operating systems based on the Linux kernel. Linux is known for its stability, security, and flexibility, making it widely used in servers, desktops, embedded systems, and other platforms. It supports a wide range of hardware and provides a robust environment for software development and deployment.
Basic Linux Commands with Examples
This Linux cheat sheet covers all the most important Linux commands from the basics.
File Management
Command | Description | Options | Examples |
---|---|---|---|
ls | List files and directories. | -l: Long format listing. -a: Include hidden files. -h: Human-readable file sizes. |
|
cd | Change directory. |
| |
pwd | Print current working directory. |
| |
mkdir | Create a new directory. | -p: Create the parent directories as needed |
|
rm | Remove files and directories. | -r: Remove directories recursively. -f: Force removal without confirmation. |
|
cp | Copy files and directories. | -r: Copy directories recursively. |
|
mv | Move/rename files and directories. |
| |
touch | Create an empty file or update file timestamps. |
| |
cat | View the contents of a file. |
| |
head | Display the first few lines of a file. | -n: Specify the number of lines to display. |
|
tail | Display the last few lines of a file. | -n: Specify the number of lines to display. -f: It makes the command keep the file open and continuously output new lines as they are added to the file. |
|
ln | Create links between files. | -s: Create symbolic (soft) links. |
|
find | Search for files and directories. | -name: Search by filename. -type: Search by file type. |
|
File Permission Commands
Each file and directory has three types of users who can interact with it:
- Owner: The user who owns the file.
- Group: Users who are part of the file's group.
- Others: All other users.
Permissions are represented by three bits for each user type:
- r: Read permission (4)
- w: Write permission (2)
- x: Execute permission (1)
Permissions can be set using a three-digit octal number. Each digit corresponds to the owner, group, and others, respectively.
- 0: No permission
- 1: Execute only
- 2: Write only
- 3: Write and execute
- 4: Read only
- 5: Read and execute
- 6: Read and write
- 7: Read, write, and execute
Command | Description | Options | Examples |
---|---|---|---|
chmod | Change file permissions. | u: User/owner permissions. g: Group permissions. o: Other permissions. +: Add permissions. –: Remove permissions. =: Set permissions explicitly. |
|
chown | Change file ownership. |
| |
chgrp | Change group ownership. |
| |
umask | Set default file permissions. |
|
File Compression and Archiving Commands
Command | Description | Options | Examples |
---|---|---|---|
tar | Create or extract archive files. | -c: Create a new archive. -x: Extract files from an archive. -f: Specify the archive file name. -v: Verbose mode. -z: Compress the archive with gzip. -j: Compress the archive with bzip2. |
|
gzip | Compress files. | -d: Decompress files. |
|
zip | Create compressed zip archives. | -r: Recursively include directories. |
|
unzip | Extract files from zip archives. | -l: List the contents of the ZIP file without extracting. -d: Extract files into a specified directory. -o: Overwrite existing files without prompting. -n: Never overwrite existing files. |
|
Process Management Commands
Command | Description | Options | Examples |
---|---|---|---|
ps | Display running processes. | aux: Show all processes. |
|
top | Monitor system processes in real-time. |
| |
kill | Terminate a process. | -9: Forcefully kill a process. |
|
pkill | Terminate processes based on their name. |
| |
pgrep | List processes based on their name. |
| |
grep | Used to search for specific patterns or regular expressions in text files or streams and display matching lines. | -i: Ignore case distinctions while searching. -v: Invert the match, displaying non-matching lines. -r or -R: Recursively search directories for matching patterns. -l: Print only the names of files containing matches. -n: Display line numbers alongside matching lines. -w: Match whole words only, rather than partial matches. -c: Count the number of matching lines instead of displaying them. -e: Specify multiple patterns to search for. -A: Display lines after the matching line. -B: Display lines before the matching line. -C: Display lines both before and after the matching line. |
|
System Information Commands
Command | Description | Options | Examples |
---|---|---|---|
uname | Print system information. | -a: All system information. |
|
whoami | View Current User |
| |
df | Show disk space usage. | -h: Human-readable sizes. |
|
du | Check User's Disk Usage |
| |
free | Display memory usage information. | -h: Human-readable sizes. |
|
uptime | Show system uptime. |
| |
lscpu | Display CPU information. |
| |
lspci | List PCI devices. |
| |
lsusb | List USB devices. |
|
Networking Commands
Command | Description | Options | Examples |
---|---|---|---|
ip | Display and manage networking, routing, and tunnel configurations. | addr: Diplay IP Addresses. route: Display/Manage route table. link: Manage network interface |
|
ifconfig | Display network interface information. | -a: Display all Interfaces |
|
ping | Send ICMP echo requests to a host. | -4: Sends ICMP request to IPv4 address of network host. -6: Sends ICMP request to IPv6 address of network host. |
|
netstat | Display network connections and statistics. | -a: Displays all connections. -n: Show numerical addresses and ports. -l: Shows listening ports. -t: Shows TCP connnections information only. -u: Shows UDP connections information only. |
|
ssh | Securely connect to a remote server. | -p: Specify a Port for connection. -i: Path of Identity (Private Key) File. -L: Tunneling with SSH (Local port forwarding). -R: Reverse Tunneling |
|
scp | Securely copy files between hosts. | -r: Copy Directory recursively |
|
wget | Download files from the web. | -O: Download file with defined Output Filename -A: Download only specific file types |
|
curl | Transfer data to or from a server. | -L: Follow Redirects -O: Download a file -o: Download file with defined Output Filename -X: Change the HTTP method -d: Send data in body of HTTP request -H: Send headers with HTTP request -u: Download with Authentication -x: Download Using a Proxy -F: Upload Binary File (e.g., ZIP file) via POST -k: Ignore Invalid SSL Certificates |
|
IO Redirection Commands
Name | Description | Examples |
---|---|---|
Standard Output Redirection | > : Redirects output, overwriting the file if it exists. >> : Redirects output, appending to the file if it exists. |
|
Standard Input Redirection | <: Redirects input from a file to a command. |
|
Standard Error Redirection | 2> : Redirects error output, overwriting the file. 2>> : Redirects error output, appending to the file. |
|
Redirect Both Output and Error | &> : Redirects both standard output and error, overwriting the file. &>> : Redirects both standard output and error, appending to the file. |
|
Combine Standard Output and Error | 2>&1: Redirects standard error to the same location as standard output. |
|
Discard Output | /dev/null: Send unwanted output or error to the null device (acts as a "black hole"). |
|
Here Document | <<: Used to pass a multiline string as input to a command. |
|
Here String | <<<: Redirects a string as input to a command. |
|
Piping | |: Sends the output of one command as input to another command. |
|
Environment Variable Commands
Command / Name | Description | Examples | ||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
env | Displays all environment variables. |
| ||||||||||||||||||||||||
printenv | Prints specific or all environment variables. |
| ||||||||||||||||||||||||
export | Temporary Environment Variable (Only for current shell session) |
| ||||||||||||||||||||||||
Persisting Environment Variables | Add to .bashrc or .bash_profile |
| ||||||||||||||||||||||||
Unsetting Environment Variables | Removes an environment variable. |
| ||||||||||||||||||||||||
Predefined Environment Variables |
| |||||||||||||||||||||||||
Special Variables in Shell Scripting |
|
User Management Commands
Command | Description | Options | Examples |
---|---|---|---|
useradd | Create a new user | -d /home/USERNAME : Specify user's home directory -m : Create the user's home directory -s /bin/bash : Specify the login shell -c "COMMENT" : Add a comment for the user |
|
passwd | Set or Update a User Password |
| |
userdel | Delete a User | -r : Remove the user's home directory and mail spool |
|
usermod | Modify an Existing User | -l NEW_USERNAME : Change the user's login name. -d /new/home/directory : Change the user's home directory -s /bin/sh : Change the login shell -aG GROUP : Add the user to a group (non-destructive) -g:Change User's Primary Group -L: lock a user account -L: lock a user account |
|
groupadd | Create a new group |
| |
su | Switch to another user | - : Start a login shell |
|
groups | List user groups |
| |
lastlog | Show Last Login Information |
| |
whoami | View Current User |
| |
du | Check User's Disk Usage |
|
Shortcuts Commands
Bash Comman Shortcuts:
Command | Description |
---|---|
Ctrl + A | Move to the beginning of the line |
Ctrl + E | Move to the end of the line |
Ctrl + U | Cut text from cursor to start |
Ctrl + K | Cut text from cursor to end |
Ctrl + Y | Paste the last cut text |
Ctrl + L | Clear the terminal screen |
Ctrl + R | Search command history |
Tab | Autocomplete file or command |
!! | Repeat the last command |
!n | Run command number n from history |
Bash Useful Commands:
Command | Description | Examples |
---|---|---|
alias | Create an alias for a command |
|
history | Show command history |
|
export | Set environment variables |
|
grep | Search text using patterns |
|
find | Search for files in a directory |
|
chmod | Change file permissions |
|
chown | Change file owner and group |
|
ps | Display currently running processes |
|
kill | Terminate a process by PID |
|
top | Display real-time system processes |
|
Nano Shortcuts Commands:
Command | Description |
---|---|
Ctrl + O | Write (save) the file |
Ctrl + X | Exit Nano |
Ctrl + G | Display Help |
Ctrl + K | Cut current line |
Ctrl + U | Paste the cut line |
Ctrl + W | Search for a text in the file |
Ctrl + C | Show cursor position (line/column) |
Alt + A | Start marking text |
Ctrl + \ | Find and replace text |
Ctrl + _ | Go to a specific line |
VI Shortcuts Commands:
Vi Modes- Insert Mode: Press i to insert text.
- Command Mode: Press Esc to switch to command mode.
- Visual Mode: Press v to start visual selection.
Command | Description |
---|---|
i | Enter insert mode |
Esc | Exit insert mode and return to command mode |
:w | Save the file |
:q | Quit |
:wq | Save and quit |
:q! | Quit without saving |
dd | Delete the current line |
yy | Copy the current line |
p | Paste copied content |
/text | Search for 'text' |
n | Repeat the last search |
u | Undo last action |
Ctrl + r | Redo an undone action |
Vim Shortcuts Commands:
Vim is a improved version of vi
Vim Modes- Normal Mode: For navigating and executing commands.
- Insert Mode: For inserting text (i to enter).
- Command-Line Mode: For running commands (: to enter).
- Visual Mode: For selecting text (v to enter).
Command | Description |
---|---|
i | Enter insert mode |
Esc | Exit insert mode and return to command mode |
:w | Save the file |
:q | Quit |
:wq | Save and quit |
:q! | Quit without saving |
dd | Delete the current line |
yy | Copy the current line |
p | Paste copied content |
/text | Search for 'text' |
n | Repeat the last search |
u | Undo last action |
Ctrl + r | Redo an undone action |
gg | Go to the beginning of the file |
G | Go to the end of the file |
v | Enter visual mode (to select text) |