0% found this document useful (0 votes)
5 views36 pages

Linux Essentials

The document provides a comprehensive guide to essential Linux commands, categorized into user and group management, file and directory management, file content management, file permissions, disk management, process management, network management, system monitoring, text processing, package management, archive and compression, shutdown and reboot, and task scheduling. Each command is accompanied by a brief description and an example of its usage. This serves as a valuable reference for users looking to navigate and manage Linux systems effectively.

Uploaded by

Aditya Amage
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views36 pages

Linux Essentials

The document provides a comprehensive guide to essential Linux commands, categorized into user and group management, file and directory management, file content management, file permissions, disk management, process management, network management, system monitoring, text processing, package management, archive and compression, shutdown and reboot, and task scheduling. Each command is accompanied by a brief description and an example of its usage. This serves as a valuable reference for users looking to navigate and manage Linux systems effectively.

Uploaded by

Aditya Amage
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 36

Linux Essential

Commands

1. User and Group Management

useradd​
Creates a new user account.​
Example: sudo useradd john

usermod​
Modifies a user account.​
Example: sudo usermod -aG sudo john

userdel​
Deletes a user account.​
Example: sudo userdel john

groupadd​
Creates a new user group.​
Example: sudo groupadd developers

groupdel​
Deletes a group.​
Example: sudo groupdel developers
chgrp​
Changes the group ownership of a file.​
Example: sudo chgrp developers report.txt

passwd​
Changes a user's password.​
Example: passwd john

who​
Displays who is currently logged in.​
Example: who
2. File and Directory Management

ls​
Lists directory contents.​
Example: ls -l /home

cd​
Changes the current directory.​
Example: cd /var/log

pwd​
Prints the current directory.​
Example: pwd

mkdir​
Creates a directory.​
Example: mkdir projects

rmdir​
Removes an empty directory.​
Example: rmdir old_folder

rm​
Removes files or directories.​
Example: rm -rf temp/
cp​
Copies files or directories.​
Example: cp file1.txt backup/

mv​
Moves or renames files.​
Example: mv file.txt newname.txt

touch​
Creates an empty file or updates timestamp.​
Example: touch index.html

find​
Searches for files or directories.​
Example: find /home -name "*.log"

locate​
Finds files by name using a database.​
Example: locate nginx.conf
3. File Content Management

cat​
Displays file contents.​
Example: cat notes.txt

more​
Displays file contents page-by-page.​
Example: more largefile.txt

less​
Displays file contents with navigation.​
Example: less largefile.txt

head​
Shows the beginning of a file.​
Example: head -n 10 log.txt

tail​
Shows the end of a file.​
Example: tail -n 10 log.txt

nano​
Opens a file in the Nano text editor.​
Example: nano config.txt
vi​
Opens a file in the Vi editor.​
Example: vi config.txt

ln​
Creates hard or symbolic links.​
Example: ln -s /var/log log_link

file​
Identifies the file type.​
Example: file image.jpg

stat​
Displays file metadata.​
Example: stat notes.txt

updatedb​
Updates the locate database.​
Example: sudo updatedb

xargs​
Builds and executes commands from input.​
Example: find . -name "*.tmp" | xargs rm
4. File Permissions and Ownership

chmod​
Changes file permissions.​
Example: chmod 755 script.sh

chown​
Changes file ownership.​
Example: chown john:admin report.txt

chgrp​
Changes group ownership.​
Example: chgrp developers app.py

umask​
Sets default permissions for new files.​
Example: umask 022

getfacl​
Displays file ACLs.​
Example: getfacl report.txt

setfacl​
Modifies file ACLs.​
Example: setfacl -m u:john:rwx file.txt
ls -l​
Lists files with permissions.​
Example: ls -l /etc/passwd

stat​
Displays detailed file info.​
Example: stat index.html

acl​
General term referring to ACL management using
getfacl and setfacl.
5. Disk Management

df​
Displays available disk space.​
Example: df -h

du​
Estimates file/directory size.​
Example: du -sh /var/log

fdisk​
Manages disk partitions.​
Example: sudo fdisk /dev/sda

parted​
Advanced partition tool.​
Example: sudo parted /dev/sdb

lsblk​
Lists block devices.​
Example: lsblk

blkid​
Displays block device attributes.​
Example: sudo blkid
mount​
Mounts a filesystem.​
Example: sudo mount /dev/sdb1 /mnt

umount​
Unmounts a filesystem.​
Example: sudo umount /mnt

mkfs​
Creates a new filesystem.​
Example: sudo mkfs.ext4 /dev/sdb1

fsck​
Checks and repairs filesystems.​
Example: sudo fsck /dev/sda1

tune2fs​
Adjusts filesystem parameters.​
Example: sudo tune2fs -l /dev/sda1

resize2fs​
Resizes a filesystem.​
Example: sudo resize2fs /dev/sda1

lvcreate​
Creates logical volumes (LVM).​
Example: lvcreate -n lv_data -L 10G vg1
vgcreate​
Creates volume groups.​
Example: vgcreate vg1 /dev/sdb1

pvcreate​
Initializes a physical volume for LVM.​
Example: pvcreate /dev/sdb1

lsattr​
Lists file attributes.​
Example: lsattr file.txt

chattr​
Changes file attributes.​
Example: chattr +i important.txt
6. Process Management

ps​
Shows running processes.​
Example: ps aux

top​
Interactive system process monitor.​
Example: top

htop​
Advanced interactive process viewer.​
Example: htop

kill​
Terminates a process by PID.​
Example: kill 1234

killall​
Kills processes by name.​
Example: killall firefox

bg​
Resumes suspended job in background.​
Example: bg %1
fg​
Brings background job to foreground.​
Example: fg %1

nice​
Starts process with custom priority.​
Example: nice -n 10 ./script.sh

renice​
Changes running process priority.​
Example: renice -n 5 -p 1234

jobs​
Lists current jobs.​
Example: jobs

pgrep​
Finds process IDs by name.​
Example: pgrep nginx

pkill​
Kills processes by name.​
Example: pkill -f backup.sh

pstree​
Shows processes in tree format.​
Example: pstree
lsof​
Lists open files by processes.​
Example: lsof -i :80

strace​
Traces system calls of a process.​
Example: strace ls

timeout​
Runs a command with time limit.​
Example: timeout 5 ping google.com

watch​
Runs a command at intervals.​
Example: watch -n 2 date
7. Network Management

ifconfig​
Displays or configures network interfaces.​
Example: ifconfig eth0

ip a​
Shows network interfaces and IPs.​
Example: ip a

ip r​
Displays routing table.​
Example: ip r

ip link set <interface> up/down​


Enables/disables network interface.​
Example: ip link set eth0 down

ping​
Sends ICMP echo request.​
Example: ping google.com

traceroute​
Shows network path to host.​
Example: traceroute openai.com
netstat -tulnp​
Lists ports and services.​
Example: netstat -tulnp

ss -tulnp​
Displays socket stats.​
Example: ss -tulnp

hostname -I​
Displays IP addresses.​
Example: hostname -I

dig​
Queries DNS servers.​
Example: dig example.com

nslookup​
Performs DNS lookup.​
Example: nslookup example.com

whois​
Displays domain registration info.​
Example: whois example.com

curl -I​
Fetches HTTP headers.​
Example: curl -I https://openai.com
wget​
Downloads files.​
Example: wget https://example.com/file.txt

scp​
Securely copies files.​
Example: scp file.txt user@host:/home/user/

rsync​
Efficiently syncs files/directories.​
Example: rsync -avz /src/ user@host:/dest/

tcpdump​
Captures network packets.​
Example: tcpdump -i eth0

nmap​
Scans hosts for open ports.​
Example: nmap 192.168.1.1
8. System Monitoring

uptime​
Shows system uptime.​
Example: uptime

dmesg​
Displays kernel ring buffer.​
Example: dmesg | less

free -m​
Displays memory usage in MB.​
Example: free -m

vmstat​
Reports system performance.​
Example: vmstat 1 5

iostat​
Shows CPU and I/O usage.​
Example: iostat

mpstat​
Displays CPU usage per processor.​
Example: mpstat -P ALL
top​
Monitors running processes.​
Example: top

htop​
Advanced interactive monitor.​
Example: htop

sar -u 5 10​
Monitors CPU usage at intervals.​
Example: sar -u 5 10

iotop​
Monitors disk I/O usage.​
Example: iotop

lsof​
Lists open files.​
Example: lsof -u john

watch -n 2 <command>​
Repeats command every 2 seconds.​
Example: watch -n 2 df -h

journalctl -xe​
Shows system logs with extra info.​
Example: journalctl -xe
systemctl status​
Checks status of a service.​
Example: systemctl status ssh

service status​
Legacy way to check services.​
Example: service apache2 status

df -h​
Shows disk usage.​
Example: df -h

du -sh​
Shows directory size.​
Example: du -sh /var/log

ps aux --sort=-%mem​
Lists processes sorted by memory.​
Example: ps aux --sort=-%mem
9. Text Processing

cat​
Displays file contents.​
Example: cat file.txt — Prints the contents of
file.txt.

tac​
Displays file contents in reverse order.​
Example: tac file.txt — Reverses the order of lines
in file.txt.

head​
Shows the first part of a file.​
Example: head -n 5 file.txt — Displays the first 5
lines.

tail​
Shows the last part of a file.​
Example: tail -n 5 file.txt — Displays the last 5
lines.

tail -f​
Monitors a file for real-time changes.​
Example: tail -f /var/log/syslog — Follows the
syslog live.
grep​
Searches for patterns in files.​
Example: grep 'error' logfile.txt — Finds lines
with 'error'.

grep -r​
Recursively searches directories.​
Example: grep -r 'TODO' /project — Finds all
TODO comments.

awk​
Pattern scanning and processing language.​
Example: awk '{print $1, $3}' file.txt —
Prints first and third fields.

sed​
Stream editor for filtering text.​
Example: sed 's/foo/bar/g' file.txt —
Replaces 'foo' with 'bar'.

cut​
Cuts sections from each line.​
Example: cut -d':' -f1 /etc/passwd — Displays
only usernames.

sort​
Sorts lines in text files.​
Example: sort names.txt — Alphabetically sorts file.
uniq​
Filters duplicate lines.​
Example: sort names.txt | uniq — Removes
duplicates.

wc -l​
Counts lines in a file.​
Example: wc -l file.txt — Outputs number of lines.

wc -w​
Counts words in a file.​
Example: wc -w file.txt — Outputs word count.

wc -c​
Counts bytes in a file.​
Example: wc -c file.txt — Outputs file size in bytes.

tr​
Translates or deletes characters.​
Example: tr 'a-z' 'A-Z' < file.txt — Converts
lowercase to uppercase.

diff​
Compares two files line by line.​
Example: diff file1.txt file2.txt — Shows
differences.
cmp​
Compares files byte by byte.​
Example: cmp file1.bin file2.bin — Returns first
mismatch.

tee​
Saves output to file and displays it.​
Example: echo 'Log entry' | tee log.txt —
Outputs and writes to file.
10. Package Management
For Debian-based systems:

apt update​
Updates package lists.​
Example: sudo apt update — Refreshes available
packages.

apt upgrade​
Upgrades all installed packages.​
Example: sudo apt upgrade — Installs latest versions.

apt install​
Installs a package.​
Example: sudo apt install curl — Installs curl.

apt remove​
Removes a package.​
Example: sudo apt remove nano — Uninstalls nano.

apt purge​
Removes package and config files.​
Example: sudo apt purge apache2 — Cleans
Apache fully.

dpkg -i​
Installs .deb files manually.​
Example: sudo dpkg -i package.deb — Installs
local package.

dpkg -r​
Removes a package.​
Example: sudo dpkg -r package — Deletes
package.

dpkg -l​
Lists all installed packages.​
Example: dpkg -l — Displays installed package list.

apt-cache search​
Searches for packages.​
Example: apt-cache search nginx — Finds nginx
packages.

apt autoremove​
Cleans up unused dependencies.​
Example: sudo apt autoremove — Frees disk space.

apt clean​
Cleans package cache.​
Example: sudo apt clean — Removes cached .deb
files.
For RHEL-based systems:

yum update / dnf update​


Updates installed packages.​
Example: sudo dnf update — Updates all packages.

yum install / dnf install​


Installs new packages.​
Example: sudo dnf install git — Installs Git.

yum remove / dnf remove​


Removes installed packages.​
Example: sudo yum remove vim — Uninstalls vim.

rpm -ivh​
Installs .rpm package.​
Example: sudo rpm -ivh file.rpm — Installs a local
.rpm file.

rpm -e​
Erases a package.​
Example: sudo rpm -e package — Removes an rpm
package.

yum list installed / dnf list installed​


Lists all packages.​
Example: dnf list installed — Shows installed
software.
yum search / dnf search​
Searches the repository.​
Example: yum search python — Finds Python-related
packages.

yum clean all​


Cleans all cached data.​
Example: sudo yum clean all — Frees up space.
11. Archive and Compression

tar -cvf​
Creates a tar archive.​
Example: tar -cvf backup.tar folder/ —
Archives folder.

tar -xvf​
Extracts a tar archive.​
Example: tar -xvf backup.tar — Extracts contents.

tar -tvf​
Lists contents of a tar archive.​
Example: tar -tvf backup.tar — Views file list.

tar -czvf​
Creates a compressed archive.​
Example: tar -czvf backup.tar.gz folder/ —
Compresses to .tar.gz.

tar -xzvf​
Extracts compressed archive.​
Example: tar -xzvf backup.tar.gz — Unpacks
gzip tarball.

tar -cJvf​
Creates a tar.xz archive.​
Example: tar -cJvf archive.tar.xz folder/ —
Compresses to .xz format.

tar -xJvf​
Extracts tar.xz archive.​
Example: tar -xJvf archive.tar.xz — Unpacks xz
archive.

zip​
Creates a zip archive.​
Example: zip archive.zip file1 file2 —
Compresses files.

unzip​
Extracts files from zip archive.​
Example: unzip archive.zip — Extracts zip contents.

unzip -l​
Lists contents of a zip.​
Example: unzip -l archive.zip — Views inside
archive.

zip -r​
Recursively zips a directory.​
Example: zip -r project.zip project/ — Zips
entire folder.
gzip​
Compresses a file.​
Example: gzip log.txt — Creates log.txt.gz.

gunzip​
Decompresses a gzip file.​
Example: gunzip log.txt.gz — Restores original file.

bzip2​
Compresses with bzip2.​
Example: bzip2 bigfile — Makes bigfile.bz2.

bunzip2​
Decompresses bzip2 file.​
Example: bunzip2 bigfile.bz2 — Restores bigfile.
12. Shutdown and Reboot

shutdown -h now​
Shuts down the system immediately.​
Example: sudo shutdown -h now

shutdown -h +10​
Schedules shutdown after 10 minutes.​
Example: sudo shutdown -h +10

shutdown -h 22:00​
Schedules shutdown at a specific time.​
Example: sudo shutdown -h 22:00

shutdown -c​
Cancels a scheduled shutdown.​
Example: sudo shutdown -c

poweroff​
Powers off the machine.​
Example: sudo poweroff

halt​
Stops all processes and halts.​
Example: sudo halt
reboot​
Reboots the system.​
Example: sudo reboot

shutdown -r now​
Immediate reboot.​
Example: sudo shutdown -r now

shutdown -r +5​
Reboot in 5 minutes.​
Example: sudo shutdown -r +5

systemctl reboot​
Systemd reboot command.​
Example: sudo systemctl reboot
13. Task Scheduling

crontab -e​
Edits cron jobs.​
Example: crontab -e — Adds a scheduled task.

crontab -l​
Lists scheduled tasks.​
Example: crontab -l — Displays your cron jobs.

crontab -r​
Removes all cron jobs.​
Example: crontab -r — Deletes your entire crontab.

crontab -u <user> -l​


Lists cron jobs for a specific user.​
Example: crontab -u john -l

at​
Schedules a one-time task.​
Example: echo "shutdown -h now" | at 10:30
14. System Diagnostics and Troubleshooting

journalctl -xe​
Views detailed logs.​
Example: journalctl -xe — Useful for errors.

journalctl -f​
Follows logs live.​
Example: journalctl -f — Similar to tail -f.

journalctl --since "1 hour ago"​


Shows logs from the past hour.​
Example: journalctl --since "1 hour ago"

dmesg | less​
Views boot and hardware logs.​
Example: dmesg | less

dmesg | grep error​


Filters kernel logs for errors.​
Example: dmesg | grep error

cat /var/log/syslog​
Displays general system log (Debian).​
Example: cat /var/log/syslog
cat /var/log/messages​
Displays system logs (RHEL).​
Example: cat /var/log/messages

uptime​
Shows system uptime and load.​
Example: uptime

free -m​
Displays memory usage in MB.​
Example: free -m

top​
Real-time process and usage monitor.​
Example: top

htop​
Improved real-time monitor.​
Example: htop

vmstat​
System performance metrics.​
Example: vmstat 1 5

iostat​
CPU and disk usage.​
Example: iostat

You might also like