Moving Around
# Print current directory
pwd
# List files
ls
# Long format
ls -l
# Show hidden files
ls -la
# Change directory
cd /path/to/folder
# Go home
cd ~
# Go back one directory
cd ..
# Go to previous directory
cd -
A practical command-line reference for technical interviews, Linux navigation, scripting basics, file permissions, processes, networking, logs, and automation.
# Create empty file
touch notes.txt
# Create directory
mkdir projects
# Create nested directories
mkdir -p app/src/components
# Copy file
cp file.txt backup.txt
# Copy directory
cp -r folder backup-folder
# Move or rename
mv old.txt new.txt
# Remove file
rm file.txt
# Remove directory and contents
rm -r folder
# Print entire file
cat file.txt
# View file page by page
less file.txt
# First 10 lines
head file.txt
# Last 10 lines
tail file.txt
# Follow file updates live
tail -f app.log
# Make script executable
chmod +x script.sh
# Owner can read/write/execute, group and others can read/execute
chmod 755 script.sh
# Owner can read/write, group and others can read
chmod 644 file.txt
# Change file owner
sudo chown user:user file.txt
# Show file permissions
ls -l
read = 4
write = 2
execute = 1
7 = 4 + 2 + 1 = rwx
6 = 4 + 2 = rw-
5 = 4 + 1 = r-x
4 = 4 = r--
# Search text inside a file
grep "error" app.log
# Case-insensitive search
grep -i "error" app.log
# Recursive search
grep -r "TODO" .
# Show line numbers
grep -n "error" app.log
# Find file by name
find . -name "app.py"
# Find all .js files
find . -name "*.js"
# Count lines, words, and characters
wc file.txt
# Sort lines
sort names.txt
# Remove duplicate lines
uniq names.txt
# Sort and remove duplicates
sort names.txt | uniq
# Print selected columns
awk '{print $1}' file.txt
# Replace text
sed 's/old/new/g' file.txt
# Pipe output from one command into another
cat app.log | grep "error"
# Count matching lines
grep "error" app.log | wc -l
# Sort unique IPs from log file
awk '{print $1}' access.log | sort | uniq
# Write output to file
echo "hello" > file.txt
# Append output to file
echo "new line" >> file.txt
# Redirect errors
command 2> errors.txt
# Redirect output and errors
command > output.txt 2>&1
# Discard output
command > /dev/null 2>&1
# Show running processes
ps aux
# Search for a process
ps aux | grep nginx
# Interactive process viewer
top
# Better process viewer if installed
htop
# Kill process by PID
kill 1234
# Force kill process
kill -9 1234
# Kill by process name
pkill nginx
# Run command in background
python3 server.py &
# Show background jobs
jobs
# Bring job to foreground
fg
# Stop current foreground process
Ctrl + Z
# Stop current command
Ctrl + C
# Show IP addresses
ip addr
# Show routes
ip route
# Test connectivity
ping example.com
# DNS lookup
nslookup example.com
# Better DNS lookup if installed
dig example.com
# Show listening ports
ss -tulnp
# Test HTTP request
curl -I https://example.com
# SSH into a server
ssh [email protected]
# SSH with custom port
ssh -p 2222 [email protected]
# Copy file to remote server
scp file.txt [email protected]:/home/user/
# Copy file from remote server
scp [email protected]:/home/user/file.txt .
# Kernel and system info
uname -a
# Disk usage
df -h
# Directory size
du -sh folder
# Memory usage
free -h
# CPU info
lscpu
# Current user
whoami
# Logged-in users
who
# View system logs
sudo journalctl
# Recent logs
sudo journalctl -n 50
# Follow logs live
sudo journalctl -f
# Authentication logs
sudo tail -n 50 /var/log/auth.log
# Syslog
sudo tail -n 50 /var/log/syslog
#!/bin/bash
name="Xavier"
echo "Hello, $name"
if [ -f "file.txt" ]; then
echo "File exists"
else
echo "File does not exist"
fi
#!/bin/bash
echo "Script name: $0"
echo "First argument: $1"
echo "Second argument: $2"
echo "All arguments: $@"
echo "Number of arguments: $#"
#!/bin/bash
for file in *.txt; do
echo "Found file: $file"
done
count=1
while [ $count -le 5 ]; do
echo "Count: $count"
count=$((count + 1))
done
#!/bin/bash
greet() {
local name="$1"
echo "Hello, $name"
}
greet "Xavier"
# Show command location
which python3
# Show environment variable
echo $PATH
# Show previous command exit code
echo $?
# Make script executable
chmod +x script.sh
# Run script
./script.sh
# Run script with bash
bash script.sh