Comprehensive Guide to Essential Linux Commands

Comprehensive Guide to Essential Linux Commands

Linux offers a powerful set of tools for managing and interacting with your system. Here’s an in-depth look at some of the most essential commands every Linux user should know.

Basic Navigation and File Management

cd — Change Directory

The cd command is used to change the current directory.

cd /path/to/directory        

ls — List Directory Contents

The ls command lists the contents of a directory.

ls
ls -l # Detailed listing
ls -a # Show hidden files        

pwd — Print Working Directory

Displays the current directory.

pwd        

cat — Concatenate and Display Files

The cat command reads and displays the contents of files.

cat file.txt        

touch — Create an Empty File

Creates an empty file or updates the timestamp of an existing file.

touch newfile.txt        

cp — Copy Files and Directories

Copies files or directories.

cp source.txt destination.txt        

mv — Move or Rename Files and Directories

Moves or renames files or directories.

mv oldname.txt newname.txt
mv file.txt /new/directory/        

rm — Remove Files and Directories

Deletes files or directories.

rm file.txt
rm -r directory/ # Recursively delete a directory        

mkdir — Create a New Directory

Creates a new directory.

mkdir — Create a New Directory
Creates a new directory.        

rmdir — Remove an Empty Directory

Removes an empty directory.

rmdir emptydirectory        

cut — Cut Out Sections of a File

Cuts sections from each line of files and writes the result to standard output.

cut -d ' ' -f 1 file.txt # Cut by delimiter and field        

File Compression and Archiving

gzip — Compress Files

Compresses files using gzip.

gzip file.txt        

gunzip — Decompress Files

Decompresses files compressed with gzip.

gunzip file.txt.gz        

tar — Archive Files

Creates or extracts archives.

tar -czvf archive.tar.gz directory/ # Create archive
tar -xzvf archive.tar.gz # Extract archive        

Searching and Filtering

find — Find Files and Directories

Searches for files and directories matching a pattern.

find /path -name "filename"        

grep — Search for a Pattern in a File

Searches for a specified pattern in files.

grep "pattern" file.txt
grep -i "pattern" file.txt # Case-insensitive search        

awk — Pattern Scanning and Processing Language

Processes and analyzes text files.

awk '{print $1}' file.txt # Print first column        

sed — Stream Editor for Filtering and Transforming Text

Performs basic text transformations on an input stream.

sed 's/old/new/g' file.txt # Replace text        

Viewing and Monitoring

head — Display the First Few Lines of a File

Displays the first few lines of a file.

head file.txt        

tail — Display the Last Few Lines of a File

Displays the last few lines of a file.

tail file.txt        

less — View File Contents

Allows scrolling through the contents of a file.

less file.txt        

more — View File Contents One Page at a Time

Displays file contents one page at a time.

more file.txt        

watch — Execute a Command Periodically

Executes a command repeatedly, displaying the output.

watch df -h        

File Permissions and Ownership

chmod — Change Permissions of Files and Directories

Changes file or directory permissions.

chmod 755 script.sh # Read, write, execute for owner; read, execute for group and others        

chown — Change the Owner of a File or Directory

Changes the ownership of files or directories.

chown user:group file.txt        

chgrp — Change the Group Ownership of a File or Directory

Changes the group ownership of files or directories.

chgrp group file.txt        

Process Management

ps — List Running Processes

Displays information about active processes.

ps aux        

top — Display System Resource Usage

Shows real-time system resource usage and process information.

top        

kill — Terminate a Process

Sends a signal to terminate a process.

kill PID
kill -9 PID # Force kill        

Disk Usage and Filesystem Management

du — Display Disk Usage

Shows disk usage of files and directories.

du -h # Human-readable format        

df — Display Free Disk Space

Displays free disk space on the filesystem.

df -h # Human-readable format        

mount — Mount a Filesystem

Mounts a filesystem.

mount /dev/sdX /mnt        

umount — Unmount a Filesystem

Unmounts a filesystem.

umount /mnt        

Networking

ping — Test Connectivity to a Network Host

Tests network connectivity.

ping google.com        

ssh — Secure Shell Remote Login

Logs into a remote machine.

ssh user@remotehost        

scp — Secure Copy Files Between Hosts

Copies files between hosts over SSH.

scp file.txt user@remotehost:/path/to/destination        

rsync — Remote File and Directory Synchronization

Synchronizes files and directories between two locations.

rsync -avz source/ user@remotehost:/path/to/destination        

curl — Transfer Data from or to a Server

Transfers data from or to a server using various protocols.

curl https://example.com        

wget — Retrieve Files from the Web

Downloads files from the web.

wget https://example.com/file.txt        

ftp — File Transfer Protocol Client

Transfers files using FTP.

ftp ftp.example.com        

sftp — Secure File Transfer Protocol Client

Securely transfers files using FTP over SSH.

sftp user@remotehost        

telnet — Telnet Client

Connects to a remote host using the Telnet protocol.

telnet remotehost        

nslookup — DNS Lookup Utility

Queries DNS to obtain domain name or IP address mapping.

nslookup example.com        

dig — DNS Lookup Utility

Performs DNS queries.

dig example.com        

netstat — Display Network Connections and Statistics

Displays network connections, routing tables, and other network interface statistics.

netstat -a        

ifconfig — Configure Network Interfaces

Configures network interfaces.

ifconfig eth0 up        

route — Display or Modify the Routing Table

Displays or modifies the IP routing table.

route -n        

iptables — Firewall and Packet Filtering Utility

Configures the Linux kernel firewall.

iptables -L        

System Information and Management

hostname — Display or Set the Hostname

Displays or sets the system's hostname.

hostname
hostname newhostname        

date — Display or Set the System Date and Time

Displays or sets the system date and time.

date
date -s "2024-06-28 10:00:00"        

timedatectl — Control the System Date and Time

Manages system time and date settings.

timedatectl
timedatectl set-time "2024-06-28 10:00:00"        

uname — Display System Information

Displays system information.

uname -a
uname -r # Kernel release
uname -m # Machine hardware name        

whoami — Display the Current User ID

Displays the username of the current user.

whoami        

id — Display User and Group Information

Displays user and group information.

id        

su — Switch User

Switches to another user.

su - username        

sudo — Execute a Command with Superuser Privileges

Executes a command as another user, typically root.

sudo command        

passwd — Change the Password of a User Account

Changes a user account's password.

passwd        

useradd — Create a New User Account

Creates a new user account.

sudo useradd username        

userdel — Delete a User Account

Deletes a user account.

sudo userdel username        

usermod — Modify a User Account

Modifies a user account.

sudo usermod -aG group username        

groupadd — Create a New Group

Creates a new group.

sudo groupadd groupname        

groupdel — Delete a Group

Deletes a group.

sudo groupdel groupname        

groupmod — Modify a Group

Modifies a group.

sudo groupmod -n newgroupname oldgroupname        

crontab — Schedule Commands to Run Periodically

Schedules commands to run at specified times and intervals.

crontab -e        

systemctl — Control the System and Service Manager

Manages systemd services.

systemctl start service
systemctl stop service
systemctl restart service
systemctl status service        

journalctl — Query the Systemd Journal

Queries and displays logs from the systemd journal.

journalctl -xe        

Conclusion

This guide provides a comprehensive overview of essential Linux commands that cover a wide range of functionalities. Mastering these commands will significantly enhance your ability to manage and interact with Linux systems efficiently.

Manik Hippalgaonkar

Network Engineer | Azure Cloud Support | Routing and switching | Cisco FTD | Firewall | Load balancer | CCNP| |TCS Alumni||Infrastructure Engineer|AWS|insident management| RCA

8 个月

Interesting!

要查看或添加评论,请登录

Kundan Antyakula??的更多文章

社区洞察