Essential Bash and shell scripting reference covering file operations, text processing, variables, loops, conditionals, permissions, and networking commands.
| Code / Syntax | Description |
|---|---|
ls -la | List all files with details |
cp src dest | Copy file or directory |
cp -r dir/ dest/ | Copy directory recursively |
mv old new | Move or rename file |
rm file | Delete a file |
rm -rf dir/ | Delete directory recursively (force) |
mkdir -p path/to/dir | Create nested directories |
find . -name '*.log' -type f | Find files by name pattern |
find . -mtime -7 -type f | Find files modified in last 7 days |
ln -s target link_name | Create symbolic link |
du -sh dir/ | Show directory size (human-readable) |
df -h | Show disk space usage |
| Code / Syntax | Description |
|---|---|
cat file.txt | Print file contents |
head -n 20 file.txt | Show first 20 lines |
tail -f logfile.log | Follow new lines appended to file |
grep -r 'pattern' dir/ | Search recursively in directory |
grep -i 'text' file | Case-insensitive search |
sed 's/old/new/g' file | Replace all occurrences in file |
awk '{print $1, $3}' file | Print 1st and 3rd columns |
sort file | uniq -c | Count unique lines |
wc -l file.txt | Count lines in file |
cut -d',' -f1,3 file.csv | Extract columns from CSV |
tr 'a-z' 'A-Z' < file | Translate characters (lowercase to upper) |
| Code / Syntax | Description |
|---|---|
VAR='value' | Assign a variable (no spaces around =) |
echo $VAR | Use a variable |
export VAR='value' | Export variable to child processes |
readonly PI=3.14 | Declare a constant variable |
for f in *.txt; do echo $f; done | Loop over files |
for i in {1..10}; do echo $i; done | Loop over range |
while read line; do echo $line; done < file | Read file line by line |
while true; do ...; sleep 5; done | Infinite loop with delay |
arr=(a b c); echo ${arr[1]} | Array access (prints 'b') |
${#arr[@]} | Array length |
$(command) | Command substitution |
| Code / Syntax | Description |
|---|---|
if [ -f file ]; then ...; fi | Check if file exists |
if [ -d dir ]; then ...; fi | Check if directory exists |
if [ -z "$VAR" ]; then ...; fi | Check if string is empty |
if [ -n "$VAR" ]; then ...; fi | Check if string is non-empty |
if [ $a -eq $b ]; then ...; fi | Numeric equality |
if [ "$a" = "$b" ]; then ...; fi | String equality |
if [[ $str =~ ^[0-9]+$ ]]; then ...; fi | Regex match |
cmd1 && cmd2 | Run cmd2 only if cmd1 succeeds |
cmd1 || cmd2 | Run cmd2 only if cmd1 fails |
test -x file && echo 'executable' | Check if file is executable |
| Code / Syntax | Description |
|---|---|
chmod 755 script.sh | rwxr-xr-x (owner all, others read+exec) |
chmod +x script.sh | Add execute permission |
chmod -R 644 dir/ | Set permissions recursively |
chown user:group file | Change file owner and group |
chown -R user dir/ | Change owner recursively |
ls -la | grep '^d' | List only directories |
umask 022 | Set default permission mask |
stat file | Show detailed file permissions and info |
sudo command | Run command as superuser |
su - username | Switch to another user |
| Code / Syntax | Description |
|---|---|
curl -s https://api.example.com | Fetch URL content silently |
curl -o file.zip https://url.com/file.zip | Download file |
curl -X POST -d '{"key":"val"}' -H 'Content-Type: application/json' url | POST JSON request |
wget -q url -O output | Download file with wget |
ssh user@host | SSH into remote machine |
scp file user@host:/path/ | Copy file to remote machine |
rsync -avz src/ user@host:dest/ | Sync directory to remote |
netstat -tlnp | Show listening ports |
ping -c 4 google.com | Ping host (4 packets) |
dig example.com | DNS lookup |
Found this cheat sheet useful? Check out our other references and tools.