Linux Commands - DAY 6
Published: 2023-08-12 21:37:35

> **26-07-2023**

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qpx4xubbzkypxbswc43r.png)

## **<u>REBOOT</u>**

- restart or reboot of the Linux system .

```
sudo reboot
sudo shutdown -r now
sudo reboot -f --> force reboot
ssh root@remote-server /sbin/reboot --> remote server restart
cat /sbin/reboot  --> not in readable form
```

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ovb4mw8857nuco7zhay1.png)

---

## **<u>HELP</u>**

- help command provides information on built-in commands.
- which helps get information about all commands.

```
help help
help cd
help -d help
help -d ls
help -d cd
help -m help
help -m pwd

d --> short description
m --> pseudo-manpage format

help -m ls  [ not working , why ? ]
help -d cat [ not working , why ? ]
help cat [ not working , why ? ]
```
 
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6w5vwh6ukn6jdhp1uxw5.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1sng20zl32tep8edpjh2.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/juhhuesiwo1eyx20oeid.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nqhykg87p6gsdwn2vs8v.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ftukjxvabxizq1tlj6n8.png)

---

## **<u>AT, ATQ, BATCH, ATRM</u>**

- to schedule commands to be executed at a particular time.
- queue, examine, or delete jobs for later execution.

```
sudo apt install at
at
date
at 23.33
at> df -Th > df.txt
at> CTRL+D
atq --> to list the jobs in queue
atrm <job_number> --> cancel the jobs in queue
```

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/onmw22u1o9wy65w43wna.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cokd6jv2n8nxddvnk9nb.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0rfu5msyt2k80croj4if.png)

Why these commands not working ???

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2u5u1alcic6gecec4w7k.png)

---

## **<u>NOLOGIN</u>**

- it will allow users with no shell access.

```
sudo useradd -s /sbin/nologin user
cat /etc/passwd | grep nologin
tail -2 /etc/passwd
```

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/z2odp89vs8hgnuiczpxq.png)

---

## **<u>CHSH</u>**

- change to shell.

```

sudo chsh -s /bin/bash user4
tail -2 /etc/passwd
chsh ( just typing this will be helpful for current user logged in or To change the login shell for user) 
```
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rmem9q06bomvvuvx52cx.png)

---

## **<u>CRONTAB</u>**

- used to automatic schedule.

```
crontab -l
crontab -e

Eg., create a script name sample.sh to display amount of free
and used memory in the system with timestamp

cat > sample.sh
#!/bin/bash
free -h
echo "this is memory available"
current_time=$(date)
echo "time is: $current_time"

To execute this script for every 2 minutes and write to
file called free.log
create free.log file

touch free.log
crontab -e
*/2 * * * * /bin/bash /home/venus/sample.sh >>
/home/venus/free.log

Restart the service which is very important,
sudo systemctl restart cron.service
crontab -l
```

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2o0s873l8wg6z5ntg3cm.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/uxc6aabvcxvkpmcvzzw1.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/90616c0axf5zh9k90kcz.png)

---

## <u>**WGET**</u>

- used to non-interactive download of files from the web or to view the site status like testing the site is accessible or not.

```
wget http://path/to/url
wget https://www.kaniyam.com/download/Learn%20GNU
%20Linux%20in%20Tamil%20-%20Part%201.pdf
wget https://www.kaniyam.com/download/Learn-GNU-Linux-
in-Tamil-Part-2.pdf

cat downloads.txt  --> download multiple sites
http://path/to/url1
http://path/to/url2
http://path/to/url3

wget -i downloads.txt
wget http://path/to/url1 http://path/to/url2
```

---

## **<u>DU</u>**

- to estimate file space usage.

```
du /etc   --> it will show all files or folders size
du /home
du -h /etc  --> size in human readable format
du -h /home/testing
du -sh /etc --> total usage size of a particular directory
du -hs * --> size of current folder
du -chs *
du -hsc /home/testing --> grand total of the folder
```

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/evaoj2li88b0yc2mku9z.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/c41pxyw4bdnb8aqswt8x.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lffed3w8qlimue7p5ba5.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qlpnhi0pk9msbuqouejd.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hba3mxdgv40gzohgh4h5.png)

---

## **<u>SYSTEMCTL</u>**

- to control the service managers.

```
sudo systemctl stop mariadb.service
sudo systemctl start mariadb.service
sudo systemctl status mariadb.service
sudo systemctl enable mariadb.service --> this will help to start the service when the system gets rebooted.
sudo systemctl disable mariadb.service

sudo systemctl list-units --type=service --> status of all service

sudo systemctl list-units --type=service --state=active/running/stopped/enabled/disabled/failed
```

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mtzdzq6w55hiaiz145o5.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/n5e2nguxsrlco81z0mcm.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sxx1ylysd6k8li5p1p12.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nzr1p8fpm5ctbxro9scr.png)

```
sudo systemctl kill -s 9 <service_name> ( kill the service too)
systemctl daemon-reload ( reload all daemon services )
```

---

## <u>**TAR**</u>

- an archiving utility

```
mkdir tar_examples && cd tar_examples
mkdir files && cd files
touch file{0..1000}.txt
cd ..
tar cf myfiles.tar files  --> archiving using tar
tar tf myfiles.tar  --> list the files in tar
tar -xvf myfiles.tar --> to extract
tar cf myfiles.tar.gz files --> archive using gzip
tar tf myfiles.tar.gz  --> to list
tar -xvzf myfiles.tar.gz  --> to extract
```

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hnk63q95k2tffskarhhl.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/klc5o34dcq4abstczr8j.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2ch61l35myr7idf8ofrr.png)

---

## **<u>APT</u>**

- command-line interface for the package management.
- **Advanced package tool**, or APT, is a free-software user interface that works with core libraries to handle the installation and removal of software on Debian, and Debian-based Linux distributions.

```
sudo apt update
sudo apt install vsftpd apache2 mariadb-server --> install packages
sudo apt depends bind9  --> check dependencies
sudo apt search apache2 --> search packages
sudo apt show apache2   --> information about the package
sudo apt upgrade        --> mostly don't try this , until any issues are faced in the current version.
sudo apt autoremove     --> remove unused package
sudo apt autoclean      --> clean the repository

```

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jwol3o9yv3efmkx83rhy.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3y3ie1nj1ifmuqpy0o1z.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/z17nmn0f256nqyrzg0ve.png)

To removed any package, first keep in mind - stop the service,

```
sudo systemctl stop apache2
sudo apt purge apache2
sudo apt list

```
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6s6219eg8w020gbi8olc.png)

---

## <u>**ADD-APT-REPOSITORY**</u>

- Adds a PPA repository into the /etc/apt/sources.list.
- Is a script which adds an external APT repository to either /etc/apt/sources.list or a file in /etc/apt/sources.list.d/ or removes an already existing repository.
- A personal package archive (PPA) is a repository that hosts software packages built and maintained by individuals or groups and made available for others. 
- PPAs are a convenient way to access the latest software versions that are not yet available in the official repositories of the operating system.

```
sudo add-apt-repository ppa:PPA_REPOSITORY_NAME/PPA
sudo add-apt-repository ppa:ondrej/php --> php PPA repository
sudo apt update
sudo apt install php

sudo add-apt-repository --remove ppa:PPA_REPO_NAME/PPA
sudo add-apt-repository --remove ppa:ondrej/php
```

---

## <u>**RSYNC**</u>

- a fast, versatile, remote (and local) file-copying tool.
- Its an alternate for scp.
- scp will start from the begining if any network issue happens in between but rsync will start from where it stopped.

```
mkdir -p /home/venus/Documents/backup
rsync -v *.txt /home/venus/Documents/backup

rsync -av --progress *.txt
your_username@remote_server:/home/testing/ --> local to remote
rsync -av --progress testing@ip:/home/testing/*.txt . --> remote to local
```

---

## <u>**SCP**</u>

- OpenSSH secure file copy.

```
scp *.txt testing@remote_server:/home/testing/
scp testing@remote_server:/home/testing/file.txt .  --> server to local
scp -r example_folder testing@remote_server:/home/testing/ --> copy folder
scp -r testing@remote_server:/home/testing/example_folder .  --> remote to local
```

---

## <u>**CURL**</u>

- curl (short for "Client URL") is a command line tool that enables data transfer over various network protocols.
- Linux curl command is used to download or upload data to a server via supported protocols such as HTTP, FTP, IMAP, SFTP, TFTP, IMAP, POP3, SCP, etc. It is a remote utility, so it works without user interaction.

```
curl https://www.kaniyam.com/foundation/
curl https://www.kaniyam.com/foundation/ > curl_file.txt
curl -# -O https://www.kaniyam.com/download/Learn%20GNU%20Linux%20in%20Tamil%20-%20Part%201.pdf --> shows the download and transfer rate.
```

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vymmt54k5sl9m3shexlk.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ofu9sowywzdk8ed4pny4.png)

---

## <u>**FREE**</u>

- free and used memory in the system.

```
free
free -h   --> human readable
free -g/m/k/b  --> in Bytes/KB/MB/GB
free -s 2 --> every 2 sec
free -h > free.log  --> redirect 
```
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gd7l60621a2bkqtep1cg.png)

---

## <u>**IFCONFIG**</u>

- shows network interface.

```
ifconfig -a
ifconfig -s --> short 
ifconfig lo --> to display any one network
```

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5nvqnua07h2aib8b7tds.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dcpr2nmeqsltrdvg87ay.png)

---

## <u>**IP**</u>

- show / manipulate routing, network devices, interfaces and tunnels.
- It is similar to the ifconfig command.
- all these below commands will have information about the connected networks.

```
ip -a
ip -4 a
ip link ls up  --> shows the running interface
ip addr show
ip route show  --> IP route table
```

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/aer5m9vgnyb2a3iituvf.png)

---

## **<u>NETSTAT</u>**

- The network statistics ( netstat ) command is a networking tool used for troubleshooting and configuration, that can also serve as a monitoring tool for connections over the network.

```
netstat --all
netstat -l     --> list the port
netstat -at    --> list TCP port
netstat -au    --> UDP port alone
netstat -lt    --> TCP port 
netstat -s     --> statistics for all ports
netstat -c     --> continuously 
sudo netstat -tulpn | grep 80
sudo netstat -tulpn | grep https
```


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7np128wtjxx73y4mi30j.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gw4wluvut4igti6w4xlt.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/b5wjocccap4p4cp07b49.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9zydkq8g2lccvrwp6eog.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/46b78hkavnyumf3s66pj.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0uxxri73kmsd11c6zfgk.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/s7ja8ifdw4pr2ngz8rj6.png)

---

## <u>**PING**</u>

- send ICMP ECHO_REQUEST to network hosts.

```
ping google.com
ping -c 5 google.com  --> limit the number of pings
ping -c 5 -q google.com  --> print the statistics
```
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/l5jsq543gfm8vvc86hv6.png)

---

## **<u>ALIAS</u>**

- creating alias for a command which you use frequently or any command.
- Aliases expire with the current shell session . or
- Define in ~/.bashrc. which will be permanent.

```
alias c=clear
alias f=free -h
c
f

Above command won't work with another session.

vi ~/.bashrc
alias c=’clear’
alias u=’uptime’
aliad f=’free -h’
:wq! Save and exit

After adding enter the below command , so that the file gets refreshed.
source ~/.bashrc
```
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vbrk46j95uk92gl2pvtc.png)

---

## **<u>UNALIAS</u>**

- to remove the alias.

unalias u
unalias f

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sxmi058ogae00x94s84y.png)

---

## **<u>CLEAR</u>**

- which is used to clear the terminal.

```
clear
control + l
reset
```
---

## **<u>SOURCE</u>**

- source is a shell built-in command which is used to read and execute the content of a file(generally set of commands), passed as an argument in the current shell script.
- Execute commands from a file in the current shell.

```
source ~/.bashrc
source /etc/profile

cat > example.txt
free -h
pwd
date
time
uptime

source example.txt
```
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pr36fyraho0rfdcqgjtf.png)

---

## **<u>SH</u>**

- is a command language interpreter that executes commands read from a command line string, the standard input, or a specified file.

```
sh
sh sample.sh
```

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/upsqponla2v63yeq2aj5.png)

---

## **<u>SSH</u>**

- is a program for logging into a remote machine and for executing commands on a remote machine.

```
ssh user@122.132.123.33
ssh user@server.com
ssh user@my.server.in -p 2222 --> using port
```
---

## **<u>TLDR</u>**

- Display simple help pages for command-line tools.
- The TLDR (stands for “**Too Long; Didn't Read**“) is a community-driven project that provides concise and simplified documentation for various Linux commands.

```
tldr -u --> update the local cache of tldr pages
tldr free
```
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mbcyelf5yap6596opcjl.png)

Need to discuss this error ,

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sg5vd5kxskfyi9c0put8.png)

---

## **_<u>Important Notes:</u>_**

1. The Internet Control Message Protocol (ICMP) is a protocol that devices within a network use to communicate problems with data transmission. In this ICMP definition, one of the primary ways in which ICMP is used is to determine if data is getting to its destination and at the right time.
2. Ping works by sending an Internet Control Message Protocol (ICMP) Echo Request to a specified interface on the network and waiting for a reply.