---
tags: interview
---
# Linux Command for manager
> Freebsd, Ubuntu
:::spoiler
| Options | Meaning |
| -------- | -------- |
| | |
| | |
| | |
| | |
:::
[TOC]
## `awk`
```bash=
awk `{ print $2 }`
awk -va=st `{ print $2a }`
```
| Options | Meaning |
| -------- | -------- |
| -v | variable |
| -F | field-separator |
## `cut`
```shell=
cut -d ' ' -f 2-
```
| Options | Meaning |
| -------- | -------- |
| -d | delimiter |
| -f | fields |
## `grep`
```bash=
# grep <keywd> [filename]
grep -e "^[dl]" myfile #start with 'd' or 'l'
grep -e "^dl" myfile #start with 'dl'
```
| Options | Meaning |
| -------- | -------- |
| -e | regex |
| -i | ignore-case |
| -v | not matching |
## System
### `chsh`
- change sh to **bash**
```shell=
chsh -s /usr/local/bin/bash
/usr/local/bin/bash
```
> ctl-x & ctl-v: check bash version
### `ntpdate`
- 校正時間
```shell=
ntpdate ntp-server
```
> Gov: time.stdtime.gov.tw
> Ncnu: ntp.ncnu.edu.tw
## Process
### `ps`
```shell=
ps ax | grep <keyword>
```
### `kill`
```shell=
kill -l
```
### `rndc`
- Reload
```shell=
rndc reload
```
## Job
### `Crontab`
## Edit file
### `sed`
- **stream editor**
| Options | Meaning |
| -------- | -------- |
| -i | 直接編輯 |
| -e | 暫時編輯,配合` file1 > file2` 另存新檔 |
| -n | 默認 |
| -f | 讀檔、執行檔案中的sed指令 |
- **Print** line 1-2
```shell=
sed -n '1,2p' filename
```
- **Append** 'Phoebe is a lazy girl' **after line 3**
```shell=
sed -i '3a Phoebe is a lazy girl' filename
```
- Delete
- **Delete** line 4-5
```shell=
sed -i '4,5d' filename
```
- **Delete** lines which have the word 'hello'
```shell=
sed -i '/hello/d' filename
```
- Change
- **Change** 'is' to 'are' **in every lines first 'is'**
```shell=
sed -i 's/is/are/1' filename
```
- **Change line 3-5** to 'Phoebe is a lazy girl'
```shell=
sed -i '3,5c Phoebe is a lazy girl' filename
```
## Log file
> Path: `/var/log/message`
### `tail`
- **Keep showing the changes of log file**
```shell=
tail -f /var/log/message
```
- Show the last 2 lines of the file
```shell=
tail -n 2 filename
```
- Show line 2-5
```shell=
head -n 5 filename | tail -n 3
```
### Daemon's log
- Keep printing the log messages of specific daemon.
```shell=
sudo named -u bind -g
```
## Info
### `stat`
- check file status
```bash=
stat filename
```
```
File: 'exer3a.c'
Size: 302 Blocks: 8 IO Block: 4096 regular file
Device: 806h/2054d Inode: 4070203 Links: 1
Access: (0777/-rwxrwxrwx) Uid: (40870/s107321014) Gid: ( 321/ csie)
Access: 2021-10-28 21:31:48.734919191 +0800
Modify: 2021-10-28 21:31:48.734919191 +0800
Change: 2021-10-28 21:31:48.734919191 +0800
Birth: -
```
- Different between change/access/modify
- change: change name
- access: read only, copy
- modify: write
### `find`
- The find utility **recursively descends the directory hierarchy** for each path seeking files that match **a Boolean expression** written in the primaries given below.
| Options | Meaning | Usage |
|:-------:|:-------------------:|:-----:|
| -ls | Associated statistics. | |
| -atime | Accessed n days ago. | -atime n |
| -ctime | Changed n days ago. | -ctime n |
| -mtime | Modified n days ago. | -mtime n |
| -type | Is of the specified type. | -type type |
| -fstype | File belongs is of type. | -fstype type| |
| -name | Pattern matches the current file name. | -name pattern |
| -iname | Case insensitive. | |
| -lname | The contents of the symbolic link are matched instead of the file name. | |
| -user | Belongs to the user. | -user username|
| -size | n blocks long(512 bytes per block) | -size n |
| -perm | Permission flags exactly match the octal number. | -perm XXX |
| -path | Pathname being examined matches pattern. | -path pattern |
| -regex | Whole path of the file matches pattern using regular expression. | -regex pattern |
| -newer | Modified more recently than the file. | -newer filename |
| -local | Non-local directories | |
| -depth | Entries in a directory are acted on before the directory itself. | |
| -maxdepth | Descend at most n directory levels. | -maxdepth n |
| -mindepth | Do not apply any tests or actions at levels less than n. | -mindepth n |
| -prune | Do not descend into the current file. | |
| --empty | Empty file. ||
| -delete | Delete the finded files. ||
| -quit | `find` terminate immediately.||
| -print | Print the pathname of the current file to standard output. ||
- Expression
```
( expression )
! expression
expression -or expression
expression -and expression
```
- A backslash(`\`) is used as an escape character within the pattern.
> Permission bit:
> 
### Disk usage
> 單位: kb
```shell=
df -h
```
- `du`
| Options | Meaning |
| -------- | -------- |
| -A | Display the apparent size instead of the disk usage. |
| -I | Ignore files and directories matching the specified mask. |
| -L | Symbolic links on the command line and in file hierarchies are followed. |
| -a | Display an entry for each file in a file hierarchy. |
| -c | Display a grand total. |
| -d | Directories deep. |
| -h | Human-readable output |
| -t | Display only entries for which size exceeds threshold. |
```shell=
du -h filename
du -h / | sort -r | head -10
```
### `top`
## Network
### `ping`
> Default ICMP packet size: **56 + 8** bytes
| Options | Meaning |
| -------- | -------- |
| -c | Package count |
| -s | Specifies the number of data bytes to be sent. |
| -v | Verbose |
```shell=
ping -c 4 hostIP
ping -vc 3 8.8.8.8
```
### `ifconfig`
- Set network interface
```bash=
ifconfig
ifconfig em0 10.21.XX.XX netmask 255.255.0.0
ifconfig em0 down
ifconfig em0 up
```
### `route`
```bash=
route add default [IPaddr]
route add -net [IPaddrRange] [IPaddr]
route change
route delete
route show [IPaddr]
route flush
```
### `netstat`
- show all network status
```shell=
netstat -na | grep XXX
```
- Show routing table
```bash=
netstat -rn
```
- Show the state of network interfaces
```bash=
netstat -i
netstat -L [interface_name]
```
- Show tcp
```bash=
netstat -p tcp
```
### `sockstat`
- list listening sockets
```bash=
sockstat -l
```
- list connected sockets
```shell=
sockstat -c
```
> -p: port
> -P: Protocol
### `host`
> Install `bind-tools` -- Command line tools from BIND: delv, dig, host, nslookup.
- **DNS lookup**
> By default, host will wait for 5 seconds for UDP responses and 10 seconds for TCP connections.
| Options | Meaning |
| -------- | -------- |
| -v | Verbose |
| -l | List zone |
| -W | Wait for up to seconds for a reply. |
```shell=
host -v google.com
```
### `dig`
> Install `bind-tools` -- Command line tools from BIND: delv, dig, host, nslookup.
- **DNS lookup**
| Options | Meaning |
| -------- | -------- |
| -b | Set the source IP address of the query. |
| -x | Reverse lookup |
```shell=
dig hostIP @NameServer
```