Linux Shell Commands: Comprehensive Guide for Beginners and Professionals

This article provides a detailed and technical overview of essential Linux shell commands, categorized for efficient system usage. Commands are presented with syntax and practical examples for immediate application.


1. File and Directory Management

1.1 Listing Files and Directories

  • Command: ls
  • Description: Lists files and directories in the current working directory.

 ls -l 

Options:

  • -a: Show hidden files.
  • -l: Long listing format with file permissions and details.
  • -h: Display sizes in human-readable format.

Example:

 ls -alh /home/user 


1.2 Print Current Directory

  • Command: pwd
  • Description: Prints the absolute path of the current working directory.

 pwd 


1.3 Change Directory

  • Command: cd
  • Description: Navigates between directories.

Syntax:

 cd /path/to/directory 

Special Use Cases:

  • Go to home directory:
     cd ~ 
  • Move up one directory level:
     cd .. 

1.4 Create a Directory

  • Command: mkdir
  • Description: Creates a new directory.

 mkdir new_directory 

Options:

  • -p: Create parent directories if they do not exist.

Example:

 mkdir -p /home/user/projects/linux 


1.5 Remove Empty Directory

  • Command: rmdir
  • Description: Removes an empty directory.

 rmdir empty_directory 


1.6 Move or Rename Files

  • Command: mv
  • Description: Moves files or directories, or renames them.

Syntax:

 mv source_file target_location 

Example:

  • Rename a file:
     mv old_name.txt new_name.txt 
  • Move a file:
     mv /home/user/file.txt /home/user/documents/ 

1.7 Copy Files or Directories

  • Command: cp
  • Description: Copies files and directories.

Syntax:

 cp source_file destination 

Options:

  • -r: Recursive copy for directories.

Example:

 cp -r /home/user/source_folder /home/user/backup_folder 


1.8 Delete Files and Directories

  • Command: rm
  • Description: Deletes files and directories.

Syntax:

 rm file_name 

Options:

  • -r: Recursive delete for directories.
  • -i: Prompt before every removal.

Example:

 rm -ri /home/user/temp_folder 


1.9 Create Empty Files

  • Command: touch
  • Description: Creates an empty file or updates the timestamp of an existing file.

 touch newfile.txt 


1.10 Create Symbolic Links

  • Command: ln
  • Description: Creates hard or symbolic links.

Syntax:

 ln -s target_file link_name 

Example:

 ln -s /home/user/original_file.txt /home/user/link_to_file.txt 


1.11 Clear Terminal Screen

  • Command: clear
  • Description: Clears the terminal screen.

 clear 


2. Viewing and Editing Files

2.1 Display File Contents

  • Command: cat
  • Description: Outputs the contents of a file to the terminal.

Syntax:

 cat file.txt 


2.2 Paginated View of Files

  • Command: less
  • Description: Allows paginated viewing of file contents.

Syntax:

 less largefile.txt 

Navigation within less:

  • Move forward: Press Space.
  • Move backward: Press b.
  • Exit: Press q.

2.3 Display File Head

  • Command: head
  • Description: Outputs the first few lines of a file.

Syntax:

 head -n 10 file.txt 


2.4 Display File Tail

  • Command: tail
  • Description: Outputs the last few lines of a file.

Syntax:

 tail -n 10 file.txt 


2.5 Search for Patterns

  • Command: grep
  • Description: Searches for a string pattern in a file.

Syntax:

 grep "pattern" file.txt 

Options:

  • -i: Case-insensitive search.
  • -r: Recursive search in directories.

Example:

 grep -ri "error" /var/log 


2.6 Compare Files

  • Command: diff
  • Description: Compares two files and outputs their differences.

Syntax:

 diff file1.txt file2.txt 

2.7 Find Identical Files

  • Command: cmp
  • Description: Compares two files byte by byte to determine if they are identical.

 cmp file1.txt file2.txt 


2.8 Find Lines Common to Two Files

  • Command: comm
  • Description: Displays lines common to and different in two sorted files.

 comm file1.txt file2.txt 


2.9 Sort File Content

  • Command: sort
  • Description: Sorts the content of a file line by line.

 sort file.txt 

Options:

  • -r: Sort in reverse order.
  • -n: Perform numeric sort.

Example:

 sort -rn numbers.txt 


3. System Information Commands

3.1 Display System Information

  • Command: uname
  • Description: Outputs basic system information.

 uname -a 


3.2 Display Current User

  • Command: whoami
  • Description: Displays the current logged-in username.

 whoami 


3.3 Display Logged-in Users

  • Command: who
  • Description: Shows a list of currently logged-in users.

 who 


3.4 Show System Uptime

  • Command: uptime
  • Description: Displays system uptime and load averages.

 uptime 


3.5 Print Boot and Kernel Messages

  • Command: dmesg
  • Description: Displays system boot and kernel-related messages.

 dmesg | less 


4. Process Management Commands

4.1 List Running Processes

  • Command: ps
  • Description: Displays information about currently running processes.

 ps aux 

Options:

  • a: Show processes from all users.
  • u: Display detailed process information.
  • x: Show processes without a controlling terminal.

4.2 Monitor Processes in Real-time

  • Command: top
  • Description: Displays live system processes and their resource usage.

 top 

Navigation in top:

  • q: Quit the interface.
  • k: Kill a process by entering its PID.
  • h: View help.

4.3 Kill Processes

  • Command: kill
  • Description: Sends signals to processes to terminate or control them.

Syntax:

 kill PID 

Example:

 kill -9 12345 

Options:

  • -9: Forcefully terminate a process.

4.4 Manage Background Jobs

  • Command: jobs
  • Description: Lists background jobs.

 jobs 

Bring a job to the foreground:

 fg %1 

Send a job to the background:

 bg %1 


4.5 Kill All Processes by Name

  • Command: killall
  • Description: Terminates all processes matching a specific name.

 killall process_name 


5. Disk and File System Management Commands

5.1 Display Disk Usage

  • Command: df
  • Description: Shows file system disk space usage.

 df -h 

Options:

  • -h: Display sizes in human-readable format.
  • -T: Show file system types.

5.2 Check Directory Size

  • Command: du
  • Description: Estimates file and directory sizes.

 du -sh /path/to/directory 

Options:

  • -s: Summarize.
  • -h: Human-readable sizes.

5.3 Mount File Systems

  • Command: mount
  • Description: Mounts a file system.

Syntax:

 sudo mount /dev/sdX /mnt 

Unmounting:

 sudo umount /mnt 


5.4 Create File Systems

  • Command: mkfs
  • Description: Creates a new file system on a device.

Syntax:

 sudo mkfs.ext4 /dev/sdX 


5.5 Check and Repair File Systems

  • Command: fsck
  • Description: Checks and repairs file systems.

Syntax:

 sudo fsck /dev/sdX 


6. Networking Commands

6.1 Test Network Connectivity

  • Command: ping
  • Description: Sends ICMP packets to test the connectivity to a host.

Syntax:

 ping -c 4 8.8.8.8 

Options:

  • -c: Specify the number of packets to send.

6.2 Display Network Configuration

  • Command: ifconfig
  • Description: Displays or configures network interfaces.

Syntax:

 ifconfig 


6.3 View IP Addresses

  • Command: ip addr
  • Description: Displays detailed IP address information for all interfaces.

 ip addr 


6.4 Download Files from the Internet

  • Command: wget
  • Description: Downloads files directly from URLs.

Syntax:

wget </span><span><a disabled="true">http://example.com/file</a></span><span>

  • Command: curl
  • Description: Transfers data from or to a server using various protocols.

Syntax:

curl -O </span><span><a disabled="true">http://example.com/file</a></span><span>

Options:

  • -O: Save the file with its original name.

6.5 Trace Network Hops

  • Command: traceroute
  • Description: Traces the path packets take to reach a destination.

Syntax:

 traceroute example.com 


6.6 Secure Remote Access

  • Command: ssh
  • Description: Connects to a remote machine securely using the SSH protocol.

Syntax:

 ssh user@remote_host 

Options:

  • -i: Specify a private key for authentication.

Example:

 ssh -i ~/.ssh/id_rsa user@192.168.1.10 


6.7 Secure File Transfer

  • Command: scp
  • Description: Securely transfers files between machines using SSH.

Syntax:

 scp source_file user@remote_host:/destination/path 

Example:

 scp file.txt user@192.168.1.10:/home/user/documents/ 


6.8 Manage Firewall Rules

  • Command: ufw
  • Description: Simplified interface to manage firewall rules.

Enable the firewall:

 sudo ufw enable 

Allow specific traffic:

 sudo ufw allow 22/tcp 


  • Command: iptables
  • Description: Configures packet filtering rules for the Linux kernel.

Syntax:

 sudo iptables -A INPUT -p tcp --dport 22 -j ACCEPT 


7. User Management Commands

7.1 Add a New User

  • Command: adduser
  • Description: Creates a new user with a home directory and prompts for a password.

Syntax:

 sudo adduser username 


7.2 Modify Existing User Information

  • Command: usermod
  • Description: Modifies user account properties.

Syntax:

 sudo usermod -aG groupname username 

Example:

 sudo usermod -aG sudo john 


7.3 Change User Password

  • Command: passwd
  • Description: Updates a user’s password.

Syntax:

 passwd username 


7.4 Switch Users

  • Command: su
  • Description: Switches to another user account.

Syntax:

 su - username 


7.5 View Logged-in Users

  • Command: who
  • Description: Lists all users currently logged in.

 who 


8. File Permissions and Ownership

8.1 View File Permissions

  • Command: ls -l
  • Description: Displays detailed information about files, including permissions.

Syntax:

 ls -l 

Example Output:

 -rw-r--r-- 1 user group 1234 Jan 01 12:34 file.txt 


8.2 Change File Permissions

  • Command: chmod
  • Description: Modifies file or directory permissions.

Syntax:

 chmod [permissions] file 

Examples:

  • Grant read, write, and execute to the owner:
     chmod 700 file.txt 
  • Grant read and execute to everyone:
     chmod 755 file.txt 

8.3 Change Ownership of Files

  • Command: chown
  • Description: Changes the ownership of files or directories.

Syntax:

sudo chown user</span><span>:group</span><span> file

Examples:

  • Change the owner to john:
     sudo chown john file.txt 
  • Change the owner and group:
sudo chown john</span><span>:developers</span><span> file.txt

8.4 Change Group Ownership

  • Command: chgrp
  • Description: Changes the group ownership of a file or directory.

Syntax:

 sudo chgrp groupname file 

Example:

 sudo chgrp developers file.txt 


9. Package Management Commands

9.1 Update Package Lists

  • Command: apt update
  • Description: Updates the package database on Debian-based systems.

Syntax:

 sudo apt update 


9.2 Upgrade Installed Packages

  • Command: apt upgrade
  • Description: Installs the latest versions of all installed packages.

Syntax:

 sudo apt upgrade 


9.3 Install a Package

  • Command: apt install
  • Description: Installs a specified package.

Syntax:

 sudo apt install package_name 

Example:

 sudo apt install vim 


9.4 Remove a Package

  • Command: apt remove
  • Description: Uninstalls a specified package.

Syntax:

 sudo apt remove package_name 

Example:

 sudo apt remove vim 


9.5 Search for a Package

  • Command: apt search
  • Description: Searches for packages in the package database.

Syntax:

 apt search package_name 

Example:

 apt search python 


9.6 Work with RPM-based Systems

For Red Hat-based systems, use:

  • Command: yum install
     sudo yum install package_name 
  • Command: dnf install (preferred for newer systems):
     sudo dnf install package_name 
  • Command: rpm -ivh
     sudo rpm -ivh package_name.rpm 

10. Disk and Storage Management Commands

10.1 Check Disk Usage

  • Command: df
  • Description: Displays disk space usage for file systems.

Syntax:

 df -h 

Options:

  • -h: Human-readable format.
  • -T: Show file system types.

10.2 Check Directory or File Sizes

  • Command: du
  • Description: Displays the size of directories and files.

Syntax:

 du -sh /path/to/directory 

Options:

  • -s: Summarize total size.
  • -h: Display sizes in human-readable format.

10.3 Format a Partition

  • Command: mkfs
  • Description: Creates a file system on a partition.

Syntax:

 sudo mkfs.ext4 /dev/sdX 


10.4 Mount and Unmount File Systems

  • Command: mount
  • Description: Mounts a file system to a directory.

Syntax:

 sudo mount /dev/sdX /mnt 

  • Command: umount
  • Description: Unmounts a file system from a directory.

Syntax:

 sudo umount /mnt 


10.5 Check and Repair File Systems

  • Command: fsck
  • Description: Checks and repairs file systems.

Syntax:

 sudo fsck /dev/sdX 


11. Archiving and Compression Commands

11.1 Create a Tar Archive

  • Command: tar
  • Description: Archives files into a tarball.

Syntax:

 tar -cvf archive.tar /path/to/files 

Options:

  • -c: Create an archive.
  • -v: Verbose output.
  • -f: Specify file name.

11.2 Extract a Tar Archive

  • Command: tar
  • Description: Extracts files from a tarball.

Syntax:

 tar -xvf archive.tar 


11.3 Compress Files with gzip

  • Command: gzip
  • Description: Compresses files using gzip.

Syntax:

 gzip file.txt 

  • Command: gunzip
  • Description: Decompresses gzip-compressed files.

Syntax:

 gunzip file.txt.gz 


11.4 Zip and Unzip Files

  • Command: zip
  • Description: Compresses files into a .zip archive.

Syntax:

 zip archive.zip file1 file2 

  • Command: unzip
  • Description: Extracts files from a .zip archive.

Syntax:

 unzip archive.zip 


12. Log Management Commands

12.1 View System Logs

  • Command: journalctl
  • Description: Displays logs from the systemd journal.

Syntax:

 journalctl 

Options:

  • -b: Show logs from the current boot.
  • -u service_name: Show logs for a specific service.

12.2 Monitor Logs in Real-Time

  • Command: tail
  • Description: Displays the last few lines of a file and updates in real-time.

Syntax:

 tail -f /var/log/syslog 


12.3 Rotate Log Files

  • Command: logrotate
  • Description: Manages the automatic rotation and compression of log files.

Configuration File:

 /etc/logrotate.conf 



Comments

Leave a Reply

Your email address will not be published. Required fields are marked *