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

24 KiB
Raw Blame History

???¨°??????????

????

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)???????????????§Û????§Ö??????????

???????????????

???????

??????

  1. modification time (mtime)????????????????????
  2. status time (ctime)??????????????????????????????
  3. 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 ?????

???????????

??????????

????????§Ú????????????????????????????????????????????????????????????????§Ö???????????????????????????????????????????????????????????????????

???????????????????

  1. superblock????????????????????????? inode ?? block ?????????????????????????????????????????????
  2. inode?????????????? inode???????????????????????????????????? block ????
  3. 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]}

??????????

  1. ?????????¡¤?????????????? /bin/ls ???? ./ls ??
  2. ?????????????????§µ?
  3. ?? Bash ????????????§µ?
  4. ?? $PATH ?????????????¡¤??????????????????????§³?

???????????

???????????????????????????????????????????

  1. ??????? (stdin)      ??????? 0 ????? < ?? << ??
  2. ?????? (stdout)    ??????? 1 ????? > ?? >> ??
  3. ??????????(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 ???????????§Ö???????????????????<3F>I??

$ 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! ???§Õ????????

?¦Ï?????