Get 15% discount on 3 or more courses

Common Linux Commands

🐧 Linux Commands Explained


📁 File & Directory Management

ls – List files and directories

Explanation: Displays files and folders in the current directory.

ls
ls -l
ls -la
  • -l → Displays detailed listing (permissions, owner, size, date)
  • -a → Shows hidden files (files starting with .)

pwd – Print working directory

Explanation: Shows the full path of the current directory.

pwd

cd – Change directory

Explanation: Used to navigate between directories.

cd /etc
cd ..
cd ~
  • /etc → Absolute path
  • .. → Move one directory up
  • ~ → Home directory of the current user

mkdir – Create directories

Explanation: Creates new directories.

mkdir test
mkdir -p project/logs
  • -p → Creates parent directories if they do not exist

rm – Remove files or directories

Explanation: Deletes files and directories.

rm file.txt
rm -r folder
rm -rf folder
  • -r → Removes directories recursively (within a directory)
  • -f → Forces deletion without confirmation
⚠️ Warning: rm -rf permanently deletes data and should be used with caution.

cp – Copy files or directories

Explanation: Copies files or directories from one location to another.

cp file1 file2
cp -r dir1 dir2
  • -r → Copies directories recursively (within a directory)

mv – Move or rename files

Explanation: Moves files or renames them.

mv file.txt /tmp/
mv old.txt new.txt

📄 File Viewing & Editing

cat – View file content

Explanation: Displays the contents of a file.

cat file.txt

less – View large files

Explanation: Allows scrolling through files page by page.

less /var/log/syslog

head – View first lines of a file

Explanation: Displays the first few lines of a file.

head file.txt
head -n 5 file.txt
  • -n → Specifies number of lines to display

tail – View last lines of a file

Explanation: Displays the last few lines of a file.

tail file.txt
tail -f /var/log/messages
  • -f → Continuously monitors file updates (used for logs)

nano / vi – Edit files

Explanation: Command-line text editors.

nano file.txt
vi file.txt

🔍 Searching & Filtering

find – Search files and directories

Explanation: Searches for files based on conditions.

find / -name file.txt
  • / → Starting directory
  • -name → Search by file name

grep – Search text inside files

Explanation: Searches for patterns inside files.

grep "error" logfile.txt
grep -i "root" /etc/passwd
  • -i → Case-insensitive search

👤 User & Permission Management

whoami – Display current user

whoami

id – User identity details

id

chmod – Change file permissions

Explanation: Modifies file permissions.

chmod 755 script.sh
chmod u+x script.sh
  • 755 → Owner: read/write/execute, Others: read/execute
  • u+x → Adds execute permission to the owner

chown – Change file ownership

chown user:user file.txt

⚙️ Process & System Monitoring

ps – View running processes

ps aux
  • a → Show processes from all users
  • u → Display user-oriented format
  • x → Show processes without terminal

top / htop – Real-time monitoring

top
htop

kill – Terminate a process

kill 1234
kill -9 1234
  • -9 → Forcefully kill a process

💾 Disk & Memory

df – Disk usage

df -h
  • -h → Human-readable format

du – Directory size

du -sh /var/log
  • -s → Summary only
  • -h → Human-readable format

free – Memory usage

free -h
  • -h → Human-readable output

🌐 Networking

ip a – Show IP address

ip a

ping – Test network connectivity

ping google.com

ss – View open ports

ss -tulnp
  • -t → TCP ports
  • -u → UDP ports
  • -l → Listening ports
  • -n → Numeric output
  • -p → Show process name

curl – Test URLs and APIs

curl https://example.com

📦 Package Management

Ubuntu / Debian

apt update
apt install nginx
apt remove nginx

RHEL / CentOS / Rocky Linux

yum install httpd
dnf install httpd

🔐 System Control

sudo – Run commands as root

sudo apt update

Reboot & Shutdown

reboot
shutdown -h now
  • -h → Halt the system
  • now → Execute immediately