# Linux - Command Line

The Linux command line is a powerful tool for interacting with the operating system, automating tasks, and performing a wide range of system operations. Mastering it can make you a more efficient and capable Linux user or system administrator. In this blog, we’ll dive into the essential commands and concepts to help you master the Linux command line.

### **1\. Basic Navigation Commands**

Let’s start by learning how to navigate the Linux filesystem.

#### `pwd` – Print Working Directory

Displays the current directory you're in.

```plaintext
pwd
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1726688937646/b5b42df5-5f59-4c4b-aae1-b15f23d4de52.png align="center")

#### `ls` – List Files

Lists the contents of a directory.

```plaintext
ls      # List files in the current directory
ls -l   # Detailed list (including permissions and file sizes)
ls -a   # List hidden files
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1726689017903/d8250b42-7c3e-4bc8-8faa-c71f913d8b44.png align="center")

#### `cd` – Change Directory

Moves you to another directory.

```plaintext
cd /path/to/directory  # Change to a specific directory
cd ~                   # Change to home directory
cd ..                  # Move up one directory
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1726689202992/44e0269a-e16c-4635-8325-b7b980f62e88.png align="center")

### **2\. File Management**

Managing files and directories is fundamental to working in the Linux command line.

#### `touch` – Create an Empty File

Creates a new, empty file.

```plaintext
touch filename.txt
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1726689287081/fc886783-4231-46cf-914c-4e149b64fa6a.png align="center")

#### `cp` – Copy Files and Directories

Copies files or directories from one location to another.

```plaintext
cp file1.txt file2.txt           # Copy a file
cp -r /source/directory /target  # Copy a directory recursively
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1726689885257/56bc6e52-2bce-4a36-b19c-97dd687c942b.png align="center")

#### `mv` – Move or Rename Files

Moves or renames files and directories.

```plaintext
mv file.txt /new/path/           # Move file to another directory
mv oldname.txt newname.txt       # Rename a file
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1726690168522/4ba3b6cd-081c-4703-8ea8-e22c686992ca.png align="center")

#### `rm` – Remove Files and Directories

Deletes files or directories.

```plaintext
rm file.txt             # Delete a file
rm -r /path/to/dir      # Delete a directory and its contents recursively
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1726690346495/50a388d5-9896-4c71-8d08-ae07804a4c74.png align="center")

#### `mkdir` – Create Directory

Creates a new directory.

```plaintext
mkdir new_directory             # Create new directory
mkdir -p parent_dir/child_dir   # Create parent and child directories at once
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1726690503821/9c20acdd-3d00-4493-8e35-8dbb629e3f43.png align="center")

### **3\. File Permissions & Ownership**

Understanding and managing file permissions and ownership is key to maintaining security and proper access control on your Linux system.

#### **Viewing Permissions**

* **Using** `ls -l`:
    

```plaintext
ls -l filename
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1726130113304/782beea2-0785-4eac-9014-c72479e22ae3.png?auto=compress,format&format=webp align="left")

This command displays file permissions, ownership, and other details. Permissions are shown as a string like `-rwxr-xr--`, where:

* `r` stands for read
    
* `w` stands for write
    
* `x` stands for execute
    

#### **Changing Permissions**

* **Using** `chmod` **Numeric Mode** :
    
* `7` (rwx): Read, write, and execute
    
* `5` (r-x): Read and execute
    
* `0` (---): No permissions
    

```plaintext
chmod 755 file
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1726130285177/74ffc3c2-e89e-44c6-9649-0ac7f724e444.png?auto=compress,format&format=webp align="left")

**Using** `chmod`**Symbolic Mode:**

* Use the following notations with the `chmod` command:
    
* `u`: User (the file owner)
    
* `g`: Group (the group that owns the file)
    
* `o`: Others (everyone else)
    

```plaintext
chmod u+x file  # Add execute permission for the other user
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1726130480889/8c840c2a-792e-4eb6-9f71-e6180efee0a3.png?auto=compress,format&format=webp align="left")

#### **Changing Ownership**

* **Using** `chown`:
    

```plaintext
chown user:group file
```

The `chown` command changes the owner and group of a file or directory. For example, `chown ubuntu:dev file` changes the owner to `ubuntu` and the group to `dev`.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1726130996905/e1db420f-b53a-4b17-8dc3-fdef4703796d.png?auto=compress,format&format=webp align="left")

* **Changing Group Ownership**:
    

```plaintext
chgrp group file
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1726131093843/0c253ecc-80fa-4c25-b120-b50739a60e15.png?auto=compress,format&format=webp align="left")

* The `chgrp` command changes the group ownership of a file or directory.
    

### **4\. User and Group Management**

Linux allows the creation of multiple users, each with specific roles and permissions.

* `adduser`: Create a new user.
    

```plaintext
sudo adduser username
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1726123954681/f0e52458-b3d1-4eb8-9c57-1209f4d4efc8.png?auto=compress,format&format=webp align="left")

* `passwd`: Set or change a user’s password.
    

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1726124022034/27bb6823-7811-4773-9858-5b3dc2b68bfb.png?auto=compress,format&format=webp align="left")

* `su`: Switch user.
    

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1726124058415/8bf77ce6-f27d-401f-a2fd-b57ad7dd3fa6.png?auto=compress,format&format=webp align="left")

**Managing Groups**:

* `groupadd`: Creates a new group (e.g., `groupadd groupname`).
    

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1726120398659/1f63d897-f8dd-4286-9f34-ee930bfa3765.png?auto=compress,format&format=webp align="left")

* `usermod`: Modifies user properties, including group membership (e.g., `usermod -aG groupname username`).
    

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1726120517398/c928ed19-8246-49e2-a553-7a4c68f8845a.png?auto=compress,format&format=webp align="left")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1726120675902/a5e30525-c1d6-446f-9dbc-10787047c0dc.png?auto=compress,format&format=webp align="left")

**Viewing Users and Groups**:

* `cat /etc/passwd`: Lists all users.
    

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1726120769897/a6a3ffea-36df-44ec-9eef-14e95efc2d05.png?auto=compress,format&format=webp align="left")

* `cat/etc/group`: Lists all groups.
    

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1726120801087/6e830670-94f6-44c9-8597-11086521d373.png?auto=compress,format&format=webp align="left")

### **5\. Process Management**

Managing processes is essential for system stability and performance. Some useful process management commands are:

* `ps`: (process status) command displays information about **active processes**
    

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1726122078219/9303cbf0-5af0-4e01-a7a7-e9537dd2231f.png?auto=compress,format&format=webp align="left")

* `top`: Provides a dynamic, **real-time view** of system processes.
    

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1726122123136/32de4e8a-a611-41f7-8dc9-63da211a36b3.png?auto=compress,format&format=webp align="left")

* `htop`: An improved version of `top` with a **better interface**.
    

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1726125434092/646abc32-94c2-416e-b4ce-f1afcb1c17cc.png?auto=compress,format&format=webp align="left")

`top` and `htop`: Both tools display real-time CPU usage.

`mpstat`: Provides detailed **CPU usage** statistics.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1726141227528/37e9fda5-06ac-42cb-8920-a85d2bb69949.png?auto=compress,format&format=webp align="left")

* `kill`: Terminates a process by its ID (PID).
    

```plaintext
kill -9 PID
```

`df`: Shows disk space usage for mounted file systems in a human-readable format.

```plaintext
df -h
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1726141634825/fcab66d2-da35-424d-8dd6-b92c108d0135.png?auto=compress,format&format=webp align="left")

### **6\. Network Management**

Linux provides powerful tools to interact with networks, making it a go-to platform for system administration.

#### `ifconfig` – Network Interface Configuration

Displays the network interface configuration (useful for checking IP addresses).

```plaintext
ifconfig
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1726691192423/727e1b7c-98f3-48ca-a69d-ac6f50d95c58.png align="center")

#### `ping` – Test Network Connectivity

Sends packets to a network address to test connectivity.

```plaintext
ping google.com
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1726691278858/1fc7f7d9-e657-4e82-a394-b37d4f807ef4.png align="center")

#### `netstat` – Network Statistics

Shows network connections, routing tables, and interface statistics.

```plaintext
netstat -tuln   # Show listening ports
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1726691342017/1b58caad-fcc4-46b2-bd09-62caa7ae6b8d.png align="center")

### **7\. Package Management**

Package management systems like `apt` (Debian/Ubuntu) or `yum` (RHEL/CentOS) allow for easy installation, updating, and removal of software packages.

#### `apt-get` – Manage Packages in Ubuntu/Debian

Installs, updates, or removes software packages.

```plaintext
sudo apt-get update      # Update package list
sudo apt-get upgrade     # Upgrade all installed packages
sudo apt-get install package_name   # Install a package
sudo apt-get remove package_name    # Remove a package
```

#### `yum` – Manage Packages in CentOS/RHEL

Similar to `apt-get` but used in Red Hat-based distributions.

```plaintext
sudo yum install package_name
```

### **8\. Text Processing**

Linux offers several tools for processing text files, making it easy to manipulate, search, and transform data.

#### `cat` – Display File Contents

Displays the content of a file.

```plaintext
cat file.txt
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1726691816056/7bec5293-355f-4c80-9c62-c5dcf2af86e4.png align="center")

#### `grep` – Search Text

Searches for patterns in a file or output.

```plaintext
grep 'pattern' file.txt    # Search for a pattern in a file
grep -r 'pattern' /dir     # Search recursively in a directory
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1726692012496/5819cade-b916-4464-a193-8937157466a3.png align="center")

### **9\. Text Editors**

Text editors allow you to view or edit files from the terminal. Popular choices include:

**Nano**: Simple and user-friendly.

* **Opening a File:** To open or create a file with `nano`, use the following command:
    

```plaintext
nano filename
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1726124470056/83025d02-75ab-4b14-99c4-566675c6caef.png?auto=compress,format&format=webp align="left")

* **Editing Text:** Simply type to insert text. Use arrow keys to navigate the file.
    
* **Saving Changes:** Press `Ctrl + O` (write out), then press `Enter` to save the file.
    
* **Exiting** `nano`: Press `Ctrl + X`. If you have unsaved changes, you will be prompted to save them.
    
* **Cutting and Pasting Text:**
    
    * **Cut:** Press `Ctrl + K` to cut a line.
        
    * **Paste:** Press `Ctrl + U` to paste the cut text.
        

**Vi**: More advanced, with powerful features for text manipulation.

* **Opening a File:** To open a file with `vi`, use:
    

```plaintext
vi filename
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1726124506742/edcba41d-9660-41a9-8a74-f75f371cf526.png?auto=compress,format&format=webp align="left")

* **Modes in** `vi`:
    
    * **Normal Mode:** Default mode for navigation and commands. Press `Esc` to enter normal mode.
        
    * **Insert Mode:** Used for editing text. Enter by pressing `i`.
        
    * **Command Mode:** Used for executing commands. Enter by pressing `:`.
        
* **Editing Text:**
    
    * **Enter Insert Mode:** Press `i` (insert before the cursor) or `a` (append after the cursor).
        
    * **Exit Insert Mode:** Press `Esc`.
        
* **Saving Changes:**
    
    * **Save and Exit:** Type `:wq` and press `Enter`.
        
    * **Save Without Exiting:** Type `:w` and press `Enter`.
        
    * **Exit Without Saving:** Type `:q!` and press `Enter`.
        
* **Cutting and Pasting Text:**
    
    * **Cut Line:** In normal mode, press `dd`.
        
    * **Paste Line:** Press `p` after cutting.
        

### **10\. Automation with Shell Scripting**

Shell scripting allows you to automate repetitive tasks by writing a series of commands in a script file.

#### Basic Shell Script Example:

Create a script:

```plaintext
nano script.sh
```

Write your script:

```plaintext
#!/bin/bash
echo "Hello, Linux World!"
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1726692117674/b3d0b2ea-217a-4245-b071-45c0f74c82ff.png align="center")

Make it executable:

```plaintext
chmod +x script.sh
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1726692184536/98836bb4-7d30-423d-ba85-16ab35241103.png align="center")

Run the script:

```plaintext
./script.sh
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1726692221373/d00e8b12-a1a4-4636-8a9a-b52662693d2f.png align="center")

### Conclusion

Mastering the Linux command line is a crucial skill for system administrators, developers, and DevOps professionals. With the commands covered here, you’re well on your way to becoming proficient with the Linux terminal. Once you get comfortable, you can start automating tasks, processing large amounts of data, and managing systems with ease.
