Linux Commands Cheatsheet
1. Finding and Identifying Commands
The type command identifies how a command will be interpreted (built-in, external, alias).
# Identify 'cd' as a built-in command
type cd
# Show the location of an external command
type cal
# Show all locations that match (alias, built-in, external)
type -a echo
The which command locates an executable in the folders defined in the $PATH variable.
which ls
which cal
The whereis command searches for binaries, source files, and manual pages of a command.
whereis ls
2. Getting Help and Documentation
man displays the manual page of a command.
man man
man ls
whatis (or man -f) shows a brief description of a command and the manual section.
whatis ls
info provides detailed documentation. Useful for complex commands like ls or grep. (Use 'q' to quit)
info ls
--help is an option most commands accept to show quick usage and options.
cat --help
locate searches files and directories using a system database (very fast).
locate gshadow
Additional "Readme" or documentation files are usually in /usr/share/doc.
ls /usr/share/doc
3. Aliases and Functions (Bash)
alias creates shortcuts or alternative names for longer commands.
# Create an alias 'datecmd' that runs 'date'
alias datecmd="date"
# Now you can type 'datecmd' to execute the command
datecmd
Functions allow grouping several commands under one name.
# Define a function
test_function () {
ls -l /home
echo "End of function"
}
# Call (execute) the function
test_function
4. Chaining Commands
; (semicolon): Executes commands in sequence regardless of success.
# Show January calendar AND THEN February calendar
cal 1 2030 ; cal 2 2030
| (pipe): Chains commands. Output of first is input of second.
# List all files and filter lines containing "bash"
grep bash /etc/passwd
&& (logical AND): Second command runs only if first succeeds.
# Show "done" ONLY if /etc/ppp/ exists
ls /etc/ppp/ && echo "done"
|| (logical OR): Second command runs only if first fails.
# Try to run 'sl' (usually fails if not installed)
# If it fails, show an error message
sl /etc/ppp/ || echo "'sl' command failed"
5. File System and Wildcards
pwd (Print Working Directory) shows the current directory.
pwd
* (asterisk): Wildcard representing zero or more characters.
# Show everything starting with 's' in /etc/
echo /etc/s*
? (question mark): Wildcard representing exactly one character.
# Show files in /etc/ starting with 't' with exactly 7 more characters
echo /etc/t???????
[ ] (brackets): Represents one character from a set or range.
# Show files starting with 'g' or 'u' in /etc/
echo /etc/[gu]*
# Show files starting with a number
echo /etc/[0-9]*
6. File Manipulation
cp -v copies a file and shows a verbose message.
# Copy 'hosts' file to current directory
cp -v /etc/hosts .
touch creates an empty file if it doesn't exist, or updates modification date.
# Check file date
ls -l Firefox_wallpaper.png
# Update modification date
touch Firefox_wallpaper.png
tar -cf creates a compressed archive. -c (create), -f (file).
# Create a .tar archive 'compressed_image' containing 'Firefox_wallpaper.png'
tar -cf compressed_image.tar Firefox_wallpaper.png
# Verify creation
ls -l compressed_image.tar
7. Viewing and Processing Text
less is an interactive file viewer (pager). Use 'q' to quit, 'h' for help.
less /etc/sysctl.conf
head and tail show first or last 10 lines (default) of a file.
head /etc/sysctl.conf
# Show last 5 lines
tail -5 /etc/sysctl.conf
sort sorts file lines (alphabetically by default).
sort /etc/sysctl.conf
wc (Word Count) counts lines, words, and bytes.
# Shows: lines, words, bytes, filename
wc /etc/sysctl.conf
cut extracts columns. Useful for CSV or delimited files.
# From 'mypasswd' (':' delimited):
# -d: delimiter
# -f1,5-7 (fields 1 and 5-7)
cut -d: -f1,5-7 mypasswd
grep filters lines matching a pattern (regex).
# Find lines containing "bash" in /etc/passwd
grep bash /etc/passwd
# '.' is a wildcard for ANY character
# Search for 'r', two chars, then 'f'
grep 'r..f' red.txt
