# Commands

Most frequently used CLI (Terminal) commands on Linux

# Commands used for System Information

<table border="1" class="align-left" id="bkmrk-command%28s%29-sample-ou" style="border-collapse: collapse; width: 100%; border-width: 1px; border-style: solid;"><colgroup><col style="width: 36.5793%;"></col><col style="width: 63.3959%;"></col></colgroup><tbody><tr><td class="align-center" style="border-width: 1px;">**Command(s)**  
</td><td class="align-center" style="border-width: 1px;">**Sample Output**  
</td></tr><tr><td style="border-width: 1px;">**DATE**

The simple “date” command displays the current date and time (including the day of the week, month, time, time zone, year).

```shell
date
```

**Date TZ**

By default, “date” command uses the time zone defined in path “/etc/localtime”. Linux user can change the time zone via Terminal by using command “TZ”.

```shell
TZ=GMT date
```

```
TZ=America/New_York date
```

**Date --set**

Linux allows its user to set the current date and time of the system manually.  
Syntax: *date –set=”Date\_in\_format(YYMMDD) Time\_in\_format(HH:MM)”*

```
sudo date --set="YYYYMMDD HH:MM"
```

</td><td style="border-width: 1px;">`$ dateThu Mar  2 07:23:38 PM EST 2023$ TZ=GMT dateFri Mar  3 12:03:59 AM GMT 2023$ TZ=America/New_York dateThu Mar  2 07:04:12 PM EST 2023`

Example - This would set the system date to May 18, 2023 10:10 PM

```shell
sudo date --set="20230519 22:10"
```

</td></tr><tr><td style="border-width: 1px;">**DF**

The command “df” shows the amount of disk space used and disk space available on every file system containing each filesystem’s name and its path.

```shell
df
```

The command “df -h” shows the same result as the command “df” but now the data is in a more human-readable format.

```shell
df -h
```

</td><td style="border-width: 1px;">`$ dfFilesystem     1K-blocks       Used  Available Use% Mountedtmpfs            1623284       3612    1619672   1% /run/dev/nvme0n1p3 491343600   18123184  452739188   4% /tmpfs            8116400     104604    8011796   2% /dev/shmtmpfs               5120          4       5116   1% /run/locktmpfs            8116400          0    8116400   0% /run/qemu/dev/nvme0n1p2    456036     182424     239424  44% /boot/dev/nvme0n1p1     98304      57271      41033  59% /boot/efi/dev/sda1       47744748      57156   45229840   1% /tmp/dev/sda3     2787016696 1123163768 1531975216  43% /home/dev/sda2       47745772   31301948   13986020  70% /var`

`$ df -hFilesystem      Size  Used Avail Use% Mounted ontmpfs           1.6G  3.6M  1.6G   1% /run/dev/nvme0n1p3  469G   18G  432G   4% /tmpfs           7.8G  103M  7.7G   2% /dev/shmtmpfs           5.0M  4.0K  5.0M   1% /run/locktmpfs           7.8G     0  7.8G   0% /run/qemu/dev/nvme0n1p2  446M  179M  234M  44% /boot/dev/nvme0n1p1  96M   56M   41M  59% /boot/efi/dev/sda1       46G   56M   44G   1% /tmp/dev/sda3       2.6T  1.1T  1.5T  43% /home/dev/sda2       46G   30G   14G  70% /var `

</td></tr><tr><td style="border-width: 1px;">**FREE**

The command “free” displays the amount of free and used memory in the complete system.

```shell
free
```

</td><td style="border-width: 1px;">`$ free         total     used     free    shared  buff/cache   availableMem:  16232800  4698636  6761664     59100     4772500    11205016Swap:  2097152        0  2097152`  
</td></tr><tr><td style="border-width: 1px;">**PS**

The command “ps” which is also known as the process status command is used to provide information about the processes currently running on the system, including their respective process identification numbers (PIDs)

```shell
ps
```

**PS AUX &amp; PS -EF**

Both list all processes of all users. In that aspect `-e` and `ax` are completely equivalent.

Where they differ is output format specifier, `-f` is "full", while `u` is "user-oriented". The displayed columns are different:

```shell
ps -ef
```

```shell
ps -aux
```

</td><td style="border-width: 1px;">`$ ps    PID TTY          TIME CMD 434765 pts/0    00:00:00 bash 441698 pts/0    00:00:00 ps`

`$ ps -efUID          PID    PPID  C STIME TTY          TIME CMDroot           1       0  0 Feb07 ?        00:19:03 /sbin/init splashroot           2       0  0 Feb07 ?        00:00:03 [kthreadd]root           3       2  0 Feb07 ?        00:00:00 [rcu_gp]root           4       2  0 Feb07 ?        00:00:00 [rcu_par_gp]root           5       2  0 Feb07 ?        00:00:00 [slub_flushwq]root           6       2  0 Feb07 ?        00:00:00 [netns]`

`$ ps -auxUSER  PID %CPU %MEM  VSZ  RSS TTY STAT START TIME COMMANDroot   1  0.0  0.0 169808 12472 ? Ss   Feb07  19:03 /sbin/init splashroot    2  0.0  0.0  0     0    ? S    Feb07   0:03 [kthreadd]root    3  0.0  0.0  0     0    ? I<   Feb07   0:00 [rcu_gp]root    4  0.0  0.0  0     0    ? I<   Feb07   0:00 [rcu_par_gp]root    5  0.0  0.0  0     0    ? I<   Feb07   0:00 [slub_flushwq]root    6  0.0  0.0      0     0 ?        I<   Feb07   0:00 [netns]`

  
</td></tr><tr><td style="border-width: 1px;">**UPTIME**

The command “uptime” provides information about how long the system has been running in one line. The result for this command includes the current time, the time duration system has been running, the number of users who are currently logged on, and the system load averages for the past 1, 5, and 15 minutes respectively.

```shell
uptime
```

</td><td style="border-width: 1px;">`$ uptime 19:59:06 up 22 days, 20:17,  1 user,  load average: 0.52, 0.39, 0.38`  
</td></tr><tr><td style="border-width: 1px;"> **W**

The command “w” displays detailed information about the users who are logged into the system currently.

```shell
w
```

</td><td style="border-width: 1px;">`$ w 20:00:25 up 22 days, 20:18,  1 user,  load average: 0.44, 0.41, 0.38USER     TTY      FROM             LOGIN   IDLE   JCPU   PCPU WHATtim      :1       :1               07Feb23  xdm   2days  0.00s /usr/libexec/gdm-x-session --run-script env`  
</td></tr><tr><td style="border-width: 1px;">**PASSWD**

The command “passwd” stands for password and it is used to change the password of the user.

```shell
passwd my_user
```

</td><td style="border-width: 1px;">`$ passwd timchanging password for tim.(current) UNIX password:Enter new UNIX password:Retype new UNIX password:passwd: password updated successfully`  
</td></tr><tr><td style="border-width: 1px;">**EXIT**

The command “exit” as the name says it is used to exit from the system and log out from the current user.

```shell
exit
```

</td><td style="border-width: 1px;">`$ exitlogoutConnection to 192.168.1.1 closed.`  
</td></tr><tr><td style="border-width: 1px;">**SS**

The ss command is a modern replacement for the classic netstat command. You can use it on Linux to get statistics about your network connections.

Use the **-ltn** flags to list all listening ports on your system.

```shell
ss -ltn
```

Use the following to see if a specific port is listening on your system. In this case, the source port (sport) and destination port (dport) is 80 or you can use the protocol name, http, instead.

```shell
ss -a '( dport = :80 or sport = :80 )'
```

```shell
ss -a '( dport = :http or sport = :http )'
```

</td><td style="border-width: 1px;">`$ ss -ltnState   Recv-Q Send-Q Local Address:Port Peer Address:Port Process  LISTEN  0      4096   0.0.0.0:9000       0.0.0.0:*              LISTEN  0      4096   0.0.0.0:6767       0.0.0.0:*              LISTEN  0      4096   0.0.0.0:111        0.0.0.0:*              LISTEN  0      4096   0.0.0.0:80         0.0.0.0:* `

```
$ ss -a '( dport = :80 or sport = :80 )'
Netid State     Recv-Q Send-Q    Local Address:Port     Peer Address:PortProcess
tcp   LISTEN    0      4096            0.0.0.0:http          0.0.0.0:*          
tcp   LISTEN    0      4096               [::]:http             [::]:*
```

</td></tr><tr><td style="border-width: 1px;">**SHUTDOWN**

The command “shutdown” is used to shut down the system.

<p class="callout info">Note: The shutdown command needs superuser privileges. Hence, you should either be root or run the command with sudo.</p>

Using the command with no flags will schedule a shutdown 1 minute from execution.

```shell
sudo shutdown
```

Use the following to IMMEDIATELY shutdown your system.

```shell
sudo shutdown now
```

You can schedule a shutdown in future by providing the time argument either in +t format or in hh:mm format.

For example, if you want to shutdown the system after 15 minutes, you can use this command:

```shell
sudo shutdown +15
```

If you want to shutdown the system at 6 PM in the afternoon, you can use it in the following manner:

```shell
sudo shutdown 18:00
```

Cancel a shutdown

```shell
sudo shutdown -c
```

Reboot a system

```shell
sudo shutdown -r
```

```shell
sudo reboot
```

</td><td style="border-width: 1px;">`$ sudo shutdownShutdown scheduled for Thu 2023-03-02 20:12:13 EST, use 'shutdown -c' to cancel.`  
</td></tr><tr><td style="border-width: 1px;">  
</td><td style="border-width: 1px;">  
</td></tr><tr><td style="border-width: 1px;">  
</td><td style="border-width: 1px;">  
</td></tr><tr><td style="border-width: 1px;">  
</td><td style="border-width: 1px;">  
</td></tr></tbody></table>

###   

# Commands used for File Handling

<table border="1" id="bkmrk-%C2%A0-1" style="border-collapse: collapse; width: 100%;"><colgroup><col style="width: 33.3333%;"></col><col style="width: 33.3333%;"></col><col style="width: 33.3333%;"></col></colgroup><tbody><tr><td>**LS**

The command “ls” displays the list of all directories, folder, and files present in the current directory.

**LS - LTR**

The above-mentioned command displays the name of directories, folders, files with their respective owner name, group’s name, and rights your user has over these.

</td><td>```shell
ls
ls -ltr
```

</td><td>```powershell
/$ ls
bin    dev   lib    libx32      mnt   root  snap      sys  usr
boot   etc   lib32  lost+found  opt   run   srv       tim  var
cdrom  home  lib64  media       proc  sbin  swapfile  tmp
```

```powershell
/$ ls -ltr
total 2097256
drwxr-xr-x   2 root root       4096 Feb  9  2021 mnt
drwxr-xr-x  15 root root       4096 Feb  9  2021 var
drwx------   2 root root      16384 Feb 11  2022 lost+found
lrwxrwxrwx   1 root root          8 Feb 11  2022 sbin -> usr/sbin
lrwxrwxrwx   1 root root         10 Feb 11  2022 libx32 -> usr/libx32
lrwxrwxrwx   1 root root          9 Feb 11  2022 lib64 -> usr/lib64
lrwxrwxrwx   1 root root          9 Feb 11  2022 lib32 -> usr/lib32
lrwxrwxrwx   1 root root          7 Feb 11  2022 lib -> usr/lib
lrwxrwxrwx   1 root root          7 Feb 11  2022 bin -> usr/bin
drwxrwxr-x   2 root root       4096 Feb 11  2022 cdrom
drwx------  10 root root       4096 Feb 12  2022 tim
-rw-------   1 root root 2147487744 Aug  9  2022 swapfile
drwxr-xr-x   5 root root       4096 Aug 14  2022 home
drwxr-xr-x  14 root root       4096 Oct 22 14:08 usr
drwxrwxrwx   7 root root       4096 Nov 26 04:18 media
drwxrwxrwx  13 root root       4096 Jan  7 21:59 opt
drwxr-xr-x   2 root root       4096 Feb  5 07:19 srv
dr-xr-xr-x  13 root root          0 Feb  7 23:41 sys
dr-xr-xr-x 549 root root          0 Feb  7 23:41 proc
drwx------  12 root root       4096 Feb  8 04:25 root
drwxr-xr-x  20 root root       5120 Feb 17 23:04 dev
drwxr-xr-x  28 root root       4096 Feb 21 23:36 snap
drwxr-xr-x 168 root root      12288 Mar  1 06:24 etc
drwxr-xr-x   5 root root       4096 Mar  2 06:51 boot
drwxr-xr-x  46 root root       1500 Mar  2 20:11 run
drwxrwxrwt  32 root root      20480 Mar  2 21:06 tmp
```

</td></tr><tr><td>**MKDIR**

The command “mkdir” allows users to create directories/folders in the system. The user running this command must have suitable rights over the parent directory to create a directory or they will receive an error.  
Syntax: *mkdir New\_Directory’s\_Name*

</td><td> ```shell
mkdir NewDirectory
```

</td><td>```powershell
~$ mkdir poopoo
~$ 
~$ ls
 Android            Pictures
 AppImages          poopoo
```

</td></tr><tr><td>**RMDIR**

The command “rmdir” allows users to remove directories/folders from the system. The user running this command must have suitable rights over the parent directory to remove a directory AND the directory must not have any files or sub-directories within it or you will receive an error.  
Syntax: *rmdir Directory’s\_Name*

</td><td>```shell
rmdir DirectoryName
```

</td><td>```powershell
~$ rmdir poopoo
rmdir: failed to remove 'poopoo': Directory not empty
# Could not delete directory 
# "poopoo" because it is not
# empty

~$ rm poopoo
rm: cannot remove 'poopoo': Is a directory
# Could not remove "poopoo" 
# because it is not a file


```

</td></tr><tr><td>**RM**

The command “rm” is used to remove files from a directory.

**RM -RF**

Permanently deletes the specified directory and ALL files and sub-directories beneath the specified directory.

<p class="callout warning">Be VERY careful using this command as you can inadvertently delete your whole drive!</p>

</td><td>```shell
rm filename
```

```shell
rm -rf /path/to/dir/name
```

</td><td>```powershell
# Listing shows poopoo.txt 
# file exists under 
# direcotry "poopoo"
~/poopoo$ ls
poopoo.txt

~/poopoo$ rm poopoo.txt 

# listing now shows 
# poopoo.txt has been
# removed (deleted) 
# from directory "poopoo"
~/poopoo$ ls
~/poopoo$
```

```powershell
# Directory "poopoo" exists 
# in the listing below
~$ ls
 Android            Pictures
 AppImages          poopoo
 Audio              Public


~$ rm -rf poopoo
~$
# Successfully removed 
# "poopoo" directory 
# and all its contents
# as can be seen in the 
# listing below

~$ ls 
 Android            Parkitect
 AppImages          Pictures
 Audio              Public

```

</td></tr><tr><td>**MV**

The command “mv” is used for two purposes

- To move files or directories from one path to another path in the system.
- To rename a file or folder.

</td><td>```shell
mv Source_File_name Destination_File_Name
```

```shell
mv File_name New_name_for_file
```

</td><td>  
</td></tr><tr><td>**CP**

The command “cp” is used to copy data from a source file to the destination file. Its function is almost like the command “mv”. The only difference is by using the command “cp” the source file is not removed from the directory after its data is moved to the destination file.

</td><td>```shell
cp source_file_name destination_file_name
```

</td><td>  
</td></tr><tr><td>**TOUCH**

Creates an empty file at the specified path with the specified name.

Useful for creating a blank file you intend to edit with a CLI editor, such as VIM or NANO.

</td><td>```shell
touch /path/name/filename.ext
```

</td><td>```powershell
~$ ls doc.txt
ls: cannot access 'doc.txt': No such file or directory
~$ touch /home/tim/doc.txt
~$ ls doc.txt
doc.txt
```

</td></tr><tr><td>**CAT**

The command “cat” is a reverse of the command “tac”. It is used to display each line of the file starting from the first row and finishing on its last row.

This command is more frequently used than “tac”.

</td><td>```shell
cat file_name
```

</td><td>  
</td></tr><tr><td>**ECHO**

The command “echo” used to display any expression that is passed as an argument.

</td><td>```shell
echo expression_to_be_displayed
```

</td><td>```powershell
~/poopoo$ echo something-poopoo
something-poopoo
```

</td></tr><tr><td>**GREP**

The command “grep” is used to search for a text in the specified file/folder.

</td><td>```shell
grep “expression_to_be_Searched” file_name_to_search_in
```

</td><td> </td></tr><tr><td>**ZIP**

The command “zip” is used to compress one or more files and store them in a new file with .zip extension.

</td><td>```powershell
zip new_zip_file_name.zip
```

</td><td>```powershell
~/poopoo$ zip files.zip file1.txt file2.txt file3.txt 
  adding: file1.txt (stored 0%)
  adding: file2.txt (stored 0%)
  adding: file3.txt (stored 0%)
~/poopoo$ ls
file1.txt  file2.txt  file3.txt  files.zip
```

</td></tr><tr><td>**UNZIP**

The command “unzip” is used to decompress a .zip file and extract all the files within to current directory.

</td><td>```powershell
unzip zip_file_name.zip
```

</td><td>```powershell
~/poopoo$ unzip files.zip
Archive:  files.zip
replace file1.txt? [y]es, [n]o, [A]ll, [N]one, [r]ename: A
 extracting: file1.txt               
 extracting: file2.txt               
 extracting: file3.txt            
```

</td></tr><tr><td>**SUDO**

Sudo stands for SuperUser DO and is used to access restricted files and operations. By default, Linux restricts access to certain parts of the system preventing sensitive files from being compromised.

The **`sudo`** command temporarily elevates privileges allowing users to complete sensitive tasks without logging in as the root user.

**sudo -i** elevates the user to root for the remainder of the session rather than a command by command basis.

</td><td>```powershell
sudo some-command
```

```powershell
sudo -i
```

</td><td>```powershell
# No directory called "peepee"
# exists
$ ls
bin    dev   lib    libx32      mnt   root  snap      sys  usr
boot   etc   lib32  lost+found  opt   run   srv       tim  var
cdrom  home  lib64  media       proc  sbin  swapfile  tmp

# Attempt to make directory 
# "peepee" as a noraml user
# fails because I'm 
# trying to make the 
# directory in a path I 
# don't have rights to
$ mkdir peepee
mkdir: cannot create directory ‘peepee’: Permission denied

# using SUDO to temporarily 
# elevate my privileges, I 
# can now create the 
# directory "peepee"
# in the path as can be seen 
# in the listing below:
$ sudo mkdir peepee
[sudo] password for tim:

$ ls
bin    dev   lib    libx32      mnt     proc  sbin  swapfile  tmp
boot   etc   lib32  lost+found  opt     root  snap  sys       usr
cdrom  home  lib64  media       peepee  run   srv   tim       var

```

```powershell
~/poopoo$ sudo -i
[sudo] password for tim: 
~#
```

  
</td></tr><tr><td>  
</td><td>  
</td><td>  
</td></tr></tbody></table>