CS-Notes/notes/Linux.md
2018-02-22 14:47:22 +08:00

1072 lines
24 KiB
Markdown
Raw Blame History

<!-- GFM-TOC -->
* [???¨°??????????](#???¨°??????????)
* [????](#????)
* [???](#???)
* [??????](#??????)
* [?????](#?????)
* [PATH](#path)
* [???§Ö??](#???§Ö??)
* [sudo](#sudo)
* [GNU](#gnu)
* [????????](#????????)
* [???????§Ñ·Ú](#???????§Ñ·Ú)
* [????](#????)
* [??????????](#??????????)
* [??????](#??????)
* [1. MBR](#1-mbr)
* [2. GPT](#2-gpt)
* [??????????](#??????????)
* [1. BIOS](#1-bios)
* [2. UEFI](#2-uefi)
* [????](#????)
* [??????????????](#??????????????)
* [?????????](#?????????)
* [?????????????????](#?????????????????)
* [1. ?????????????](#1-?????????????)
* [2. ???????????](#2-???????????)
* [3. ??????](#3-??????)
* [???????](#???????)
* [?????????](#?????????)
* [??????](#??????)
* [???????](#???????)
* [??????](#??????)
* [????????????????](#????????????????)
* [1. ls](#1-ls)
* [2. cp](#2-cp)
* [3. rm](#3-rm)
* [4. mv](#4-mv)
* [??????????](#??????????)
* [1. cat](#1-cat)
* [2. tac](#2-tac)
* [3. more](#3-more)
* [4. less](#4-less)
* [5. head](#5-head)
* [6. tail](#6-tail)
* [7. od](#7-od)
* [8. touch](#8-touch)
* [????????????](#????????????)
* [1. which](#1-which)
* [2. whereis](#2-whereis)
* [3. locate](#3-locate)
* [4. find](#4-find)
* [4.1 ??????§Û?????](#41-??????§Û?????)
* [4.2 ???????????????????§Û?????](#42-???????????????????§Û?????)
* [4.3 ??????????????§Û?????](#43-??????????????§Û?????)
* [???????????](#???????????)
* [??????????](#??????????)
* [inode](#inode)
* [???? inode ?? block](#????-inode-??-block)
* [????????????????](#????????????????)
* [1. ???????](#1-???????)
* [2. ????????](#2-????????)
* [???????](#???????)
* [???](#???)
* [1. gzip](#1-gzip)
* [2. bzip2](#2-bzip2)
* [3. xz](#3-xz)
* [???](#???)
* [BASH](#bash)
* [Bash????](#bash????)
* [????????](#????????)
* [??????????](#??????????)
* [???????????](#???????????)
* [???????](#???????)
* [1. ??????cut](#1-??????cut)
* [2. ????????sort??uniq](#2-????????sort??uniq)
* [3. ???????????tee](#3-???????????tee)
* [4. ?????????tr??col??expand??join??paste](#4-?????????tr??col??expand??join??paste)
* [5. ???????split](#5-???????split)
* [??????????????????????](#??????????????????????)
* [grep](#grep)
* [printf](#printf)
* [awk](#awk)
* [vim ??????](#vim-??????)
* [?¦Ï?????](#?¦Ï?????)
<!-- GFM-TOC -->
# ???¨°??????????
## ????
**1. --help**
????????¡Â??????????
**2. man**
man ?? manual ????§Õ???????????????????????
????? man date ????? DATE(1) ????????§Ö??????????????????????????????????????
| ???? | ???? |
| -- | -- |
| 1 | ????? shell ?????§á????????????????????? |
| 5 | ??????? |
| 8 | ????????????????????? |
**3. info**
info ?? man ????????? info ???????????????—¨?????????????????
## ???
**1. ???????§Õ????? sync**
?????????????????§Õ????¦Ë??????§Ö?????????????????????????????????????????? sync ?????????
**2. shutdown**
```html
# /sbin/shutdown [-krhc] [???] [??????]
-k ?? ???????????????????????????????????
-r ?? ???????????????????????
-h ?? ??????????????????????
-c ?? ??????????§Ö? shutdown ???????
```
**3. ??????????**
reboot??halt??poweroff??
## ??????
ps ??þŸ
????? theadx ??????????
```html
ps aux | grep threadx
```
## ?????
netstat ??þŸ
???????? 80 ???????
```html
netstat -anp | grep 80
```
## PATH
????????????? PATH ????????????????¡¤????¡¤??????? : ?????
```html
/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/home/dmtsai/.local/bin:/home/dmtsai/bin
```
## ???§Ö??
0???????
1????????????????????root????
2??????????????????
3??????????????????????????????????????????
4????????¦Ä???
5???????????? X-windows ??????????????—¥
6????????????????????
## sudo
??? sudo ????????????? root ????§Ö????þŸ????????? /etc/sudoers ?§Ó?????????þŸ
## GNU
GNU ????????????¨±?????????????????????????????????????? GNU???????????????? GPL ????????????? GPL ???? GNU ??¨´??????§¿?ï…???????????????
- ???¦Ê???????§Õ??????????
- ???????????
- ?????????????????????????¨À?
## ????????
RPM ?? DPKG ???????????????????????RPM ???? Redhat Package Manager???????? Red Hat ????????????? GNU ???????????????????? Linux ?? (RHEL) ?????????????? RPM ???§à?????????? Debian ?????? (UBUNTU) ?? DEB ???????????? DPKG?????? Debian Package??????????? RPM ?????
YUM ???? RPM ?????????????????????????????????????????????????? RPM ???????????????????????????????????????????????????????????????????????????????????????????????????YUM ???????????????????????????????????
## ???????§Ñ·Ú
Linux ???§Ñ??????????? Linux ????????????????
**???? DPKG**
??????§Ñ?
- Ubuntu
???????§Ñ?
- Debian
**???? RPM**
??????§Ñ?
- Red Hat
???????§Ñ?
- Fedora
- CentOS
# ????
## ??????????
Linux ????????????????????????
????????????????
- SCSI/SATA/USB ?????/dev/sd[a-p]
- IDE ?????/dev/hd[a-d]
??????????????????????????????????§Û??????????????????¦Ë??????
## ??????
?????????????????????????????????? MBR ???????????????????????? GPT ??????
### 1. MBR
MBR ?§µ??????????????????????§µ?????????????Master boot record, MBR??????????partition table???????? MBR ? 446 bytes??partition table ? 64 bytes??
????????? 64 bytes????????›¥ 4 ?????????? 4 ???????????????Primary?????????????Extended?????????????????????????????????????????????????????????????????????????????????????????????§»????????????????
Linux ???????????????????????????????????????????+???????? /dev/sda1????????????????? 5 ?????
### 2. GPT
?????????§Ó??????????§³?????? 512bytes ?????????? 4k??GPT ?????????§Õ?????????????????????????????Logical Block Address, LBA????
GPT ?? 1 ?????????? MBR?????????? 33 ????????????????????????? 33 ????????????????????§Ò????
GPT ????????????????????????????????? 128 ????????
![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/a5c25452-6fa5-49e7-9322-823077442775.jpg)
## ??????????
### 1. BIOS
BIOS ????????????????§Ö??????????????????????????????????????????????????? MBR???? MBR ??????§Ö?????????????????????????????????????????????
MBR ?§Ö????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????? MBR ?§Ö???????????????????????????????????????????????????????????????????????????????
![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/f900f266-a323-42b2-bc43-218fdb8811a8.jpg)
???????????????????? Windows ???? Linux???????? Windows ?????? MBR???? Linux ???????????????????? MBR ???????????????????????????????????????????????????
### 2. UEFI
UEFI ????? BIOS ???????????—¨?????????
## ????
?????????????????????????????????????????????????????????
![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/249f3bb1-feee-4805-a259-a72699d638ca.jpg)
# ??????????????
## ?????????
?????????????????????????????????????????????§Ó???????????
??? ls ????????????????????????????????? drwxr-xr-x. 3 root root 17 May 6 00:14 .config????????????????????
- drwxr-xr-x?????????????????? 1 ¦Ë??????????¦²??? 9 ¦Ë?????????¦±?
- 3??????????
- root???????????
- root????????ï”
- 17???????§³??
- May 6 00:14???????????????
- .config?????????
???????????????¾©???§µ?
- d??????
- -???????
- l???????????
9 ¦Ë????????????§µ?? 3 ?????ï…?? 3 ?ï…??????????????????????????????????????????????????§Ö? 3 ¦Ë???? r??w??x ??????????????§Õ??????§³?
## ?????????????????
### 1. ?????????????
```html
# chgrp [-R] groupname dirname/filename
-R????????
```
### 2. ???????????
?????????????????????????????????????î•
```html
# chown [-R] ?????:????? dirname/filename
```
### 3. ??????
?????????????????????????????????? 3 ??¦Ë???????????????¦Ë???????????¦Ë????? 4??2??1????????????????????? r??4??w??2??x??1??
```html
# chmod [-R] xyz dirname/filename
```
???????? .bashrc ???????????? -rwxr-xr--??
```html
# chmod 754 .bashrc
```
???????¡Â??????Ú…????
```html
# chmod [ugoa] [+-=] [rwx] dirname/filename
- u???????
- g?????????
- o????????
- a????????
- +????????
- -????????
- =???Ú…???
```
??????? .bashrc ???????????????§Õ????
```html
# chmod a+w .bashrc
```
## ???????
?????????›¥???????????????§µ?????›¥????????????????§³????????????? w ??????????????????????
???›¥????§Ò???????????????????????§Ò?????????????? r ???????????????§Ò?w ????????????????§Ò????????????????????????????????????????x ??????????????????????x ????? r ?? w ??????????????????????????????????????????????§Ò??????????§Ò??????????
## ?????????
??????????????????§á???????????? 666 ??????? -rw-rw-rw- ??
?????????????????????????????????§á???????????? 777 ??????? drwxrwxrwx??
??????? umask ???????????????????????????????????????????? 002 ??????????????????????? 2 ???????????§Õ???????????????????????? -rw-rw-r-- ??
## ??????
??????? Linux ???§Ñ·Ú????????????????Filesystem Hierarchy Standard (FHS) ?ÕÇ?? Linux ??????????????????????????
- / (root, ????)
- /usr (unix software resource)????????????????????????????
- /var (variable)???????????????§Û????§Ö??????????
???????????????
![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/27ace615-558f-4dfb-8ad4-7ac769c10118.jpg)
# ???????
## ??????
1. modification time (mtime)????????????????????
2. status time (ctime)??????????????????????????????
3. access time (atime)????????????????
## ????????????????
### 1. ls
?§Ô???????????????????????????????§Ñ??????????
```html
# ls [-aAdfFhilnrRSt] file|dir
-a ???§Ô?????????
-d ?????§Ô???????
-l ?????????????§Ô???????????????????????????
```
### 2. cp
?????????
?????????????????????????????????????§³?
```html
cp [-adfilprsu] source destination
-a ?????? -dr --preserve=all ??????????? dr ??¦Ï????????
-d ?????????????????????????????????????????????
-i ?????????????????????????????????
-p ?????????????????????
-r ????????????
-u ??destination ?? source ?????? destination???? destination ????????????????
--preserve=all ?????? -p ??????????????????? SELinux ??????, links, xattr ?????????
```
### 3. rm
?????????
```html
# rm [-fir] ???????
-r ????????
```
### 4. mv
?????????
```html
# mv [-fiu] source destination
# mv [options] source1 source2 source3 .... directory
-f ?? force ????????????????????????????????????????
```
## ??????????
### 1. cat
???????????
```html
# cat [-AbEnTv] filename
-n ????????§Ü?? ??????????????§Ü???? -b ??????
```
### 2. tac
?? cat ?????????????????§á???????
### 3. more
????????????????????????????????
### 4. less
?? more ?????
### 5. head
??????????????§³?
```html
# head [-n number] filename
-n ??????????????????????§Ö????
```
### 6. tail
?? head ????????????????????§³?
### 7. od
??????????????????????????????????????
### 8. touch
??????????????????????
```html
# touch [-acdmt] filename
-a ?? ???? atime
-c ?? ???? ctime?????????????????????????
-m ?? ???? mtime
-d ?? ????????????????????????????????????????? --date="????????"
-t ????????????????????????????????????[YYYYMMDDhhmm]
```
## ????????????
### 1. which
?????????
```html
# which [-a] command
-a ????????????§Ô???????????§Ö????
```
### 2. whereis
whereis ??????????????????????????????????????
```html
# whereis [-bmsu] dirname/filename
```
### 3. locate
locate ?????¨´????????????????????????
locate ??? /var/lib/mlocate/ ???????????????????????›¥??????§µ?????????????¦²?????????? locate ????????????????????? updatedb ???????????????
```html
# locate [-ir] keyword
-r???????????
```
### 4. find
find ?????????????????????????????
```html
# find filename [option]
```
#### 4.1 ??????§Û?????
```html
-mtime n ???§Ô??? n ??????????????????????
-mtime +n ???§Ô??? n ????(???? n ????)????????????
-mtime -n ???§Ô??? n ?????(?? n ????)????????????
-newer file ?? ?§Ô??? file ????????
```
+4??4 ?? -4 ?????????¦¶?????
![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/658fc5e7-79c0-4247-9445-d69bf194c539.png)
#### 4.2 ???????????????????§Û?????
```html
-uid n
-gid n
-user name
-group name
-nouser ???????????????? /etc/passwd ?????
-nogroup????????????öö?????? /etc/group ?????
```
#### 4.3 ??????????????§Û?????
```html
-name filename
-size [+-]SIZE??????? SIZE ?????(+)??§³(-)?????????? SIZE ?????§µ?c: ???? byte??k: ???? 1024bytes??????????? 50KB??????????????? -size +50k
-type TYPE
-perm mode ???????????? mode ?????
-perm -mode ???????????? mode ?????
-perm /mode ??????????????? mode ?????
```
# ???????????
## ??????????
????????§Ú????????????????????????????????????????????????????????????????§Ö???????????????????????????????????????????????????????????????????
???????????????????
1. superblock????????????????????????? inode ?? block ?????????????????????????????????????????????
2. inode?????????????? inode???????????????????????????????????? block ????
3. block????????????????????????????????? block??
????????????????????????? inode ??????????????????????? block?????????? block ?????????????
????????????????????????? block ????????
Ext2 ???????????????????????????????????? block ????????????????????????????? block ??ï…???????
![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/1974a836-aa6b-4fb8-bce1-6eb11969284a.jpg)
## inode
Ext2 ????????? block ??§³?? 1k??2k ?? 4k ?????????? block ??§³??????????????§³??????? inode ??§³????? 128 bytes??
inode ?§Þ???????????????? block????????? block ???§³?????????????????????? block??????? inode ??§³???????????????????? block?????????????????????????????????? block ???????§³????????? block ?????????????? block??
![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/89091427-7b2b-4923-aff6-44681319a8aa.jpg)
inode ????????????????
- ???????????(read/write/excute)??
- ?????????????????(owner/group)??
- ?????????????
- ????????????????????(ctime)??
- ?????¦Å??????(atime)??
- ??????????(mtime)??
- ???????????????(flag)???? SetUID...??
- ????????????????? (pointer)??
## ???? inode ?? block
???????????????????? inode ????????? block??block ???????????????????????? inode ????????????????????????? inode ???????????????????????????§µ????????????????????????????????§»?????????? w ????§Û??
## ????????????????
```html
# ln [-sf] source_filename dist_filename
-s ??????? hard link???? -s ? symbolic link
-f ????????5?????????????????????
```
### 1. ???????
hard link ?????????????????????????????????????????????? inode ?????????????????????????????????????????? 0??
????????????????? Filesystem??????????????????
```html
# ln /etc/crontab .
# ll -i /etc/crontab crontab
34474855 -rw-r--r--. 2 root root 451 Jun 10 2014 crontab
34474855 -rw-r--r--. 2 root root 451 Jun 10 2014 /etc/crontab
```
### 2. ????????
symbolic link ???????? Windows ????????????????????????????????????????????????????????????????????????????????????????
symbolic link ????????????????
```html
# ll -i /etc/crontab /root/crontab2
34474855 -rw-r--r--. 2 root root 451 Jun 10 2014 /etc/crontab
53745909 lrwxrwxrwx. 1 root root 12 Jun 23 22:31 /root/crontab2 -> /etc/crontab
```
# ???????
## ???
Linux ?????§Ü????????????????????????????
| ????? | ??????? |
| -- | -- |
| \*.Z | compress |
|\*.zip | zip |
|\*.gz | gzip|
|\*.bz2 | bzip2 |
|\*.xz | xz |
|\*.tar | tar ???????????????§à?????? |
|\*.tar.gz | tar ????????????????? gzip ????? |
|\*.tar.bz2 | tar ????????????????? bzip2 ????? |
|\*.tar.xz | tar ????????????????? xz ????? |
### 1. gzip
gzip ?? Linux ?????????????????? compress??zip ?? gzip ????????????
???? gzip ?????????????????????
?? 9 ???????????????????¨¢?
??????? zcat??zmore??zless ??????????????????
```html
$ gzip [-cdtv#] filename
-c ???????????????????????
-d ???????
-t ??????????????????
-v ??????????????
-# ?? # ???????????????????????????????????????????6
```
### 2. bzip2
???? gzip ???????????
??????bzcat??bzmore??bzless??bzgrep??
```html
$ bzip2 [-cdkzv#] filename
-k ??????????
```
### 3. xz
???? bzip2 ???????????
?????????gzip??bzip2??xz ??????????????????????????????????????????????
??????xzcat??xzmore??xzless??xzgrep??
```html
$ xz [-dtlkc#] filename
```
## ???
????????????????????????????????????????????????????????tar ???????????????????????? gip??bzip2??xz ?????????????????
```html
$ tar [-z|-j|-J] [cv] [-f ?????tar???] filename... ==??????
$ tar [-z|-j|-J] [tv] [-f ???§Ö?tar???] ==??
$ tar [-z|-j|-J] [xv] [-f ???§Ö?tar???] [-C ??] ==?????
-z ?????zip??
-j ?????bzip2??
-J ?????xz??
-c ?????????????
-t ??????????????????§»?????
-x ??????????????????
-v ???????/???????????§µ???????????????????
-f : filename?????????????
-C ?? ?? ??????????????
```
????????????
?????? ?? tar -jcv -f filename.tar.bz2 ???????????????????
?? ??&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;?? tar -jtv -f filename.tar.bz2
?????&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;??tar -jxv -f filename.tar.bz2 -C ??????????
# BASH
??????? shell ?????????????Bash ???? shell ??????
## Bash????
**1. ???????**
?????¨´??????þŸ???¦Å??????§Ö??????????????????§µ? \~/.bash_history ????§Þ?????????¦Å??????§Û??????þŸ
**2. ????????????**
??????tab
**3. ????????**
???? lm ?? ls -al ???????
**4. shell scripts**
**5. ????**
???? ls -l /usr/bin/X\* ?§Ô? /usr/bin ?????????? X ??????????
## ????????
?????????????????? = ??????????????????????? \$ ????????? \${} ????????????????? echo ???þŸ
```bash
$ var=abc
$ echo $var
$ echo ${var}
```
????????????§á????????????????????????????????????????????????????????var="lang is \$LANG"????var???? lang is zh_TW.UTF-8??????????????????????????????????????? var='lang is \$LANG'???? var ???? lang is \$LANG??
??????? \`???\` ???? \$(???) ????????????§ß????????????????? version=\$(uname -r)???? version ???? 3.10.0-229.el7.x86_64??
??????? export ???????????????????????????????????????????????????¦Í???????????? Bash ?????????? Bash??
Bash ?????????????????????????????????????????§Ú?????????????????????????????????????????????????? declare ????
```html
$ declare [-aixr] variable
-a ?? ?????????????
-i ?? ?????????????
-x ?? ?????????????
-r ?? ?????readonly????
```
??? [ ] ??????????§Ó?????
```bash
$ array[1]=a
$ array[2]=b
$ echo ${array[1]}
```
## ??????????
1. ?????????¡¤?????????????? /bin/ls ???? ./ls ??
2. ?????????????????§µ?
3. ?? Bash ????????????§µ?
4. ?? \$PATH ?????????????¡¤??????????????????????§³?
## ???????????
???????????????????????????????????????????
1. ??????? (stdin) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;??????? 0 ????? < ?? << ??
2. ?????? (stdout)&nbsp;&nbsp;&nbsp;&nbsp;??????? 1 ????? > ?? >> ??
3. ??????????(stderr)??????? 2 ????? 2> ?? 2>> ??
???§µ?????????????????????????????????????????????????????
??????????????????????????????????? /dev/null ???????????????
??????????????????????????????????????????????????????????????????????? 2>&1 ???????????????????????????
```bash
$ find /home -name .bashrc > list 2>&1
```
## ???????
??????????????????????????????????????????????????????????????????????????????????????????¨´???????????????? | ??????????????þŸ
```bash
$ ls -al /etc | less
```
### 1. ??????cut
????????????§Ö???§³?
cut ??????????§Ù??????????????
```html
$ cut
-d ???????
-f ?????? -d ???????? -f n ????? n ??????
-c ??????????¦Ë???????
```
????1??last ????????????????????????????????
```html
$ last
root pts/1 192.168.201.101 Sat Feb 7 12:35 still logged in
root pts/1 192.168.201.101 Fri Feb 6 12:13 - 18:46 (06:33)
root pts/1 192.168.201.254 Thu Feb 5 22:37 - 23:53 (01:16)
$ last | cut -d ' ' -f 1
```
????2???? export ????????????? 12 ??????????????????
```html
$ export
declare -x HISTCONTROL="ignoredups"
declare -x HISTSIZE="1000"
declare -x HOME="/home/dmtsai"
declare -x HOSTNAME="study.centos.vbird"
.....(???????).....
$ export | cut -c 12
```
### 2. ????????sort??uniq
**sort** ????????
```html
$ sort [-fbMnrtuk] [file or stdin]
-f ???????§³§Õ
-b ??????????????
-M ?????¡¤???????????????? JAN, DEC
-n ?????????
-r ??????????
-u ?????? unique ?????????????????
-t ?????????????tab
-k ??????????????
```
??????/etc/passwd ???????? : ?????????????????????
```html
$ cat /etc/passwd | sort -t ':' -k 3
root:x:0:0:root:/root:/bin/bash
dmtsai:x:1000:1000:dmtsai:/home/dmtsai:/bin/bash
alex:x:1001:1002::/home/alex:/bin/bash
arod:x:1002:1003::/home/arod:/bin/bash
```
**uniq** ?????????????????????
```html
$ uniq [-ic]
-i ???????§³§Õ
-c ?????§Þ???
```
??????????????????????
```html
$ last | cut -d ' ' -f 1 | sort | uniq -c
1
6 (unknown
47 dmtsai
4 reboot
7 root
1 wtmp
```
### 3. ???????????tee
?????????????????????????§µ??? **tee** ??????????????????????????????????????????????? tee ?????????????????????????????
```html
$ tee [-a] file
```
### 4. ?????????tr??col??expand??join??paste
**tr** ???????????§Ö???????????????????<3F>I??
```html
$ tr [-ds] SET1 ...
-d ?? ??????? SET1 ????????
```
???????? last ????????????§³§Õ??????§Õ??
```html
$ last | tr '[a-z]' '[A-Z]'
```
**col** ?? tab ?????????????
```html
$ col [-xb]
-x ?? ?? tab ?????????????
```
**expand** ?? tab ??????????????????? 8 ????
```html
$ expand [-t] file
-t ??tab ??????????
```
**join** ????????????????§Ü???????
```html
$ join [-ti12] file1 file2
-t ????????????????
-i ???????§³§Õ?????
-1 ????????????????????
-2 ????????????????????
```
**paste** ????????????????
```html
$ paste [-d] file1 file2
-d ????????????? tab
```
### 5. ???????split
**split** ????????????????????
```html
$ split [-bl] file PREFIX
-b ?????§³?????§Ù?????????¦Ë?????? b, k, m ??
-l ?????????????§Ù?????
- PREFIX ??????????????????
```
# ??????????????????????
## grep
???????????????????????????
```html
$ grep [-acinv] [--color=auto] ???????? filename
-a ?? ?? binary ????? text ??????????????
-c ?? ???????????
-i ?? ?????§³§Õ
-n ?? ????§Ü?
-v ?? ????????????????? ???????? ??????????
--color=auto ??????????????????
```
??????????? the ??????????????????????????? --color=auto ???????????????? Linux ?????????? the ???????
```html
$ grep -n 'the' regular_express.txt
8:I can't finish the test.
12:the symbol '*' is represented as start.
15:You are the best is mean you are the no. 1.
16:The world Happy is the same with "glad".
18:google is the best tools for search keyword
```
??? { ?? } ??????? shell ???????????????????????????????????????‰^
```html
$ grep -n 'go\{2,5\}g' regular_express.txt
```
?????????????¦Ï? [???????](https://github.com/00000H/notes/blob/master/notes/%E6%AD%A3%E5%88%99%E8%A1%A8%E8%BE%BE%E5%BC%8F.md).
## printf
?????????????
????????????????? printf ????????????? $( ) ?????
```html
$ printf '%10s %5i %5i %5i %8.2f \n' $(cat printf.txt)
DmTsai 80 60 92 77.33
VBird 75 55 80 70.00
Ken 60 90 70 73.33
```
## awk
```html
$ awk '???????? 1{???? 1} ???????? 2{???? 2} ...' filename
```
awk ??¦Ä?????§µ????????§³??¦Ë????¦²??????¦Å???????????\$n??n ???¦Ê???? 1 ?????\$0 ???????§³?
???? 1???????????????????? ip
```html
$ last -n 5
dmtsai pts/0 192.168.1.100 Tue Jul 14 17:32 still logged in
dmtsai pts/0 192.168.1.100 Thu Jul 9 23:36 - 02:58 (03:22)
dmtsai pts/0 192.168.1.100 Thu Jul 9 17:23 - 23:36 (06:12)
dmtsai pts/0 192.168.1.100 Thu Jul 9 08:02 - 08:17 (00:14)
dmtsai tty1 Fri May 29 11:55 - 12:11 (00:15)
$ last -n 5 | awk '{print $1 "\t" $3}
```
awk ??????
| ???????? | ???????? |
| -- | -- |
| NF | ??????§Ö???????? |
| NR | ??????????????????? |
| FS | ??????????????????? |
???? 2??????????????§Ü????????????§Ø??????
```html
$ last -n 5 | awk '{print $1 "\t lines: " NR "\t columns: " NF}'
dmtsai lines: 1 columns: 10
dmtsai lines: 2 columns: 10
dmtsai lines: 3 columns: 10
dmtsai lines: 4 columns: 10
dmtsai lines: 5 columns: 9
```
?????????????????????§Ö?????? ==??
???? 3??/etc/passwd ????????????? UID???? UID §³?? 10 ????????§Õ???
```text
cat /etc/passwd | awk 'BEGIN {FS=":"} $3 < 10 {print $1 "\t " $3}'
root 0
bin 1
daemon 2
```
# vim ??????
![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/341c632a-1fc1-4068-9b9f-bf7ef68ebb4c.jpg)
????????????????????????????????ݴ?????
| ???? | ???? |
| -- | -- |
| :w | §Õ?????|
| :w! | ???????????????§Õ???????????????§Õ???????????????????§Û? |
| :q | ??|
| :q! | ???????????|
| :wq | §Õ????????|
| :wq!| ???§Õ????????|
# ?¦Ï?????
- ???. ?? ?? ?? Linux ? ?? ?? ?? ?? ? ?? ?? ??[J]. 2009.
- [Linux ??????????????](https://www.ibm.com/developerworks/cn/linux/l-cn-rpmdpkg/index.html)