24 KiB
- ???ò??????????
- ????
- ??????????????
- ???????
- ???????????
- ???????
- BASH
- ??????????????????????
- vim ??????
- ?ο?????
???ò??????????
????
1. --help
????????÷??????????
2. man
man ?? manual ????д???????????????????????
????? man date ????? DATE(1) ????????е??????????????????????????????????????
| ???? | ???? |
|---|---|
| 1 | ????? shell ?????п????????????????????? |
| 5 | ??????? |
| 8 | ????????????????????? |
3. info
info ?? man ????????? info ???????????????棬?????????????????
???
1. ???????д????? sync
?????????????????д????λ??????е?????????????????????????????????????????? sync ?????????
2. shutdown
# /sbin/shutdown [-krhc] [???] [??????]
-k ?? ???????????????????????????????????
-r ?? ???????????????????????
-h ?? ??????????????????????
-c ?? ??????????е? shutdown ???????
3. ??????????
reboot??halt??poweroff??
??????
ps ??䶮
????? theadx ??????????
ps aux | grep threadx
?????
netstat ??䶮
???????? 80 ???????
netstat -anp | grep 80
PATH
????????????? PATH ????????????????·????·??????? : ?????
/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 ????????
??????????
1. BIOS
BIOS ????????????????е??????????????????????????????????????????????????? MBR???? MBR ??????е?????????????????????????????????????????????
MBR ?е????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????? MBR ?е???????????????????????????????????????????????????????????????????????????????
???????????????????? Windows ???? Linux???????? Windows ?????? MBR???? Linux ???????????????????? MBR ???????????????????????????????????????????????????
2. UEFI
UEFI ????? BIOS ???????????棬?????????
????
?????????????????????????????????????????????????????????
??????????????
?????????
?????????????????????????????????????????????в???????????
??? 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. ?????????????
# chgrp [-R] groupname dirname/filename
-R????????
2. ???????????
?????????????????????????????????????顣
# chown [-R] ?????:????? dirname/filename
3. ??????
?????????????????????????????????? 3 ??λ???????????????λ???????????λ????? 4??2??1????????????????????? r??4??w??2??x??1??
# chmod [-R] xyz dirname/filename
???????? .bashrc ???????????? -rwxr-xr--??
# chmod 754 .bashrc
???????÷??????趨????
# chmod [ugoa] [+-=] [rwx] dirname/filename
- u???????
- g?????????
- o????????
- a????????
- +????????
- -????????
- =???趨???
??????? .bashrc ???????????????д????
# 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)???????????????й????е??????????
???????????????
???????
??????
- modification time (mtime)????????????????????
- status time (ctime)??????????????????????????????
- access time (atime)????????????????
????????????????
1. ls
?г???????????????????????????????а??????????
# ls [-aAdfFhilnrRSt] file|dir
-a ???г?????????
-d ?????г???????
-l ?????????????г???????????????????????????
2. cp
?????????
?????????????????????????????????????С?
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
?????????
# rm [-fir] ???????
-r ????????
4. mv
?????????
# mv [-fiu] source destination
# mv [options] source1 source2 source3 .... directory
-f ?? force ????????????????????????????????????????
??????????
1. cat
???????????
# cat [-AbEnTv] filename
-n ????????к?? ??????????????к???? -b ??????
2. tac
?? cat ?????????????????п???????
3. more
????????????????????????????????
4. less
?? more ?????
5. head
??????????????С?
# head [-n number] filename
-n ??????????????????????е????
6. tail
?? head ????????????????????С?
7. od
??????????????????????????????????????
8. touch
??????????????????????
# touch [-acdmt] filename
-a ?? ???? atime
-c ?? ???? ctime?????????????????????????
-m ?? ???? mtime
-d ?? ????????????????????????????????????????? --date="????????"
-t ????????????????????????????????????[YYYYMMDDhhmm]
????????????
1. which
?????????
# which [-a] command
-a ????????????г???????????е????
2. whereis
whereis ??????????????????????????????????????
# whereis [-bmsu] dirname/filename
3. locate
locate ?????ù????????????????????????
locate ??? /var/lib/mlocate/ ???????????????????????洢??????У?????????????Σ?????????? locate ????????????????????? updatedb ???????????????
# locate [-ir] keyword
-r???????????
4. find
find ?????????????????????????????
# find filename [option]
4.1 ??????й?????
-mtime n ???г??? n ??????????????????????
-mtime +n ???г??? n ????(???? n ????)????????????
-mtime -n ???г??? n ?????(?? n ????)????????????
-newer file ?? ?г??? file ????????
+4??4 ?? -4 ?????????Χ?????
4.2 ???????????????????й?????
-uid n
-gid n
-user name
-group name
-nouser ???????????????? /etc/passwd ?????
-nogroup????????????鲻?????? /etc/group ?????
4.3 ??????????????й?????
-name filename
-size [+-]SIZE??????? SIZE ?????(+)??С(-)?????????? SIZE ?????У?c: ???? byte??k: ???? 1024bytes??????????? 50KB??????????????? -size +50k
-type TYPE
-perm mode ???????????? mode ?????
-perm -mode ???????????? mode ?????
-perm /mode ??????????????? mode ?????
???????????
??????????
????????и????????????????????????????????????????????????????????????????е???????????????????????????????????????????????????????????????????
???????????????????
- superblock????????????????????????? inode ?? block ?????????????????????????????????????????????
- inode?????????????? inode???????????????????????????????????? block ????
- block????????????????????????????????? block??
????????????????????????? inode ??????????????????????? block?????????? block ?????????????
????????????????????????? block ????????
Ext2 ???????????????????????????????????? block ????????????????????????????? block ??飬???????
inode
Ext2 ????????? block ??С?? 1k??2k ?? 4k ?????????? block ??С??????????????С??????? inode ??С????? 128 bytes??
inode ?м???????????????? block????????? block ???С?????????????????????? block??????? inode ??С???????????????????? block?????????????????????????????????? block ???????С????????? block ?????????????? block??
inode ????????????????
- ???????????(read/write/excute)??
- ?????????????????(owner/group)??
- ?????????????
- ????????????????????(ctime)??
- ?????ε??????(atime)??
- ??????????(mtime)??
- ???????????????(flag)???? SetUID...??
- ????????????????? (pointer)??
???? inode ?? block
???????????????????? inode ????????? block??block ???????????????????????? inode ????????????????????????? inode ???????????????????????????У????????????????????????????????Щ?????????? w ????й??
????????????????
# ln [-sf] source_filename dist_filename
-s ??????? hard link???? -s ? symbolic link
-f ????????5?????????????????????
1. ???????
hard link ?????????????????????????????????????????????? inode ?????????????????????????????????????????? 0??
????????????????? Filesystem??????????????????
# 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 ????????????????
# 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 ??????????????????
$ gzip [-cdtv#] filename
-c ???????????????????????
-d ???????
-t ??????????????????
-v ??????????????
-# ?? # ???????????????????????????????????????????6
2. bzip2
???? gzip ???????????
??????bzcat??bzmore??bzless??bzgrep??
$ bzip2 [-cdkzv#] filename
-k ??????????
3. xz
???? bzip2 ???????????
?????????gzip??bzip2??xz ??????????????????????????????????????????????
??????xzcat??xzmore??xzless??xzgrep??
$ xz [-dtlkc#] filename
???
????????????????????????????????????????????????????????tar ???????????????????????? gip??bzip2??xz ?????????????????
$ 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 ??????????????????? ?? ?? ?? tar -jtv -f filename.tar.bz2 ????? ??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 ???䶮
$ 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 ????
$ declare [-aixr] variable
-a ?? ?????????????
-i ?? ?????????????
-x ?? ?????????????
-r ?? ?????readonly????
??? [ ] ??????????в?????
$ array[1]=a
$ array[2]=b
$ echo ${array[1]}
??????????
- ?????????·?????????????? /bin/ls ???? ./ls ??
- ?????????????????У?
- ?? Bash ????????????У?
- ?? $PATH ?????????????·??????????????????????С?
???????????
???????????????????????????????????????????
- ??????? (stdin) ??????? 0 ????? < ?? << ??
- ?????? (stdout) ??????? 1 ????? > ?? >> ??
- ??????????(stderr)??????? 2 ????? 2> ?? 2>> ??
???У?????????????????????????????????????????????????????
??????????????????????????????????? /dev/null ???????????????
??????????????????????????????????????????????????????????????????????? 2>&1 ???????????????????????????
$ find /home -name .bashrc > list 2>&1
???????
??????????????????????????????????????????????????????????????????????????????????????????ù???????????????? | ??????????????䶮
$ ls -al /etc | less
1. ??????cut
????????????е???С?
cut ??????????з??????????????
$ cut
-d ???????
-f ?????? -d ???????? -f n ????? n ??????
-c ??????????λ???????
????1??last ????????????????????????????????
$ 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 ??????????????????
$ 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 ????????
$ sort [-fbMnrtuk] [file or stdin]
-f ???????Сд
-b ??????????????
-M ?????·???????????????? JAN, DEC
-n ?????????
-r ??????????
-u ?????? unique ?????????????????
-t ?????????????tab
-k ??????????????
??????/etc/passwd ???????? : ?????????????????????
$ 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 ?????????????????????
$ uniq [-ic]
-i ???????Сд
-c ?????м???
??????????????????????
$ last | cut -d ' ' -f 1 | sort | uniq -c
1
6 (unknown
47 dmtsai
4 reboot
7 root
1 wtmp
3. ???????????tee
?????????????????????????У??? tee ??????????????????????????????????????????????? tee ?????????????????????????????
$ tee [-a] file
4. ?????????tr??col??expand??join??paste
tr ???????????е???????????????????滻??
$ tr [-ds] SET1 ...
-d ?? ??????? SET1 ????????
???????? last ????????????Сд??????д??
$ last | tr '[a-z]' '[A-Z]'
col ?? tab ?????????????
$ col [-xb]
-x ?? ?? tab ?????????????
expand ?? tab ??????????????????? 8 ????
$ expand [-t] file
-t ??tab ??????????
join ????????????????к???????
$ join [-ti12] file1 file2
-t ????????????????
-i ???????Сд?????
-1 ????????????????????
-2 ????????????????????
paste ????????????????
$ paste [-d] file1 file2
-d ????????????? tab
5. ???????split
split ????????????????????
$ split [-bl] file PREFIX
-b ?????С?????з?????????λ?????? b, k, m ??
-l ?????????????з?????
- PREFIX ??????????????????
??????????????????????
grep
???????????????????????????
$ grep [-acinv] [--color=auto] ???????? filename
-a ?? ?? binary ????? text ??????????????
-c ?? ???????????
-i ?? ?????Сд
-n ?? ????к?
-v ?? ????????????????? ???????? ??????????
--color=auto ??????????????????
??????????? the ??????????????????????????? --color=auto ???????????????? Linux ?????????? the ???????
$ 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 ???????????????????????????????????????塣
$ grep -n 'go\{2,5\}g' regular_express.txt
?????????????ο? ???????.
printf
?????????????
????????????????? printf ????????????? $( ) ?????
$ 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
$ awk '???????? 1{???? 1} ???????? 2{???? 2} ...' filename
awk ??δ?????У????????С??λ????Σ??????ε???????????$n??n ???κ???? 1 ?????$0 ???????С?
???? 1???????????????????? ip
$ 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??????????????к????????????ж??????
$ 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 ????????д???
cat /etc/passwd | awk 'BEGIN {FS=":"} $3 < 10 {print $1 "\t " $3}'
root 0
bin 1
daemon 2
vim ??????
????????????????????????????????洢?????
| ???? | ???? |
|---|---|
| :w | д????? |
| :w! | ???????????????д???????????????д???????????????????й? |
| :q | ?? |
| :q! | ??????????? |
| :wq | д???????? |
| :wq! | ???д???????? |
?ο?????
- ???. ?? ?? ?? Linux ? ?? ?? ?? ?? ? ?? ?? ??[J]. 2009.
- Linux ??????????????







