Files & Directories
๐
ls
View the list of files and folders in a directory.
Basic syntax and options
- Basic form: `ls [options] [path]`
- -l: shows detailed info like file size, permissions, and modified date.
- -a: shows all files, including hidden files (those starting with a dot).
- -h: shows file sizes in human-readable units (KB/MB).
ls -al โ view a detailed list including hidden files.
๐
cd
Change your current working directory.
Basic syntax and options
- `cd path` moves you to the given directory.
- `cd ..` moves up one directory, `cd ~` moves to your home directory.
- `cd -` returns to the previous directory you were in.
cd /var/log โ move into the /var/log directory.
๐
pwd
Prints the full path of the directory you're currently in.
Basic syntax and options
- Short for Print Working Directory.
- Often used in shell scripts to save the current path into a variable.
๐๏ธ
mkdir
Creates a new directory (folder).
Basic syntax and options
- `mkdir foldername` creates a folder.
- The -p option creates nested folders in one go even if the intermediate paths don't exist.
mkdir -p project/src/utils โ create nested folders in a single command.
๐๏ธ
rmdir
Deletes an empty directory.
Basic syntax and options
- Can't delete a directory that has contents โ use `rm -r` for that.
โจ
touch
Creates an empty file, or updates the modified time of an existing file.
Basic syntax and options
- If the file doesn't exist it creates one; if it does, only its last-modified time is updated to now.
touch notes.txt โ create an empty text file.
๐
cp
Copies files or directories.
Basic syntax and options
- Used as `cp source destination`.
- You need the -r option to copy an entire directory (folder).
- The -i option asks for confirmation before overwriting a file with the same name.
cp -r src backup โ copy the entire src folder to backup.
โก๏ธ
mv
Moves a file or directory, or renames it.
Basic syntax and options
- Keeping the same location but changing the name renames the file; specifying a different path moves it.
mv old.txt new.txt โ rename a file.
โ
rm
Deletes files or directories.
Basic syntax and options
- By default it only deletes files โ deleting a directory requires the -r (recursive) option.
- The -f option force-deletes without confirmation, so use it carefully.
- There's no recycle bin concept โ once deleted, recovery is very difficult.
rm -rf old_folder โ force-delete a folder and everything inside it (use with caution).
๐
cat
Prints a file's contents straight to the screen.
Basic syntax and options
- You can concatenate and print several files at once (`cat a.txt b.txt`).
- Often used to quickly check the contents of a short file.
๐
find
Finds files or directories matching given conditions.
Basic syntax and options
- `find path -name "pattern"` searches by name.
- -type f (file) / -type d (directory) lets you filter by type.
- The -mtime option lets you search based on modification time.
find . -name "*.log" โ find every .log file under the current folder.
๐ณ
tree
Shows the directory structure as a tree, all at a glance.
Basic syntax and options
- Not preinstalled on some distros, so you may need to install it separately (e.g. `apt install tree`).
- The -L number option limits how many folder levels deep it displays.
Permissions & Users
๐
chmod
Changes access permissions (read/write/execute) for a file or directory.
Basic syntax and options
- Permissions are expressed by adding read(4), write(2), and execute(1) for owner, group, and others in that order.
- For example, 755 means the owner can do everything, while the group and others can only read and execute.
- You can also set permissions symbolically (+ / -), like `chmod +x script.sh`.
chmod 755 script.sh โ grant the owner read/write/execute, and everyone else read/execute.
๐ค
chown
Changes the owner (user) of a file or directory.
Basic syntax and options
- Used as `chown username filename`.
- You can change both owner and group at once with `chown user:group filename`.
chown user1 file.txt โ change the owner of file.txt to user1.
๐ฅ
chgrp
Changes the group that owns a file or directory.
Basic syntax and options
- Used as `chgrp groupname filename`.
๐ก๏ธ
sudo
Runs a command with administrator (root) privileges.
Basic syntax and options
- Commonly said to stand for "Substitute User DO."
- Used for tasks that require more than a regular user's permissions, like changing system settings or installing packages.
- Asks for the password of the currently logged-in account before running.
๐
su
Switches to another user (root by default).
Basic syntax and options
- Typing just `su` tries to switch to root; `su username` switches to a specific account.
- Unlike sudo, it requires the password of the account you're switching to.
๐
whoami
Prints the name of the currently logged-in user.
Basic syntax and options
- Handy for quickly checking which account you're working as, in scripts or on a remote server.
Text Processing & Search
๐
grep
Finds lines containing a given string (pattern) in a file or input.
Basic syntax and options
- `grep "pattern" filename` is the basic form.
- The -i option searches case-insensitively.
- The -r option searches recursively through files in a directory.
- The -n option also shows the line number of each match.
grep -rn "TODO" ./src โ find every line containing TODO, with line numbers, in all files under src.
โ๏ธ
sed
A stream editor that replaces or edits text according to rules.
Basic syntax and options
- The most common use is string substitution: `sed 's/old/new/g' file`
- The trailing g means replace every match on each line.
- The -i option edits the original file directly instead of printing to the screen.
sed -i 's/foo/bar/g' file.txt โ replace every foo with bar in file.txt and save it.
๐งฎ
awk
Both a command and a small programming language for processing text by line and column (field).
Basic syntax and options
- By default it splits each line into fields (`$1`, `$2`, โฆ) based on whitespace.
- Often used to pull out a specific column from a log file.
awk '{print $1}' access.log โ print only the first field of each line.
โฌ๏ธ
head
Prints only the first few lines of a file.
Basic syntax and options
- Defaults to 10 lines.
- Use the -n option to set how many lines to print (e.g. `head -n 20`).
โฌ๏ธ
tail
Prints only the last few lines of a file.
Basic syntax and options
- Defaults to 10 lines.
- The -f (follow) option is often used to watch a log file in real time.
tail -f /var/log/syslog โ keep showing new content as it's added to the log file.
๐
sort
Sorts text line by line.
Basic syntax and options
- Sorts alphabetically by default.
- The -n option sorts numerically.
- The -r option reverses the sort order.
๐งน
uniq
Collapses consecutive duplicate lines into one.
Basic syntax and options
- Doesn't work properly on unsorted data, so it's usually paired with `sort` (`sort file | uniq`).
- The -c option also shows how many times each line repeated.
๐ข
wc
Counts the lines, words, and characters (bytes) in a file.
Basic syntax and options
- -l prints only the line count, -w only the word count, -c only the byte count.
wc -l file.txt โ print the total number of lines in the file.
๐
less
A viewer that lets you scroll through a file's contents a screen at a time.
Basic syntax and options
- Unlike cat, it doesn't load the whole file into memory at once, so it opens large files quickly and lightly.
- Scroll with the arrow keys, search with `/searchterm`, and quit with q.
๐
diff
Compares two files line by line and shows the differences.
Basic syntax and options
- Shows which lines were added, removed, or changed, which is useful for comparing code or config files.
diff old.txt new.txt โ show the lines that differ between the two files.
Processes & System
โ๏ธ
ps
Shows a list of currently running processes.
Basic syntax and options
- `ps aux` is the most common form โ it shows detailed info for every process from every user.
ps aux | grep nginx โ find only the running processes whose name contains nginx.
๐
top
Shows running processes in real time, ranked by CPU and memory usage.
Basic syntax and options
- The screen refreshes periodically on its own; press q to quit.
- htop is a widely used, more user-friendly upgrade (requires separate installation).
๐
kill
Sends a signal to a process to terminate it.
Basic syntax and options
- Used as `kill processID (PID)`.
- If the default signal doesn't stop it, use the -9 (force kill) option.
kill -9 1234 โ force-terminate the process with PID 1234.
๐ฅ
killall
Terminates multiple processes at once by name.
Basic syntax and options
- Convenient because you can kill processes by their executable name without knowing the PID.
killall firefox โ terminate every process named firefox.
๐งพ
jobs
Shows the background or stopped jobs in the current terminal session.
Basic syntax and options
โฏ๏ธ
bg / fg
Resumes a stopped job in the background (bg) or foreground (fg).
Basic syntax and options
- After pausing a job with Ctrl+Z, you can continue it in the background with `bg`, or bring it back to the foreground with `fg`.
๐งฐ
systemctl
Manages services (daemons) on systemd-based Linux systems.
Basic syntax and options
- `systemctl start/stop/restart servicename` starts, stops, or restarts a service.
- `systemctl status servicename` checks its current status.
- `systemctl enable servicename` sets it to start automatically at boot.
systemctl restart nginx โ restart the nginx service.
๐ฝ
df
Shows the total size and usage of disks (filesystems).
Basic syntax and options
- The -h option shows sizes in human-readable units (like GB).
df -h โ show the usage of each disk partition in GB/MB.
๐ฆ
du
Shows how much space a file or directory is using.
Basic syntax and options
- `du -sh foldername` shows the total size of an entire folder at a glance.
๐ง
free
Shows the system's memory (RAM) usage.
Basic syntax and options
- The -h option displays it in human-readable units.
Network
๐ถ
ping
Sends a signal to a given address to check network connectivity and response time.
Basic syntax and options
- Keeps showing response times until you press Ctrl+C.
ping google.com โ keep checking the response time to google.com.
๐
curl
Requests data from a URL and retrieves the response โ commonly used for testing APIs.
Basic syntax and options
- By default it prints the response body to the screen.
- Use -O to save the response to a file, and -I to check only the headers.
curl -I https://example.com โ check only the response headers for that site.
โฌ๏ธ
wget
Downloads the file a URL points to.
Basic syntax and options
- More specialized for file downloads than curl.
- If a download is interrupted, the -c option lets you resume it.
๐
ssh
Securely connects (remote login) to a remote server.
Basic syntax and options
- Communication is encrypted, letting you safely control a remote server.
ssh user@192.168.0.10 โ connect to the server at that IP as the user account.
๐ค
scp
Securely copies files between local and remote machines using SSH.
Basic syntax and options
- Used as `scp source destination`; a path on the server side is written as `user@host:path`.
scp file.txt user@server:/home/user/ โ send a local file to a remote server.
๐งญ
ip / ifconfig
Checks configuration info like a network interface's IP address.
Basic syntax and options
- `ip addr` is the standard command on modern distros, while `ifconfig` is the older way (some distros need to install it separately).
๐
netstat / ss
Checks currently open network connections and port status.
Basic syntax and options
- `ss` is the modern command that replaces netstat, and it runs faster.
๐ฐ๏ธ
traceroute
Shows the network route (hops) packets take to reach a destination.
Basic syntax and options
- Used to check where along the route a delay or problem is occurring.
Archive & Misc
๐ฆ
tar
Bundles multiple files into a single archive file, or unpacks one.
Basic syntax and options
- Often bundles and compresses at the same time, so it helps to remember the option combinations.
- c=create, x=extract, z=gzip compression, v=show progress, f=specify filename.
- Examples: -czvf (compress and bundle), -xzvf (extract while decompressing).
tar -czvf backup.tar.gz ./project โ compress the project folder with gzip into backup.tar.gz.
๐๏ธ
gzip / gunzip
Compresses a file to .gz format (gzip) or decompresses it (gunzip).
Basic syntax and options
- Can only compress a single file, so it's often used together with tar when bundling multiple files.
๐
zip / unzip
Compresses or extracts files and folders in .zip format.
Basic syntax and options
- A compression format that's well compatible with other operating systems like Windows.
unzip archive.zip โ extract the contents of archive.zip into the current folder.
๐
history
Shows a list of previously entered commands.
Basic syntax and options
- Typing `!number` re-runs the command at that number in the history.
๐ท๏ธ
alias
Registers a short nickname for a long command you use often.
Basic syntax and options
- Adding it to a shell config file (like ~/.bashrc) makes it apply automatically every time you open a new terminal.
alias ll="ls -al" โ register ll so that typing it alone runs ls -al.
๐
man
Opens the official manual (documentation) for a command.
Basic syntax and options
- Scroll with the arrow keys, search inside the manual with `/searchterm`, and quit with q.
man ls โ see the full options and usage of the ls command.
๐ฌ
echo
Prints a string or variable's value to the screen.
Basic syntax and options
- Also often used in shell scripts to check a value, or to append content to a file.
echo "text" >> file.txt โ append a line containing "text" to the end of file.txt.
๐
which
Finds the path of the program file that actually runs for a given command.
Basic syntax and options
- Useful for checking which file actually runs when multiple versions of the same command name are installed.
which python3 โ show the actual file path that runs for the python3 command.
vi/vim Editor
๐
vi ๊ธฐ๋ณธ ๋ชจ๋
vi (vim) has three modes โ command, insert, and command-line โ and everything you do involves switching between them.
Basic syntax and options
- Opening a file starts you in command mode (you can't type text directly).
- Pressing i, a, etc. switches to insert mode, where you can type text.
- Pressing Esc in insert mode returns you to command mode.
- Pressing colon (:) in command mode switches to command-line mode, where you enter commands like save and quit.
vi file.txt โ open file.txt in vi.
โ๏ธ
i / a / I / A
Switches to insert mode and starts typing text.
Basic syntax and options
- i: starts inserting "before" the cursor position.
- a: starts inserting "after" the cursor position.
- I: starts inserting at the beginning of the line; A: at the end of the line.
๐
Esc
Returns to command mode from insert mode or another mode.
Basic syntax and options
- Almost everything in vi happens in command mode, so it helps to make a habit of pressing Esc to return to command mode before your next action.
๐งญ
h / j / k / l
Moves the cursor one space at a time (left/down/up/right).
Basic syntax and options
- h: left, j: down, k: up, l: right.
- Designed so you can move quickly without taking your hands off the home row, unlike the arrow keys.
๐
w / b / e, 0 / $
Moves the cursor by word or by line.
Basic syntax and options
- w: moves to the start of the next word, b: to the start of the previous word.
- e: moves to the end of the current (or next) word.
- 0: moves to the beginning of the line, $: to the end of the line.
๐
gg / G / :์ซ์
Jumps straight to a position anywhere in the document.
Basic syntax and options
- gg: jumps to the very first line of the document.
- G: jumps to the very last line of the document.
- Typing `:number` and pressing Enter jumps straight to that line number.
:42 โ jump to line 42.
โ๏ธ
dd
Deletes (cuts) the current line.
Basic syntax and options
- A deleted line is copied into a temporary register, so you can paste it with p.
- Combined with a number (`3dd`), it deletes several lines at once.
๐
yy
Copies (yanks) the current line.
Basic syntax and options
- Unlike dd, it copies without deleting.
- Adding a number, like `3yy`, copies several lines at once.
๐ฅ
p / P
Pastes content you've copied or deleted.
Basic syntax and options
- p: pastes "after" the cursor line/position.
- P: pastes "before" the cursor line/position.
โฉ๏ธ
u / Ctrl+r
Undoes or redoes an action.
Basic syntax and options
- u: undoes the most recent action.
- Ctrl+r: redoes an action you undid.
๐ก
x
Deletes a single character under the cursor.
Basic syntax and options
- Handy for fixing a small typo right away.
- Adding a number, like `3x`, deletes several characters at once.
๐๏ธ
v / V
Switches to visual mode for selecting a range.
Basic syntax and options
- v: selects a range character by character.
- V: selects a range line by line.
- After selecting a range, pressing d (delete), y (copy), etc. applies it to the whole selection.
๐
/ ํจํด, ? ํจํด, n / N
Searches for a string within the document.
Basic syntax and options
- /pattern + Enter: searches forward from the cursor.
- ?pattern + Enter: searches backward from the cursor.
- n: jumps to the next match in the same direction, N: in the opposite direction.
/error โ find where "error" appears in the document.
๐
:%s/old/new/g
Finds and replaces a string across the whole document or a given range.
Basic syntax and options
- Leaving out % applies it only to the current line; leaving out g replaces only the first match on each line.
- Adding c at the end (`:%s/old/new/gc`) asks for confirmation before each replacement.
:%s/foo/bar/g โ replace every foo with bar throughout the document.
๐พ
:w / :q / :wq / :q!
Saves your work or quits the editor.
Basic syntax and options
- :w โ saves the current content.
- :q โ quits the editor (when there are no unsaved changes).
- :wq (or ZZ) โ saves and quits.
- :q! โ force-quits without saving changes.
:wq โ save and quit vi.
๐ข
:set number
Shows line numbers at the start of each line.
Basic syntax and options
- Turn it on with `:set number` (or `:set nu`), and off again with `:set nonumber`.
๐
.
Pressing the period (.) key repeats the edit you just made, exactly once more.
Basic syntax and options
- Especially useful when you need to apply the same edit in several places throughout a document.