# Linux Basic Commands

## Notes on rights

400 read by owner 040 read by group 004 read by anybody 200 write by owner 020 write by group 002 write by anybody 100 execute by owner 010 execute by group 001 execute by anybody

## Search for text in files recursively

The following grep will return each filename containing the given text :

`grep -Ril "text"`

* `-R` Read all files under each directory, recursively.
* `-i` case-insensitive search
* `-l` Print the file name for each match. This is the default when there is more than one file to search.

## Search for Email Adresses Recursively

The following grep will match all email adresses @gmail.com :

`grep -RIHEo "\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,6}\b" . | grep gmail.com`

* `-R` Read all files under each directory, recursively.
* `-I` Process a binary file as if it did not contain matching data.
* `-H` Print the file name for each match. This is the default when there is more than one file to search.
* `-E` Interpret PATTERN as an extended regular expression.
* `-o` Print only the matched (non-empty) parts of a matching line, with each such part on a separate output line.

To generate list file from addresses found :

`grep -RhIEo "\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,6}\b" . | grep gmail.com | sort -u > mails.txt`

* `-h` disables filename printing
* `sort -u` removes duplicates

## List Open Files For Process

`lsof -p PID`

* PID = Process ID

## Display files and folders sorted by size

### Ascending

`du -hs ~/web/* | sort -h`

### Descending

`du -hs ~/web/* | sort -hr`

## Network setup

### Disable NetworkManager

```shell
systemctl disable NeworkManager
```

### Set static IP with NetworkManagercomputer :: Documents/projects/PentestFTW »

#### Set IP

```shell
nmcli con mod My\ Connection ipv4.addresses 10.1.1.250/24
```

#### Set Gateway

```shell
nmcli con mod My\ Connection ipv4.gateway 10.1.1.1
```

#### Set DNS

```shell
nmcli con mod My\ Connection ipv4.dns 8.8.8.8     
```

#### Set connection up

```shell
nmcli con up My\ Connection
```

### Connect to wifi with wpa\_supplicant

#### Scan available ESSID

```shell
sudo iwlist wlo1 scan | grep ESSID | sort -u
```

#### Generate config for ESSID

```shell
wpa_passphrase mySSID myPass | sudo tee -a /etc/wpa_supplicant/wpa_supplicant.conf
```

#### Connect to wifi

```shell
sudo wpa_supplicant -c /etc/wpa_supplicant/wpa_supplicant.conf -i wlo1 -B
```

#### Obtain IP address via DHCP

```shell
sudo dhclient -v wlo1
```

### Set static IP

#### Assign IP address to interface

```shell
sudo ip addr add 192.168.112.45/24 dev myNetworkInterface
```

#### Add default route

```shell
 sudo ip route add default via myGateway dev myNetworkInterface
```

### Extract IP adresses from file

```shell
grep -E -o "([0-9]{1,3}[\.]){3}[0-9]{1,3}"
```
