|$ curl https://forge-ai.dev/api/markdown?path=docs/linux/cli
$cat docs/linux-command-line.md
updated Recently·25 min read·published
Linux Command Line
Introduction
The command line is the fastest way to interact with Linux. Once you learn a core set of commands and concepts, you can manage files, inspect system state, and chain tools together far more efficiently than with a GUI.
File Operations
file-operations.sh
Bash
| 1 | # Create files and directories |
| 2 | touch app.js |
| 3 | mkdir src |
| 4 | mkdir -p src/components/ui # create parents |
| 5 | |
| 6 | # Copy, move, remove |
| 7 | cp app.js app-backup.js |
| 8 | mv app.js src/ |
| 9 | rm app-backup.js |
| 10 | rm -rf node_modules # recursive, force |
| 11 | |
| 12 | # View file contents |
| 13 | cat README.md |
| 14 | less /var/log/syslog # scroll with arrow keys, q to quit |
| 15 | head -n 20 app.js |
| 16 | tail -f /var/log/nginx.log # follow live changes |
| 17 | |
| 18 | # Find files |
| 19 | find . -name "*.tsx" |
| 20 | find /var/log -type f -mtime +7 # files older than 7 days |
| 21 | find . -type d -name "node_modules" |
⚠
warning
Be careful with rm -rf. It permanently deletes files without sending them to a trash bin. Double-check the path before pressing Enter.
Pipes & Redirection
Pipes send the output of one command into another. Redirection writes output to files or reads input from files.
pipes.sh
Bash
| 1 | # Pipe: count lines in output |
| 2 | ls -la | wc -l |
| 3 | |
| 4 | # Chain multiple commands |
| 5 | cat access.log | grep 404 | awk '{print $1}' | sort | uniq -c | sort -nr |
| 6 | |
| 7 | # Redirect output to a file |
| 8 | ls -la > listing.txt # overwrite |
| 9 | ls -la >> listing.txt # append |
| 10 | |
| 11 | # Redirect stderr to a file |
| 12 | node build.js 2> errors.log |
| 13 | |
| 14 | # Redirect both stdout and stderr |
| 15 | node build.js > output.log 2>&1 |
| 16 | |
| 17 | # Use a file as stdin |
| 18 | sort < unsorted.txt |
Wildcards & Globbing
globbing.sh
Bash
| 1 | # Match any characters |
| 2 | ls *.js |
| 3 | |
| 4 | # Match a single character |
| 5 | ls file?.txt |
| 6 | |
| 7 | # Match character ranges |
| 8 | ls [0-9]*.log |
| 9 | |
| 10 | # Recursive glob with ** |
| 11 | ls **/*.test.ts |
| 12 | |
| 13 | # Quote to prevent shell expansion |
| 14 | echo "*" |
Environment Variables
environment.sh
Bash
| 1 | # View all environment variables |
| 2 | env |
| 3 | |
| 4 | # Print a specific variable |
| 5 | echo $HOME |
| 6 | echo $PATH |
| 7 | |
| 8 | # Set a variable for the current session |
| 9 | export NODE_ENV=production |
| 10 | |
| 11 | # Add to PATH |
| 12 | export PATH="$HOME/.local/bin:$PATH" |
| 13 | |
| 14 | # Make permanent by adding to ~/.bashrc or ~/.zshrc |
| 15 | echo 'export PATH="$HOME/.local/bin:$PATH"' >> ~/.bashrc |
| 16 | source ~/.bashrc |
Command History
history.sh
Bash
| 1 | history # show recent commands |
| 2 | !42 # rerun command number 42 |
| 3 | !! # rerun last command |
| 4 | sudo !! # rerun last command with sudo |
| 5 | Ctrl + R # reverse search through history |