Bash Commands

Bash shell scripting commands, syntax, and shortcuts.

File Operations

ls -la

List all files with details including hidden files

cp -r source dest

Copy directory recursively

mv file1 file2

Move or rename file

rm -rf directory

Remove directory and contents recursively

find . -name "*.txt"

Find files by name pattern

locate filename

Quick file search using database

touch file.txt

Create empty file or update timestamp

mkdir -p path/to/dir

Create directory with parent directories

Text Processing

cat file.txt

Display file contents

head -n 10 file.txt

Show first 10 lines of file

tail -f /var/log/syslog

Follow log file in real-time

grep "pattern" file.txt

Search for pattern in file

grep -r "text" /path

Recursively search for text in directory

wc -l file.txt

Count lines in file

sort file.txt

Sort lines alphabetically

uniq file.txt

Remove duplicate adjacent lines

cut -d"," -f1,3 file.csv

Extract fields from CSV

tr "[:lower:]" "[:upper:]"

Translate lowercase to uppercase

Process Management

ps aux

List all running processes

top

Display dynamic real-time process information

htop

Interactive process viewer

kill -9 PID

Force kill process by PID

killall process_name

Kill all processes by name

bg

Resume suspended job in background

fg

Bring background job to foreground

jobs

List active jobs

nohup command &

Run command immune to hangups

pkill -f pattern

Kill processes matching pattern

Permissions & Ownership

chmod 755 file.sh

Set file permissions (rwxr-xr-x)

chmod +x script.sh

Make file executable

chown user:group file

Change file owner and group

chown -R user:group dir

Change ownership recursively

umask 022

Set default file creation permissions

stat file.txt

Display detailed file status

Disk & System Info

df -h

Show disk space usage in human-readable format

du -sh /path

Show directory size summary

du -h --max-depth=1

Show size of subdirectories

free -h

Display memory usage

uname -a

Show system information

uptime

Show system uptime and load

whoami

Display current username

id

Show user and group IDs

Networking

ping -c 4 host

Send 4 ICMP echo requests

curl -O https://url/file

Download file from URL

wget https://url/file

Download file using wget

netstat -tuln

Show listening ports

ss -tuln

Display socket statistics

ifconfig

Show network interface configuration

ip addr show

Display IP addresses

traceroute host

Trace route to host

Archiving & Compression

tar -czf archive.tar.gz /path

Create gzipped tar archive

tar -xzf archive.tar.gz

Extract gzipped tar archive

tar -tzf archive.tar.gz

List contents of tar archive

zip -r archive.zip /path

Create zip archive

unzip archive.zip

Extract zip archive

gzip file.txt

Compress file with gzip

gunzip file.txt.gz

Decompress gzip file

Variables & Environment

export VAR=value

Set environment variable

echo $VAR

Display variable value

env

Show all environment variables

printenv VAR

Print specific environment variable

unset VAR

Remove variable

readonly VAR=value

Create read-only variable

Command History

history

Show command history

!!

Execute last command

!n

Execute command number n from history

!string

Execute most recent command starting with string

Ctrl+R

Search command history interactively

history -c

Clear command history

Redirection & Pipes

command > file

Redirect output to file (overwrite)

command >> file

Redirect output to file (append)

command 2> file

Redirect stderr to file

command &> file

Redirect both stdout and stderr

command1 | command2

Pipe output to another command

command < file

Read input from file

command1 && command2

Execute command2 if command1 succeeds

command1 || command2

Execute command2 if command1 fails