|$ 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

LinuxCLIBashBeginner🎯Free Tools
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
2touch app.js
3mkdir src
4mkdir -p src/components/ui # create parents
5
6# Copy, move, remove
7cp app.js app-backup.js
8mv app.js src/
9rm app-backup.js
10rm -rf node_modules # recursive, force
11
12# View file contents
13cat README.md
14less /var/log/syslog # scroll with arrow keys, q to quit
15head -n 20 app.js
16tail -f /var/log/nginx.log # follow live changes
17
18# Find files
19find . -name "*.tsx"
20find /var/log -type f -mtime +7 # files older than 7 days
21find . -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
2ls -la | wc -l
3
4# Chain multiple commands
5cat access.log | grep 404 | awk '{print $1}' | sort | uniq -c | sort -nr
6
7# Redirect output to a file
8ls -la > listing.txt # overwrite
9ls -la >> listing.txt # append
10
11# Redirect stderr to a file
12node build.js 2> errors.log
13
14# Redirect both stdout and stderr
15node build.js > output.log 2>&1
16
17# Use a file as stdin
18sort < unsorted.txt
Wildcards & Globbing
globbing.sh
Bash
1# Match any characters
2ls *.js
3
4# Match a single character
5ls file?.txt
6
7# Match character ranges
8ls [0-9]*.log
9
10# Recursive glob with **
11ls **/*.test.ts
12
13# Quote to prevent shell expansion
14echo "*"
Environment Variables
environment.sh
Bash
1# View all environment variables
2env
3
4# Print a specific variable
5echo $HOME
6echo $PATH
7
8# Set a variable for the current session
9export NODE_ENV=production
10
11# Add to PATH
12export PATH="$HOME/.local/bin:$PATH"
13
14# Make permanent by adding to ~/.bashrc or ~/.zshrc
15echo 'export PATH="$HOME/.local/bin:$PATH"' >> ~/.bashrc
16source ~/.bashrc
Command History
history.sh
Bash
1history # show recent commands
2!42 # rerun command number 42
3!! # rerun last command
4sudo !! # rerun last command with sudo
5Ctrl + R # reverse search through history