PUBLIC REFERENCE // LINUX
Linux & Bash Commands
Linux permissions and process management are built on a small set of primitives — users, groups, and signals — that show up again and again once you learn to read them, whether you're debugging a stuck process or fixing a permission error.
- Commands
- 117
- Sections
- 10
- Source policy
- Official links
FIELD NOTES
Decisions worth remembering
- chmod's numeric mode is just three octal digits (owner/group/other) added from read=4, write=2, execute=1 — 755 means rwxr-xr-x.
- Always quote variables in scripts ("$var"), not just for spaces — an unset or empty variable can otherwise make a command like rm -rf $dir/* dangerously ambiguous.
- kill sends SIGTERM by default, which a process can ignore or handle gracefully; kill -9 (SIGKILL) can't be caught and should be a last resort since it skips cleanup.
- Piping into xargs is usually faster and safer than backticks or $() in a loop for running a command over many files, and it handles filenames with spaces better with -print0/-0.
LINUX // REFERENCE
File & Directory
ls -laList all files with details (including hidden)
ls -lhList files with human-readable sizes
cd /path/to/dirChange directory
cd ~Go to home directory
cd -Go to previous directory
pwdPrint current working directory
mkdir -p path/to/dirCreate directory (and parents)
cp file.txt dest/Copy file to destination
cp -r src/ dest/Copy directory recursively
mv file.txt newname.txtMove or rename a file
rm file.txtRemove a file
Safety: This command can discard data, stop work, or remove resources. Replace placeholders and verify the exact target in a disposable environment first.
rm -rf directory/Remove directory and all contents
Safety: This command can discard data, stop work, or remove resources. Replace placeholders and verify the exact target in a disposable environment first.
touch file.txtCreate empty file or update timestamp
cat file.txtDisplay file contents
less file.txtScroll through file contents
head -n 20 file.txtShow first 20 lines of a file
tail -n 20 file.txtShow last 20 lines of a file
tail -f logfile.logFollow file in real-time (for logs)
find . -name "*.txt"Find files by name pattern
find . -type f -mtime -7Find files modified in last 7 days
ln -s /path/to/file link-nameCreate a symbolic link
stat file.txtShow file metadata (size, dates, permissions)
file document.pdfDetect file type
wc -l file.txtCount lines in a file
diff file1.txt file2.txtShow differences between two files
LINUX // REFERENCE
File Permissions
chmod 755 file.shSet permissions (rwxr-xr-x)
chmod +x script.shMake a file executable
chmod -R 644 directory/Set permissions recursively
Safety: This command can discard data, stop work, or remove resources. Replace placeholders and verify the exact target in a disposable environment first.
chown user:group file.txtChange file owner and group
chown -R user directory/Change ownership recursively
ls -lView permissions (first column: -rwxr-xr-x)
umask 022Set default permission mask (new files: 644)
LINUX // REFERENCE
Process Management
ps auxList all running processes
ps aux | grep nginxFind processes by name
topInteractive process viewer
htopBetter interactive process viewer (if installed)
kill PIDSend SIGTERM to a process
kill -9 PIDForce kill a process (SIGKILL)
Safety: This command can discard data, stop work, or remove resources. Replace placeholders and verify the exact target in a disposable environment first.
killall nginxKill all processes by name
pkill -f "python script.py"Kill processes matching a pattern
command &Run command in the background
jobsList background jobs
fg %1Bring job #1 to foreground
nohup command &Run command immune to hangups
lsof -i :8080Show process using port 8080
LINUX // REFERENCE
Networking
curl https://example.comMake an HTTP GET request
curl -X POST -H "Content-Type: application/json" -d '{"key":"val"}' https://api.example.comPOST JSON data
curl -o file.zip https://example.com/file.zipDownload a file
wget https://example.com/file.zipDownload a file (alternative)
ping google.comTest connectivity to a host
traceroute google.comTrace route to a host
dig google.comDNS lookup
nslookup google.comDNS query (alternative)
netstat -tulnShow all listening ports
ss -tulnShow listening sockets (modern netstat)
ifconfigShow network interface configuration
ip addr showShow IP addresses (modern ifconfig)
ssh user@hostConnect to remote host via SSH
scp file.txt user@host:/path/Copy file to remote host
rsync -avz src/ user@host:/dest/Sync files to remote (efficient)
LINUX // REFERENCE
Search & Find
grep "pattern" file.txtSearch for pattern in file
grep -r "pattern" ./srcRecursive search in directory
grep -i "pattern" file.txtCase-insensitive search
grep -n "pattern" file.txtShow line numbers with matches
grep -v "pattern" file.txtShow lines NOT matching pattern
grep -l "pattern" *.txtShow only filenames with matches
grep -E 'pattern1|pattern2' file.txtExtended regex (OR patterns)
find . -name "*.log" -deleteFind and delete matching files
locate filenameFast file search using database
which python3Find full path of a command
whereis python3Show binary, man page, and source paths
LINUX // REFERENCE
Text Processing
sed 's/old/new/g' file.txtReplace all occurrences of text
sed -i 's/old/new/g' file.txtReplace in-place (edit file)
sed -n '5,10p' file.txtPrint lines 5 to 10
awk '{print $1, $3}' file.txtPrint columns 1 and 3
awk -F',' '{print $2}' file.csvSet delimiter and print column 2
awk 'NR==5' file.txtPrint line number 5
sort file.txtSort lines alphabetically
sort -n numbers.txtSort numerically
sort -rn numbers.txtSort numerically in reverse
uniq file.txtRemove consecutive duplicate lines
sort file.txt | uniq -cCount occurrences of each unique line
cut -d',' -f2 file.csvExtract column 2 from CSV
tr a-z A-ZTranslate lowercase to uppercase
tr -d "[:space:]"Delete all whitespace
xargs rmPass stdin as arguments to a command
Safety: This command can discard data, stop work, or remove resources. Replace placeholders and verify the exact target in a disposable environment first.
jq .name data.jsonQuery JSON data (if jq installed)
LINUX // REFERENCE
Compression & Archives
tar -czf archive.tar.gz directory/Create gzipped tar archive
tar -xzf archive.tar.gzExtract gzipped tar archive
tar -xzf archive.tar.gz -C /dest/Extract to specific directory
tar -tf archive.tar.gzList contents without extracting
zip -r archive.zip directory/Create zip archive
unzip archive.zipExtract zip archive
gzip file.txtCompress file (creates file.txt.gz)
gunzip file.txt.gzDecompress .gz file
LINUX // REFERENCE
Disk Usage
df -hShow disk space usage (human-readable)
du -sh directory/Show directory size
du -sh *Show sizes of all items in current dir
du -ah | sort -rh | head -20Find the 20 largest files/dirs
ncduInteractive disk usage viewer (if installed)
LINUX // REFERENCE
User Management
whoamiShow current user
idShow user ID, group ID, and groups
sudo commandRun command as superuser
sudo su -Switch to root user
useradd -m usernameCreate a new user with home directory
passwd usernameSet or change a user password
usermod -aG sudo usernameAdd user to sudo group
groups usernameList groups for a user
lastShow recent login history
LINUX // REFERENCE
Environment Variables
echo $HOMEPrint an environment variable
printenvList all environment variables
export MY_VAR="value"Set an environment variable for current session
unset MY_VARRemove an environment variable
MY_VAR=value commandSet variable only for one command
envPrint all environment variables
source ~/.bashrcReload shell config file
echo "export MY_VAR=value" >> ~/.bashrcPersist a variable across sessions
COMMON QUESTIONS
Linux command FAQ
What is the difference between chmod 755 and chmod 644?
755 gives the owner rwx and everyone else r-x (used for directories and executables); 644 gives the owner rw and everyone else read-only (used for regular files).
How do I find which process is using a specific port?
sudo lsof -i :8080 or sudo ss -tulpn | grep 8080.
What does the number after a signal mean, like kill -9?
It's the signal number — 9 is SIGKILL (immediate, un-catchable termination); 15 (the default) is SIGTERM, a request the process can catch and clean up after.
Why does rm sometimes fail with "Directory not empty"?
Plain rm won't remove directories at all — you need rm -r for recursive removal, and -f to suppress prompts or ignore missing files.