A Comprehensive Guide to Essential Linux Commands
A Comprehensive Guide to Essential Linux Commands
Davoud Azari
Contents
Linux Commands: Explanation and Practical Examples ........................................................................ 1
1. File and Directory Management .................................................................................................... 3
2. File Viewing and Editing ................................................................................................................ 5
3. Process Management........................................................................................................................ 6
4. Disk Management ............................................................................................................................. 8
5. Networking........................................................................................................................................ 9
6. User and Group Management ........................................................................................................ 11
7. System Information and Monitoring .............................................................................................. 12
8. Archiving and Compression ......................................................................................................... 13
9. Package Management (Depends on Distribution) .......................................................................... 14
10. System Services and Daemon Management ............................................................................. 16
11. Scheduling Tasks ......................................................................................................................... 17
12. File Permissions and Security .................................................................................................... 17
13. System Backup and Restore ....................................................................................................... 18
14. System Diagnostics and Troubleshooting ................................................................................. 19
15. Networking & Remote Management ......................................................................................... 21
16. Text Processing Utilities ............................................................................................................. 23
17. System Shutdown and Reboot ................................................................................................... 25
18. File System Mounting and Management .................................................................................. 25
19. Filesystem Permissions and Security......................................................................................... 26
1. File and Directory Management
ls
ls -l # detailed list
ls -a # includes hidden files
ls -h # human-readable file sizes
ls -R # recursive listing
ls -S # sort by file size
ls -t # sort by modification time
cd – Change directory:
cd /path/to/directory
cd .. # move up one directory
cd ~ # go to home directory
cd - # go to previous directory
pwd
cp file1.txt /path/to/destination
cp -r folder1 /path/to/destination # directory recursively
cp -i file1.txt /path/to/destination # prompt before overwriting
cp -u file1.txt /path/to/destination # only if source is newer
cp -v file1.txt /path/to/destination # verbose output
rm file1.txt
rm -r directory_name # remove directory recursively
rm -f file1.txt # force remove, skip confirmation
rm -i file1.txt # prompt before removing
rm -v file1.txt # verbose output
mkdir new_directory
mkdir -p /path/to/directory # create parent directories if not exist
mkdir -v new_directory # verbose output
rmdir – Remove empty directories:
rmdir directory_name
rmdir -v directory_name # verbose output
locate filename
locate -i filename # case insensitive search
tree
tree -L 2 # limit depth to 2 levels
tree -d # list directories only
tree -f # show full path of files
stat file1.txt
cat file.txt
cat -n file.txt # number the lines
cat -b file.txt # number non-blank lines only
cat -A file.txt # show all control characters
tac file.txt
more file.txt
more +10 file.txt # start viewing from line 10
less file.txt
less +G file.txt # go to end of file
less +/pattern file.txt # search for pattern
head file.txt
head -n 20 file.txt # first 20 lines
head -c 50 file.txt # first 50 bytes
tail file.txt
tail -f file.txt # follow file as it's being updated
tail -n 20 file.txt # last 20 lines
tail -c 100 file.txt # last 100 bytes
nano file.txt
nano -w file.txt # disable line wrapping
nano -v file.txt # start in view-only mode
vim file.txt
vi file.txt
vim -y file.txt # read-only mode
vim -R file.txt # open in read-only mode
emacs file.txt
emacs -nw file.txt # start in terminal mode (no GUI)
awk '{print $1}' file.txt # print the first column of each line
awk -F, '{print $1}' file.txt # specify a delimiter (comma)
sort file.txt
sort -r file.txt # reverse order
sort -n file.txt # numerical sort
uniq file.txt
uniq -c file.txt # count occurrences of each line
uniq -d file.txt # show only duplicate lines
3. Process Management
ps
ps -e # list all processes
ps -ef # full-format listing
ps -aux # user-oriented format
ps -u user # processes by specific user
top
top -u user # show processes for a specific user
top -d 5 # update every 5 seconds
top -p pid # monitor specific process ID
htop
htop -u user # show processes of a specific user
htop -d 5 # set refresh delay
jobs
jobs -l # list jobs with PID
uptime
time command
4. Disk Management
df
df -h # human-readable format
df -T # show file system type
df /path/to/directory # disk space of a specific directory
lsblk
lsblk -f # show filesystem type
lsblk -o NAME,FSTYPE,SIZE,MOUNTPOINT # custom output
umount /mnt
umount /dev/sda1
blkid
blkid /dev/sda1 # get attributes for specific device
5. Networking
ifconfig
ifconfig eth0 up # bring interface eth0 up
ifconfig eth0 down # bring interface eth0 down
ifconfig eth0 192.168.1.10 netmask 255.255.255.0 # set IP and netmask
ifconfig -a # show all interfaces, even inactive ones
whoami
who
w – Show who is logged in and what they’re doing:
uptime
General Commands
crontab -e
crontab -l
crontab -r
at 09:00
# Enter command to execute at 09:00
batch
# Enter commands to run when system load is low
sleep 5s
getfacl file.txt
dd if=/dev/sda of=/path/to/backup.img
dd if=/path/to/backup.img of=/dev/sda
dmesg – Print the kernel ring buffer messages (system boot and hardware-related
messages):
dmesg
dmesg | grep error # filter for error messages
dmesg -T # show human-readable timestamps
journalctl
journalctl -xe # show detailed error messages
journalctl -u service_name # show logs for a specific service
journalctl -f # follow logs in real-time
nc (Netcat) – Network utility for reading and writing from network connections:
o nc -l -p 1234 – Listen on port 1234:
nc -l -p 1234
nc example.com 1234
iptables – Administration tool for IPv4 packet filtering and NAT (Network Address
Translation):
firewalld – Frontend for managing firewall rules (used in some distros like Fedora and
CentOS):
curl – Transfer data from or to a server using various protocols (HTTP, FTP, etc.):
wget – Download files from the web via HTTP, HTTPS, FTP:
scp – Secure copy over SSH (used to copy files between systems):
o scp file.txt user@remote:/path/to/destination/ – Copy file to remote server:
o awk -F':' '{print $1}' file.txt – Set a custom delimiter (e.g., colon):
o cut -c 1-5 file.txt – Cut specific character positions from each line:
sort file.txt
sort -r file.txt
uniq -c file.txt
tee – Read from standard input and write to standard output and files:
o echo "text" | tee file.txt – Write to file and show output on screen:
wc -w file.txt
sudo reboot
sudo halt
sudo poweroff
sudo init 0
o init 6 – Reboot:
sudo init 6
o mount -t ext4 /dev/sda1 /mnt – Mount a partition with a specific file system
type:
umount /mnt
fstab – File system table (configuration file for mounting file systems):
o /etc/fstab – View and configure persistent mount points:
cat /etc/fstab
blkid
blkid /dev/sda1 # get details about a specific device
sudo fsck -A
umask 022
getfacl file.txt