Linux Command Quick Reference
- Basic Commands: clear, history, pwd, man
- File Navigation: ls, cd
- File & Directory Operations: mkdir, touch, cp, rm, rmdir, mv
- Reading & Writing Files: cat, less, echo
- Searching: grep, find
- Chaining & Sorting: Pipe, sort, sed
- Redirection: >, >>, <
- File Permissions: chmod
- System & Processes: ps, kill, df, du
- Archives: tar
- Shell Concepts: Variables, Wildcards, Aliases, which, Job control
Basic Commands
clear
Clear your terminal window. You can also use control + l.
history
Shows all previously entered commands. Use the up arrow and down arrow to navigate command history.
pwd
Prints the full path of the directory you're currently in. Always helpful when you get turned around navigating with cd.
man
Shows the manual page for any command. This is the most important thing you can learn early on — how to find help for things you don't know. Before searching the internet, use man.
man ls # read all options and examples
man grep
Inside man, press / to search, q to quit. Most commands also accept --help for a quick overview.
Tip: use man to look up every flag listed here. For example, man rm shows the full list of options beyond -r and -f.
File & Directory Operations
mkdir
Creates a directory.
mkdir -p a/b/c # create nested directories automatically
mkdir -v mydir # verbose output
mkdir my\ dir # directories with spaces: escape the space
touch
Creates a blank file. If the file exists, it updates the timestamp without changing contents.
cp
Copies files or directories.
cp file1.txt file2.txt # copy a file
cp -r mydir mydir-copy # copy a directory (recursive)
cp -p file1.txt file2.txt # preserve permissions/timestamps
rm
Removes files and directories. THIS ACTION CANNOT BE UNDONE!!
rm myfile.txt # remove a file
rm -rf mydir # remove directory recursively (force)
rm -v myfile.txt # verbose output
rmdir
Removes an empty directory only. If there's content inside, use rm -r instead.
mv
Moves or renames files and directories.
mv oldname.txt newname.txt # rename
mv file.txt /path/to/dest/ # move to directory
Reading & Writing Files
cat
Displays the contents of a file on the terminal. Not suitable for large files — use less instead.
cat file.txt
less
Opens a file in scrollable read-only mode. Press j to scroll down, k to scroll up. Press q to exit. Press / to search, n/N for next/previous match.
echo
Prints text to the terminal or writes it into a file using redirection.
echo "content" > file.txt # overwrite
echo "line" >> file.txt # append
Searching
grep
Searches for text in files.
grep "word" file.txt # search a file
grep -R "word" /path/to/dir/ # search recursively
grep -i "word" file.txt # case-insensitive
grep -c "word" file.txt # count matches
grep -n "word" file.txt # show line numbers
grep -v "error" logfile.txt # invert matching
find
Searches for files and directories by name, type, or size.
find . -name "*.txt" # by name
find . -type d -name "logs" # by type (directory)
find . -type f -size +100M # files larger than 100MB
Chaining & Sorting
Pipe (|)
The pipe symbol | chains commands together, sending the output of one as input to the next.
ls -t | head -n 5
Shows the 5 most recently modified files. ls -t orders by modification time, and head limits the output.
sort
Sorts lines of input.
sort file.txt
sort -n numbers.txt # numeric sort
sort -r file.txt # reverse
sort -u file.txt # unique only
sed
Finds and replaces text in files.
sed 's/old/new/g' file.txt
The g replaces all occurrences per line. Redirect output to a new file or use -i to edit in-place.
Redirection
> and >>
ls > filelist.txt # overwrite
echo "line" >> file.txt # append
sort < unsorted.txt > sorted.txt # redirect from file
File Permissions
chmod
Changes file and directory permissions. Four digit notation: 4=read, 2=write, 1=execute.
ls -l # see current permissions: drwxr-xr-x
chmod 755 script.sh # rwxr-xr-x
chmod 644 file.txt # rw-r--r--
chmod 600 secret.key # rw-------
chmod u+x script.sh # add execute for owner
chmod g-w file.txt # remove write for group
System & Processes
ps
Shows running processes.
ps aux # all processes
ps aux | grep nginx # find a specific process
kill
Stops a running process by its process ID (PID).
kill 1234 # polite request (SIGTERM)
kill -9 1234 # force kill (SIGKILL - use as last resort)
pkill nginx # kill by name
df
Shows available disk space.
df -h # human-readable sizes
Check this if you get "No space left on device" errors.
du
Shows disk usage of a file or directory.
du -sh mydir/ # total size in human-readable format
Archives
tar
tar -czvf archive.tar.gz mydir/ # create gzipped archive
tar -xzvf archive.tar.gz # extract
tar -tzvf archive.tar.gz # list contents
Shell Concepts
Variables
The shell stores values in named variables, referenced with $.
export MYVAR="hello"
echo $MYVAR
Common built-ins: $? (last exit status), $HOME, $PATH, $USER.
Wildcards
Match multiple files with a single pattern.
rm *.txt # all .txt files
Aliases
A shortcut for a longer command. Define in ~/.bashrc.
alias ll='ls -la'
alias cls='clear'
List all with alias, remove one with unalias ll.
which
Finds the full path to a command:
which python
Job control
Run commands in the background:
cp largefile.txt /dest/ &
jobs
fg %1
Press Ctrl + Z to pause, then fg %1 or bg %1 to resume.