# Top 23 Linux Commands YOU MUST KNOW !

Linux Operating System is used to run over 90% of all cloud infrastructure and hosting services. For this reason , it's important to be familiar with popular Linux command. I am a Junior Software Engineer , I am using Amazon Linux Machine for demonstration. Let’s get right into it!

## What Is a Linux Command?

A Linux command is a program or utility that runs on the command line. A command line is an interface that accepts lines of text and processes them into instructions for your computer.

*Before start discussing commands you should know following basic concepts:*

* **Directory :** In Linux folder refers to directory.
    
* **Arguments :**
    
    Argument is an input given as part of commands.
    
    *Syntax*
    
    ```bash
    <command> <argument>  
    <command> <argument> <argument>
    ```
    
    *Example*
    
    ```bash
    [ec2-user@ip-172-31-29-38 ~]$ cd GitDev
    [ec2-user@ip-172-31-29-38 GitDev]$ ls
    [ec2-user@ip-172-31-29-38 GitDev]$ touch code.py test.py
    [ec2-user@ip-172-31-29-38 GitDev]$ ls
    code.py test.py
    ```
    
    Look at the above, command **cd GitDev** has changed our directory to **GitDev** and **touch code.py test.py** created two files for us. These are arguments which are inputs to commands.
    
* **Options :**
    
    Options modify the default behavior of the command. Generally, they are used to change the output.
    
    *Example*
    
    ```bash
    [ec2-user@ip-172-31-29-38 GitDev]$ ls
    code.py  test.py
    [ec2-user@ip-172-31-29-38 GitDev]$ ls -l
    total 0
    -rw-r--r--. 1 ec2-user ec2-user 0 Mar  4 17:19 code.py
    -rw-r--r--. 1 ec2-user ec2-user 0 Mar  4 17:21 test.py
    [ec2-user@ip-172-31-29-38 GitDev]$ ls --inode
    493092 code.py  493093 test.py
    ```
    
    By default without option, the **ls** command lists the contents of the current directory. In the second command, we used the **\-l** option the command displays contents with their properties in a list format. When we use **\--inode** option in third the **ls** command, the command displays the **inode** number with the contents.
    
* **Absolute Path**
    
    An absolute path is the location of a file or directory specified from the root working directory(/). Absolute path is complete path of file.
    
    *Example:*
    
    ```bash
    
    [ec2-user@ip-172-31-29-38 ~]$ ls /home/ec2-user/GitDev/
    code.py  test.py
    ```
    
    In the above example we provided complete path **/home/ec2-user/GitDev/** of **GitDev** directory to list content of directory.
    
* ***Relative Path***
    
    Relative path is defined as path related to the current working directory.
    
    *Example :*
    
    ```bash
    [ec2-user@ip-172-31-29-38 ~]$ cd GitDev/
    [ec2-user@ip-172-31-29-38 GitDev]$ ls
    code.py  test.py
    ```
    
    In the above example we are changing directory to **GitDev** using relative path **GitDev/.** There is no / before argument **GitDev** which indicates it’s a relative directory to present working directory.
    

### 1.pwd

<mark>pwd</mark> command use to print the current working directory.

```bash
[ec2-user@ip-172-31-29-38 ~]$ pwd
/home/ec2-user
```

Here, my current working directory is **/home/ec2-user**.

### 2.ls

<mark>ls</mark> command use to list the content of directory.

```bash
          [ec2-user@ip-172-31-29-38 GitDev]$ ls
          code.py  test.py
          [ec2-user@ip-172-31-29-38 GitDev]$ ls -l
          total 0
          -rw-r--r--. 1 ec2-user ec2-user 0 Mar  4 17:19 code.py
          -rw-r--r--. 1 ec2-user ec2-user 0 Mar  4 17:21 test.py
```

The **ls** command lists the contents of the **GitDev** directory. In the second command, we used the **\-l** option the command displays contents with their properties in a list format.

### 3.cd

<mark>cd</mark> command use to change the directory:

* Change directory to
    
* Change directory to **home** (~) directory
    
    ```bash
    [ec2-user@ip-172-31-29-38 ~]$ pwd
    /home/ec2-user
    [ec2-user@ip-172-31-29-38 ~]$ cd ~
    [ec2-user@ip-172-31-29-38 ~]$ pwd
    /home/ec2-user
    ```
    
* change directory to **parent** (..) directory
    
    ```bash
    [ec2-user@ip-172-31-29-38 ~]$ pwd
    /home/ec2-user
    [ec2-user@ip-172-31-29-38 ~]$ cd ..
    [ec2-user@ip-172-31-29-38 home]$ pwd
    /home
    ```
    
* change directory to **previous (-)** directory
    
    ```bash
    [ec2-user@ip-172-31-29-38 home]$ pwd
    /home
    [ec2-user@ip-172-31-29-38 home]$ cd -
    /home/ec2-user
    ```
    

### 4.touch

<mark>touch</mark> command use to create file without content.

* ```bash
                [ec2-user@ip-172-31-29-38 GitDev]$ touch code.py test.py
                [ec2-user@ip-172-31-29-38 GitDev]$ ls
                code.py test.py
    ```
    

### 5.cat

<mark>cat</mark> command use to display the content o file. It is used to read the data from a file and gives its content as output. It also helps us to create, view, and concatenate files.

```bash
[ec2-user@ip-172-31-29-38 ~]$ cat /etc/os-release
NAME="Amazon Linux"
VERSION="2023"
ID="amzn"
ID_LIKE="fedora"
VERSION_ID="2023"
PLATFORM_ID="platform:al2023"
PRETTY_NAME="Amazon Linux 2023"
ANSI_COLOR="0;33"
CPE_NAME="cpe:2.3:o:amazon:amazon_linux:2023"
HOME_URL="https://aws.amazon.com/linux/"
BUG_REPORT_URL="https://github.com/amazonlinux/amazon-linux-2023"
SUPPORT_END="2028-03-15"
[ec2-user@ip-172-31-29-38 ~]$
```

To concatenate several files, enter:

```bash
[ec2-user@ip-172-31-29-38 GitDev]$ touch dev1.py dev2.py
[ec2-user@ip-172-31-29-38 GitDev]$ ls
code.py  dev1.py  dev2.py  test.py
[ec2-user@ip-172-31-29-38 GitDev]$ cat dev1.py dev2.py > final.py
[ec2-user@ip-172-31-29-38 GitDev]$ ls
code.py  dev1.py  dev2.py  final.py  test.py
```

This command creates a file named **final.py** that is a copy of **dev1.py** followed by **dev2.py**

### 6.mkdir

<mark>mkdir</mark> command use to create new directory in the current working directory. The mkdir command allows you to create directories from within the terminal. The default syntax is **mkdir** followed by the directory name.

```bash
[ec2-user@ip-172-31-29-38 ~]$ mkdir GitDev
[ec2-user@ip-172-31-29-38 ~]$ ls
GitDev
```

You can see ,we have created the **GitDev** directory.

### 7.cp

<mark>cp</mark> command use to copy files and directories from source to desired destination.

* **Copying Files**
    
    The following are examples of coping files
    
    * To make copy of file withing the current directory
        
        ```bash
        [ec2-user@ip-172-31-29-38 GitDev]$ cp code.py copy_code.py
        [ec2-user@ip-172-31-29-38 GitDev]$ ls
        code.py  copy_code.py  dev1.py  dev2.py  final.py  test.py
        ```
        
        This created copy of **code.py** file as **copy\_code.py**
        
    * To copy a file in your current directory into another directory
        
        ```bash
        [ec2-user@ip-172-31-29-38 ~]$ cp main.py /home/ec2-user/GitDev
        [ec2-user@ip-172-31-29-38 ~]$ cd GitDev/
        [ec2-user@ip-172-31-29-38 GitDev]$ ls
        code.py  copy_code.py  dev1.py  dev2.py  final.py  main.py  test.py
        ```
        
        This copied main.py file from current directory to **/home/ec2-user/GitDev** directory
        
    * To copy all the files in a directory to a new directory
        
        ```bash
        [ec2-user@ip-172-31-29-38 ~]$ cp GitDev/* GitTest
        [ec2-user@ip-172-31-29-38 ~]$ cd GitTest/
        [ec2-user@ip-172-31-29-38 GitTest]$ ls
        code.py  copy_code.py  dev1.py  dev2.py  final.py  main.py  test.py
        ```
        
        This copied all files from **GitDev** directory to **GitTest** directory.
        
* **Copying Directory**
    
    To copy all the files from source directory to destination directory
    
    ```bash
    [ec2-user@ip-172-31-29-38 ~]$ cp -r GitDev GitTest
    [ec2-user@ip-172-31-29-38 ~]$ cd GitTest/
    [ec2-user@ip-172-31-29-38 GitTest]$ ls
    code.py  copy_code.py  dev1.py  dev2.py  final.py  main.py  test.p
    ```
    

### 8.mv

**<mark>mv</mark>** command use to **move** files and directories from one directory to another or to **rename** a file or directory

* **Move files**
    
    ```bash
    [ec2-user@ip-172-31-29-38 ~]$ mv final.py GitDev/
    [ec2-user@ip-172-31-29-38 GitDev]$ ls
    final.py
    ```
    
    Here, we moved **final.py** to **GitDev.** Use **\-r**(recursively) option to move files more than one.
    
* **Rename file**
    
    ```bash
    [ec2-user@ip-172-31-29-38 ~]$ mv main.py new_name.py
    [ec2-user@ip-172-31-29-38 ~]$ ls
    new_name.py
    ```
    
    Here, we renamed **main.py** to **new\_name.py**
    

### 9.rm

**<mark>rm</mark>** use to delete file from current

```bash
[ec2-user@ip-172-31-29-38 ~]$ ls
GitDev  GitTest  main.py
[ec2-user@ip-172-31-29-38 ~]$ rm main.py 
[ec2-user@ip-172-31-29-38 ~]$ ls
GitDev  GitTest
```

To delete all the files in the mydir directory, one by one, type the following:

```bash
[ec2-user@ip-172-31-29-38 ~]$ cd GitDev/
[ec2-user@ip-172-31-29-38 GitDev]$ ls
code.py  copy_code.py  dev1.py  dev2.py  final.py  main.py  test.py
[ec2-user@ip-172-31-29-38 GitDev]$ cd ..
[ec2-user@ip-172-31-29-38 ~]$ rm -i GitDev/*
rm: remove regular empty file 'GitDev/code.py'? y
rm: remove regular empty file 'GitDev/copy_code.py'? yy
rm: remove regular empty file 'GitDev/dev1.py'? y
rm: remove regular empty file 'GitDev/dev2.py'? y
rm: remove regular empty file 'GitDev/final.py'? y 
rm: remove regular empty file 'GitDev/main.py'? y
rm: remove regular empty file 'GitDev/test.py'? y
[ec2-user@ip-172-31-29-38 ~]$ cd GitDev/
[ec2-user@ip-172-31-29-38 GitDev]$ ls
```

After each file name displays, type <kbd>y</kbd> and press Enter to delete the file.

### 10.rmdir

<mark>rmdir</mark> command uses to delete directory.

* **Remove empty directory***GitDev*
    
    ```bash
    [ec2-user@ip-172-31-29-38 ~]$ rmdir GitDev/
    [ec2-user@ip-172-31-29-38 ~]$ ls
    GitTest
    ```
    
* **Remove non empty directory***GitTest*
    
    ```bash
    [ec2-user@ip-172-31-29-38 ~]$ rmdir -r GitTest/
    [ec2-user@ip-172-31-29-38 ~]$ ls
    ```
    

### 12\. find

<mark>find</mark> command use to find the file in directory.

Syntax : *find directory\_path -name file\_name*

Here directory\_path is path of directory where you want search file.

```bash
ec2-user@ip-172-31-28-71 ~]$ find . -name main.py
./GitDev/main.py
```

In the above command searches file main.py in current directory.

```bash
[ec2-user@ip-172-31-28-71 ~]$ find . -name *.py    
./GitDev/main.py
./GitDev/test.py
```

In the above command searches all the which file name ends with .py in current directory.

### 13.grep

<mark>grep</mark> command use to search lines in file matching pattern.

```bash
[ec2-user@ip-172-31-28-71 ~]$ cat main.py    
# This program adds two numbers
num1 = 1.5
num2 = 6.3
# Add two numbers
sum = num1 + num2
# Display the sum
print('The sum of {0} and {1} is {2}'.format(num1, num2, sum))
[ec2-user@ip-172-31-29-58 ~]$ grep -i "num" main.py 
# This program adds two numbers
num1 = 1.5
num2 = 6.3
# Add two numbers
sum = num1 + num2
print('The sum of {0} and {1} is {2}'.format(num1, num2, sum))
[ec2-user@ip-172-31-29-58 ~]$ grep -c "num" main.py 
6
```

The -i option enables to search for a string case insensitively in the given file. It matches the words "num" ,"num1", "num2". We can find the number of lines that matches the given string/pattern using option -c.

### 14.awk

<mark>awk</mark> command use for pattern search and processing.

What operations awk command can do?

* Scanning files line by line
    
* Splitting each input line into fields
    
* Comparing input lines and fields to patterns
    
* Performing specified actions on matching lines
    
    ```bash
    [ec2-user@ip-172-31-28-71 ~]$ cat main.py    
    # This program adds two numbers
    num1 = 1.5
    num2 = 6.3
    # Add two numbers
    sum = num1 + num2
    # Display the sum
    print('The sum of {0} and {1} is {2}'.format(num1, num2, sum))
    [ec2-user@ip-172-31-29-58 ~]$  awk '/num/ {print}' main.py 
    # This program adds two numbers
    num1 = 1.5
    num2 = 6.3
    # Add two numbers
    sum = num1 + num2
    print('The sum of {0} and {1} is {2}'.format(num1, num2, sum))
    [ec2-user@ip-172-31-29-58 ~]$ awk '{print $1,$4}' main.py 
    # adds
    num1 
    num2 
    # numbers
    sum +
    # sum
    print('The {0}
    ```
    

awk '/num/ {print}' [main.py](http://main.py) this prints the lines which match the given pattern. awk '{print $1,$4}' main.py splitting a line into two fields $1 and $4.

### 15.df

<mark>df</mark> command is used to see the storage usage by different file system directories (technical term here is file system mounts).This displays information about total space and available space on a file system.

```bash
[ec2-user@ip-172-31-28-71 ~]$ df
Filesystem     1K-blocks    Used Available Use% Mounted on
devtmpfs            4096       0      4096   0% /dev
tmpfs             486172       0    486172   0% /dev/shm
tmpfs             194472    2880    191592   2% /run
/dev/xvda1       8310764 1591284   6719480  20% /
tmpfs             486172       0    486172   0% /tmp
/dev/xvda128       10202    1310      8892  13% /boot/efi
tmpfs              97232       0     97232   0% /run/user/1000
```

### 16.du

<mark>du</mark> command use to print disk usage in MB Gb. This displays the number of blocks used for files.

Exit Status:

This command returns the following exit values:

| **Item** | **Description** |
| --- | --- |
| **0** | Successful completion. |
| **\&gt;0** | An error occurred. |

```bash
[ec2-user@ip-172-31-28-71 ~]$ du
4       ./.ssh
12      ./GitDev
36      .
```

This displays the number of disk blocks in the current directory and each of its subdirectories.

### 17.lscpu

<mark>lscpu</mark> command use to print CPU information. The lscpu command in Linux with the `-J` argument generates the output in JSON format.

```bash
[ec2-user@ip-172-31-28-71 ~]$ lscpu -J
{
   "lscpu": [
      {
         "field": "Architecture:",
         "data": "x86_64",
         "children": [
            {
               "field": "CPU op-mode(s):",
               "data": "32-bit, 64-bit"
            },{
               "field": "Address sizes:",
               "data": "46 bits physical, 48 bits virtual"
            },{
               "field": "Byte Order:",
               "data": "Little Endian"
               }
     ]
}
MORE
```

### 18.ps

<mark>ps </mark> command use to get the information of processes. Processes need system resources like CPU, Physical memory etc. to stay alive.ps is a powerful tool that allows you to view information about the processes running on your Linux system. It helps monitor running processes, identify process ID (**PID**), terminal type (**TTY**), **CPU** time usage, command name, user ID and other information.

* ***ps -ef or ps -aux****− List currently running processes in full format*
    

```bash
[ec2-user@ip-172-31-28-71 ~]$ ps -ef
UID          PID    PPID  C STIME TTY          TIME CMD
root           1       0  0 Mar06 ?        00:00:05 /usr/lib/systemd/systemd --switched-root --system --deserialize=32
root           2       0  0 Mar06 ?        00:00:00 [kthreadd]
root           3       2  0 Mar06 ?        00:00:00 [rcu_gp]
root           4       2  0 Mar06 ?        00:00:00 [rcu_par_gp]
root           5       2  0 Mar06 ?        00:00:00 [slub_flushwq]
root           6       2  0 Mar06 ?        00:00:00 [netns]
MORE
```

### 19.Kill

<mark>Kill</mark> command use to kill process.

Syntax : kill -9 process\_id or process\_name

```bash
[ec2-user@ip-172-31-28-71 ~]$ ps -ef | grep http
root       59143       1  0 16:57 ?        00:00:00 /usr/sbin/httpd -DFOREGROUND
apache     59164   59143  0 16:57 ?        00:00:00 /usr/sbin/httpd -DFOREGROUND
apache     59165   59143  0 16:57 ?        00:00:00 /usr/sbin/httpd -DFOREGROUND
apache     59166   59143  0 16:57 ?        00:00:00 /usr/sbin/httpd -DFOREGROUND
apache     59167   59143  0 16:57 ?        00:00:00 /usr/sbin/httpd -DFOREGROUND
ec2-user   62502   38348  0 17:03 pts/0    00:00:00 grep --color=auto http
[ec2-user@ip-172-31-28-71 ~]$ sudo kill -9 59164
[ec2-user@ip-172-31-28-71 ~]$
```

This kills process apache which has process id 59143. You can get process ids from ps command.

### 20.netstat

<mark>netstat</mark>  -an command that there’s indeed a process listening on ports. This command helps to check things about how your computer connects to the internet. netstat stands for network statistics.

```bash
[ec2-user@ip-172-31-29-58 ~]$ netstat -a | more 
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address           Foreign Address         State      
tcp        0      0 0.0.0.0:ssh             0.0.0.0:*               LISTEN     
tcp        0      0 ip-172-31-29-58.us-:ssh ec2-18-237-140-16:52390 ESTABLISHED
tcp6       0      0 [::]:ssh                [::]:*                  LISTEN     
udp        0      0 ip-172-31-29-58.:bootpc 0.0.0.0:*                          
udp        0      0 localhost:323           0.0.0.0:*                          
udp6       0      0 ip-172-31:dhcpv6-client [::]:*                             
udp6       0      0 localhost6:323          [::]:*                             
raw6       0      0 [::]:ipv6-icmp          [::]:*                  7          
Active UNIX domain sockets (servers and established)
Proto RefCnt Flags       Type       State         I-Node   Path
unix  3      [ ]         STREAM     CONNECTED     18595    
unix  3      [ ]         STREAM     CONNECTED     18690    /run/systemd/journal/stdout
unix  3      [ ]         STREAM     CONNECTED     16002    
unix  2      [ ACC ]     STREAM     LISTENING     14501    /run/systemd/resolve/io.systemd.Resolve
unix  2      [ ACC ]     STREAM     LISTENING     14506    /run/systemd/resolve/io.systemd.Resolve.Monitor
--More--
```

netstat -a | more show both listening and non-listening sockets. With the –interfaces option, show interfaces that are not up.

### 21.ifconfig

<mark>ifconfig </mark> command find the IP addresses. Configures or displays network interface parameters for a network by using TCP/IP.

```bash
[ec2-user@ip-172-31-28-71 ~]$ ifconfig
enX0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 9001
        inet 172.31.28.71  netmask 255.255.240.0  broadcast 172.31.31.255
        inet6 fe80::49:54ff:fec4:c79d  prefixlen 64  scopeid 0x20<link>
        ether 02:49:54:c4:c7:9d  txqueuelen 1000  (Ethernet)
        RX packets 48407  bytes 32785275 (31.2 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 31357  bytes 3158831 (3.0 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 12  bytes 1020 (1020.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 12  bytes 1020 (1020.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

[ec2-user@ip-172-31-28-71 ~]$
```

### 22.tcpdump

<mark>tcpdump</mark> command can be used to find all communications (aka network packages) happening through your network (coming in or going out of your machine)

### 23.ping

<mark>ping</mark> is a network utility tool and helps to check if a particular IP address is reachable

```bash
[ec2-user@ip-172-31-28-71 ~]$ ping google.com
PING google.com (142.250.217.110) 56(84) bytes of data.
64 bytes from sea09s30-in-f14.1e100.net (142.250.217.110): icmp_seq=1 ttl=58 time=7.77 ms
64 bytes from sea09s30-in-f14.1e100.net (142.250.217.110): icmp_seq=2 ttl=58 time=7.82 ms
64 bytes from sea09s30-in-f14.1e100.net (142.250.217.110): icmp_seq=3 ttl=58 time=7.90 ms
64 bytes from sea09s30-in-f14.1e100.net (142.250.217.110): icmp_seq=4 ttl=58 time=7.87 ms
64 bytes from sea09s30-in-f14.1e100.net (142.250.217.110): icmp_seq=5 ttl=58 time=7.86 ms
64 bytes from sea09s30-in-f14.1e100.net (142.250.217.110): icmp_seq=6 ttl=58 time=7.79 ms
64 bytes from sea09s30-in-f14.1e100.net (142.250.217.110): icmp_seq=7 ttl=58 time=7.83 ms
64 bytes from sea09s30-in-f14.1e100.net (142.250.217.110): icmp_seq=8 ttl=58 time=7.82 ms
^C
--- google.com ping statistics ---
8 packets transmitted, 8 received, 0% packet loss, time 7012ms
rtt min/avg/max/mdev = 7.769/7.832/7.900/0.040 ms
[ec2-user@ip-172-31-28-71 ~]$ ping git.com
PING git.com (44.233.224.228) 56(84) bytes of data.
^C
--- git.com ping statistics ---
56 packets transmitted, 0 received, 100% packet loss, time 57168ms

[ec2-user@ip-172-31-28-71 ~]$
```
