Essential Linux and Grep Commands
Grep Commands
grep "hello" file.txt — Search a word in a file
grep -i "hello" file.txt — Case-insensitive search
grep -r "hello" /path/to/dir — Recursive search in a directory
grep -n "hello" file.txt — Show line numbers with matches
grep -w "hello" file.txt — Search exact word match
grep -v "hello" file.txt — Invert match (lines not containing the pattern)
grep -c "hello" file.txt — Count number of matches
grep -o "hello" file.txt — Show only the matched part
grep "^hello" file.txt — Lines starting with "hello" (regex)
grep "hello$" file.txt — Lines ending with "hello" (regex)
grep -E "cat|dog" file.txt — Search multiple patterns using extended regex
Linux Commands
File and Directory Management
ls — List files and directories
cd — Change directory
pwd — Print working directory
mkdir — Create new directory
rm — Remove files or directories
cp — Copy files or directories
mv — Move or rename files or directories
touch — Create empty file
find — Search files and directories
File Viewing and Editing
cat — View file contents
less / more — View file page by page
head — Show first lines of a file
tail — Show last lines of a file
nano / vim / vi — Text editors
Permissions and Ownership
chmod — Change file permissions
chown — Change file owner
ls -l — View detailed file permissions
Process and System Management
ps — Show running processes
top / htop — Real-time system monitor
kill PID — Terminate process by ID
df -h — Show disk usage
du -sh — Show folder size
free -h — Show memory usage
uptime — Show how long the system has been running
uname -a — System information
whoami — Show current user
id — Show user and group info
Networking
ping — Check network connection
ifconfig / ip a — Show network interfaces
netstat -tuln — Show open ports
curl — Fetch web content
wget — Download files from the web
ssh user@host — Connect to remote server via SSH
Package Management
sudo apt update && sudo apt upgrade — Update/upgrade (Debian/Ubuntu)
sudo apt install packagename — Install package (Debian/Ubuntu)
sudo yum install packagename — Install package (RHEL/CentOS)
sudo dnf install packagename — Install package (Fedora/RHEL 8+)
Others
history — Show command history
alias — Create command shortcuts
man command — Show manual for a command
tar -czvf file.tar.gz folder/ — Archive/compress files
scp file user@host:/path — Copy files between systems securely
Disk and Filesystem
mount — Mount a filesystem
umount — Unmount a filesystem
lsblk — List information about block devices
blkid — Locate/print block device attributes
fsck — Check and repair a filesystem
mkfs — Create a new filesystem
User Management
adduser username — Add a new user
deluser username — Delete a user
passwd username — Change user password
usermod -aG group username — Add user to a group
groups username — Show groups for a user
Environment and Shell
echo $VARIABLE — Display environment variable value
export VAR=value — Set environment variable
set — Set shell options and positional parameters
env — Show environment variables
source filename — Execute commands from a file in the current shell
Compression and Archiving
gzip file — Compress a file using gzip
gunzip file.gz — Decompress a .gz file
zip file.zip files — Compress files into a zip archive
unzip file.zip — Extract zip archive
tar -xvzf file.tar.gz — Extract .tar.gz file
Scheduling and Automation
crontab -e — Edit crontab for scheduling tasks
crontab -l — List user cron jobs
at time — Schedule a one-time task
systemctl enable service — Enable a service at startup
systemctl disable service — Disable a service at startup
Log Files and Monitoring
journalctl — Query system logs (systemd)
dmesg — Print kernel ring buffer
tail -f /var/log/syslog — Monitor system log file in real time
logrotate — Rotate and compress log files