?##### 存儲網絡:
“`
DAS—–直接連接存儲(Direct attached storage)
NAS—–網絡連接存儲(Network attached storage)
SAN—–存儲區域網絡(storage area network)
“`
“`
DAS優缺點:
直接存儲(Direct Attached Storage)。存儲設備與主機的緊密相連
1.管理成本較低,實施簡單
2.儲時直接依附在服務器上,因此存儲共享受到限制
3.CPU必須同時完成磁盤存取和應用運行的雙重任務,所以不利于CPU的
指令周期的優化,增加系統負擔
“`
“`
NAS優缺點:
網絡連接存儲(Network Attached Storage):
通過局域網在多個文件服務器之間實現了互聯,基于文件的協議( NFS、
SMB/CIFS ),實現文件共享
1.集中管理數據,從而釋放帶寬、提高性能
2.可提供跨平臺文件共享功能
3.可靠性較差,適用于局域網或較小的網絡
“`
“`
SAN優缺點:
存儲區域網絡(Storage Area Networks,SAN)
利用高速的光纖網絡鏈接服務器與存儲設備,基于SCSI,IP,ATM等多種高
級協議,實現存儲共享
1.服務器跟儲存裝置兩者各司其職
2.利用光纖信道來傳輸數據﹐以達到一個服務器與儲存裝置之間多對多
的高效能、高穩定度的存儲環境
3.實施復雜,管理成本高
“`
##### 操作系統六大功能:
“`
硬件驅動
進程管理
網絡管理
安全管理
文件管理
內存管理
“`
##### 命令lsblk(查看塊設備情況)
例:
“`
[root@centos7 share]#lsblk
NAME ? MAJ:MIN RM ?SIZE RO TYPE MOUNTPOINT
fd0 ? ? ?2:0 ? ?1 ? ?4K ?0 disk
sda ? ? ?8:0 ? ?0 ?200G ?0 disk
├─sda1 ? 8:1 ? ?0 ? ?1G ?0 part /boot
├─sda2 ? 8:2 ? ?0 ? 50G ?0 part /
├─sda3 ? 8:3 ? ?0 ? 30G ?0 part /data
├─sda4 ? 8:4 ? ?0 ? ?1K ?0 part
└─sda5 ? 8:5 ? ?0 ? ?2G ?0 part [SWAP]
sr0 ? ? 11:0 ? ?1 ?8.1G ?0 rom ?/run/media/root/CentOS 7 x86_64
“`
##### 開發接口標準:
“`
程序員寫好的程序在a操作系統可以用,能否在b上應用
編程接口兼容:
posix:portable operating system ?可移植的操作系統
任何遵循POSIX標準的api接口,其源代碼在a系統上可用,在b上也可用,在abi運行接口上則不兼容,一旦被編譯成二進制,就變成對應機器獨特的二進制格式
API:application program interface
程序員面對的編程接口 ? ?定義了源代碼與庫之間的接口
ABI:application binary interface
程序應用者面對的運行程序接口 ? ?描述了應用程序與os之間的底層接口
“`
##### ?自動登錄:
“`
修改配置文件vi /etc/gdm/custom.conf
[daemon]
AutomaticLoginEnable=true ? ? ? ?###開啟自動登錄
AutomaticLogin=root ? ? ? ? ? ? ?###以root身份運行
“`
##### 命令id (查看用戶ID號)
“`
-u 只顯示id號
例:
[root@centos7 share]#id -u
0
“`
##### 命令tty(查看當前終端)
“`
例:
[root@centos6 ~]# tty
/dev/pts/0
“`
##### 命令who(查看當前所有登錄用戶,并查看其終端)
“`
例:
[root@centos6 ~]# who
root ? ? tty1 ? ? ? ? 2018-03-28 19:02 (:0) ? ? ? ? ? ?###本機登錄
root ? ? pts/0 ? ? ? ?2018-03-28 19:06 (192.168.30.1) ###表示通過遠程登錄
“`
##### 命令whoami(顯示當前用戶身份)
“`
例:
[root@centos6 ~]# whoami
root
“`
##### 命令who am i(顯示身份,登錄終端等信息)
“`
例:
[root@centos6 ~]# who am i
root ? ? pts/0 ? ? ? ?2018-03-28 19:06 (192.168.30.1)
“`
##### 命令w(查看誰在登錄,并查看其正在做什么)
“`
例:
[root@centos7 share]#w
?00:51:42 up ?8:39, ?4 users, ?load average: 0.00, 0.01, 0.05
USER ? ? TTY ? ? ?FROM ? ? ? ? ? ? LOGIN@ ? IDLE ? JCPU ? PCPU WHAT
root ? ? :0 ? ? ? :0 ? ? ? ? ? ? ? 16:12 ? ?xdm? ?10:21 ? 0.69s /usr/libexec/gnome-sess
root ? ? pts/0 ? ?:0 ? ? ? ? ? ? ? 18:47 ? ?6:03m ?0.30s ?0.30s bash
root ? ? pts/1 ? ?192.168.30.1 ? ? 22:23 ? ?6.00s ?1.45s ?0.15s w
root ? ? tty2 ? ? ? ? ? ? ? ? ? ? ?18:48 ? ?5:58m ?0.19s ?0.19s -bash
“`
##### 命令init
“`
例:
init 3 關掉圖形界面進入字符界面
init 0 關機
init 5 切換到圖形界面
備注:init是模式切換,startx 只是開啟圖形工具,不改變運行等級
“`
##### 命令runlevel(查看運行等級)
“`
例:
[root@centos7 share]#runlevel
N 5
“`
##### 命令chvt
“`
例:
chvt 2 ?切換到虛擬終端2
“`
##### shell
“`
cat /etc/shells查看當前系統所有shell類型
“`
##### 命令>(創建新文件,清空文件)
“`
例:
> file ? ? ###若文件不存在則創建,若存在則清空文件
“`
##### 命令提示符
“`
相關變量PS1
修改提示符格式,man bash可查看PS1詳細用法
PS1=”\[\e[1;5;41;33m\][\u@\h \W]\\$\[\e[0m\]”
\e \033 \u 當前用戶
\h 主機名簡稱 \H 主機名
\w 當前工作目錄 \W 當前工作目錄基名
\t 24小時時間格式 \T 12小時時間格式
使其永久生效,寫入配置文件/etc/profile.d/env.sh,文件名必須是.sh結尾
. 或者source重新加載配置文件
“`
##### 命令help
“`
help 列出所有內置命令
help cd 查看內置命令幫助
ls –help 查看外部命令幫助
“`
##### 命令type(查看命令是否外置內置)
“`
例:
root@centos7 share]#type cd
cd is a shell builtin
“`
##### 命令enable(禁用內部命令)
“`
-n 禁用內部命令
例:
enable -n cd
enable cd ? ? ? ###啟用命令
“`
##### 外部命令查找方式
“`
外部命令按變量$PATH查找
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
例:
cp /usr/bin/yes /usr/local/sbin/yes
執行命令yes
執行的結果用type查看
[root@centos6 ~]#type yes
yes is hashed (/usr/local/sbin/yes)
但要注意清除hash緩存
“`
##### 命令hash
“`
hash顯示緩存
hash –l 顯示緩存可以作為輸入使用
hash –p name 將命令全路徑path改名為name
hash –t name 打印緩存中name的路徑
hash –d name清除name緩存
hash –r 清除緩存
“`
##### 命令which(查看外部命令對應的路徑)
“`
按照$PATH逐個查找。假如在第一個目錄中找到了,則不再繼續往下查找,要想列出所有執行which –a name。
which –skip-alias ls ? 不顯示別名,只顯示路徑
例:
[root@centos6 ~]#which –skip-alias ls
/bin/ls
“`
##### 命令whereis(顯示路徑更為全面,包括一些文檔路徑)
“`
例:
[root@centos7 share]#whereis ls
ls: /usr/bin/ls /usr/share/man/man1/ls.1.gz /usr/share/man/man1p/ls.1p.gz
“`
##### 命令alias(定義命令別名)
“`
要想一直生效,寫入配置文件.bashrc(只針對當前用戶有效,對所有用戶有效,則寫入配置文件/etc/bashrc)
“`
##### 命令unalias(取消別名)
“`
例:
unalias -a ? ?###取消所有別名
“`
##### 內部命令和別名的優先級
“`
例:alias enable=who
執行命令enable
[root@centos6 ~]#enable
root ? ? tty1 ? ? ? ? 2018-03-29 07:46 (:0)
root ? ? pts/0 ? ? ? ?2018-03-29 07:53 (:0.0)
root ? ? pts/1 ? ? ? ?2018-03-29 07:54 (192.168.30.1)
說明別名命令優先級更高
“`
##### 只使用原始命令而不使用別名的方法
“`
‘command’
\command
命令的絕對路徑
“`
##### 一條命令分多行輸入執行
“`
例:
[root@centos6 ~]#hos\
> t\
> name
centos6
“`
##### 命令date
“`
-d ?–date ? 顯示指定日期
例:
[root@centos7 share]#date -d “-2 day” +%F
2018-03-28 ? ? ? ? ? ? ? ? ? ? ? ? ####顯示前兩天日期
-s
例:[root@centos7 share]#date -s “-1 day”
Thu Mar 29 01:40:26 CST 2018 ? ? ? ###設置成昨天的時間
練習:
顯示前天是星期幾
date -d “-2 day” +%A
“`
##### 命令hwclock,clock
“`
-s –hctosys 以硬件時間為準
-w –systohc以系統時間為準
“`
##### 命令timedatectl
“`
timedatectl list-timezones ? ? ? ? ? ? ? ?列出所有時區
timedatectl status ? ? ? ? ? ? ? ? ? ? ? ?查看當前時區
timedatectl set-timezone “Asia/Hongkong” ?設置時區
時區相關文件/etc/localtime ?或者/usr/share/zoneinfo
“`
##### 命令cal(查看日歷)
“`
-y
例:
[root@centos7 share]#cal 3 2018
? ? ?March 2018
Su Mo Tu We Th Fr Sa
? ? ? ? ? ? ?1 ?2 ?3
?4 ?5 ?6 ?7 ?8 ?9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31
“`
##### 命令shutdown
“`
-r reboot
-h halt
-c cancel
TIME:無指定,默認相當于 +1
now: 立刻,相當于+0
+m: 相對時間表示法,幾分鐘之后;例如 +3
hh:mm: 絕對時間表示,指明具體時間
“`
##### 命令wall
“`
例:
wall “hello”
“`
##### 命令screen
“`
創建新screen會話
screen –S [SESSION]
加入screen會話
screen –x [SESSION]
退出并關閉screen會話
exit
剝離當前screen會話
Ctrl+a,d
顯示所有已經打開的screen會話
screen -ls
恢復某screen會話
screen -r [SESSION]
“`
##### 利用screen實現遠程協助
“`
1.在本機創建會話screen -S help
2.在遠程登錄需要幫助的主機,查看會話screen -ls ,然后加入會話screen -x實現同步操作
“`
##### 利用screen備份數據,防止備份中斷
“`
1.進入會話(執行screen),然后開始備份操作
2.假若備份中斷,進入會話(執行screen),然后執行screen -r進行恢復
“`
##### 命令echo
“`
功能:顯示字符
?語法:echo [-neE][字符串]
?說明:echo會將輸入的字符串送往標準輸出。輸出的字符串間以空白字符隔開
并在最后加上換行號
?選項:
? -E (默認)不支持 \ 解釋功能
? -n 不自動換行
? -e 啟用 \ 字符的解釋功能
?顯示變量
echo “$VAR_NAME“ ?變量會替換,弱引用
echo ‘$VAR_NAME‘ ? 變量不會替換,強引用
?啟用命令選項-e,若字符串中出現以下字符,則特別加以處理,而不會將它當成
一般文字輸出
\a 發出警告聲
\b 退格鍵
\c 最后不加上換行符號
\n 換行且光標移至行首
\r 回車,即光標移至行首,但不換行
\t 插入tab
\\ 插入\字符
\0nnn 插入nnn(八進制)所代表的ASCII字符
echo -e ‘\033[43;31;5mmagedu\033[0m’
\xHH插入HH(十六進制)所代表的ASCII字
“`
“`
例:
[root@centos6 data]#echo {10..30..2}
10 12 14 16 18 20 22 24 26 28 30 ? ? ? ?####按2遞增
例:[root@centos6 data]#echo {a..Z}
a ` _ ^ ] ?[ Z ? ? ? ? ? ? ? ? ? ? ? ? ?###結果按照ASCII碼順序顯示
例:[root@centos6 data]#echo file{a,b,c}.{log,txt}
filea.log filea.txt fileb.log fileb.txt filec.log filec.txt
“`
##### 登錄前提示
“`
配置文件/etc/motd
編輯內容,登錄后即可顯示登錄提示
“`
##### 登錄后提示信息
“`
/etc/issue
“`
“`
“`
##### 一個命令調用另一個命令的輸出
“`
例:
cp `which –skip-alias ls` /tmp
“`
##### tab鍵
“`
~2tab ?列出所有用戶
$2tab ?列出所有變量
“`
##### 命令history
“`
echo $HISTSIZE ? ?默認記錄1000條,編輯/etc/profile可修改HISTSIZE
命令行歷史
保存你輸入的命令歷史??梢杂盟鼇碇貜蛨绦忻?/div>
登錄shell時,會讀取命令歷史文件中記錄下的命令~/.bash_history
-c清空歷史,但不會清除.bash_history 中的歷史
-d 清空指定的歷史條目
-a 把內存中的歷史寫入磁盤中
-r 把磁盤中的歷史追加到內存
-w: 保存歷史列表到指定的歷史文件
-n: 讀歷史文件中未讀過的行到歷史列表
-p:不記錄歷史 ? ? ? ? ? ? ? ? ?例:history -p `ls`
-s:構造歷史,但命令實際不執行 ? 例:history -s `rm -rf *`
“`
“`
登錄進shell后新執行的命令只會記錄在緩存中;這些命令會用戶退出時“追
加”至命令歷史文件中
?重復前一個命令,有4種方法
? 重復前一個命令使用上方向鍵,并回車執行
? 按 !! 并回車執行
? 輸入 !-1 并回車執行
? 按 Ctrl+p 并回車執行
?!:0 ?執行前一條命令(去除參數)
?Ctrl + n 顯示當前歷史中的下一條命令,但不執行
?Ctrl + j 執行當前命令
?!n 執行history命令輸出對應序號n的命令
?!-n 執行history歷史中倒數第n個命令
?!string 重復前一個以“string”開頭的命令
?!?string 重復前一個包含string的命令
?!string:p 僅打印命令歷史,而不執行
?!$:p 打印輸出 !$ (上一條命令的最后一個參數)的內容
?!*:p 打印輸出 !*(上一條命令的所有參數)的內容
?^string 刪除上一條命令中的第一個string
?^string1^string2 將上一條命令中的第一個string1替換為string2
?!:gs/string1/string2 將上一條命令中所有的string1都替換為 string2
?使用up(向上)和down(向下)鍵來上下瀏覽從前輸入的命令
?ctrl-r來在命令歷史中搜索命令
?(reverse-i-search)`’:
?Ctrl+g:從歷史搜索模式退出
?要重新調用前一個命令中最后一個參數
?!$ ?表示
?Esc, .(點擊Esc鍵后松開,然后點擊 . 鍵)
?Alt+ .(按住Alt鍵的同時點擊 . 鍵)
調用歷史參數:
?command !string:^ 從命令歷史中搜索以 string 開頭的命令,并獲取它的第一
個參數
?command !string:$ 從命令歷史中搜索以 string 開頭的命令,并獲取它的最后一
個參數
?command !string:n 從命令歷史中搜索以 string 開頭的命令,并獲取它的第n
個參數
?command !string:* 從命令歷史中搜索以 string 開頭的命令,并獲取它的所有
參數
“`
##### 命令whatis=man -f
“`
例:
[root@centos6 ~]#whatis ls
ls ? ? ? ? ? ? ? ? ? (1) ?– list directory contents
ls ? ? ? ? ? ? ? ? ? (1p) ?– list directory contents
centos6制作數據庫 ?makewhatis
centos7制作數據庫 ?mandb
“`
##### man幫助
“`
man -k password ?表示查看跟password相關的章節
“`
##### 命令歷史相關環境變量
“`
HISTSIZE:命令歷史記錄的條數
HISTFILE:指定歷史文件,默認為~/.bash_history
HISTFILESIZE:命令歷史文件記錄歷史的條數
HISTTIMEFORMAT=”%F %T” 顯示時間
HISTIGNORE=“str1:str2*:… “ 忽略str1命令,str2開頭的歷史
控制命令歷史的記錄方式:
環境變量:HISTCONTROL
ignoredups 默認,忽略重復的命令,連續且相同為“重復”
ignorespace 忽略所有以空白開頭的命令
ignoreboth 相當于ignoredups, ignorespace的組合
erasedups 刪除重復命令
export 變量名=”值“
存放在 /etc/profile 或 ~/.bash_profile ? ?最好存放在自己創建的文件中/etc/profile.d/env.sh
“`
##### 命令ldd(查看程序庫調用)
“`
例:[root@centos7 share]#ldd /bin/ls
linux-vdso.so.1 => ?(0x00007ffdcf3ee000)
libselinux.so.1 => /lib64/libselinux.so.1 (0x00007f2f9eeb0000)
libcap.so.2 => /lib64/libcap.so.2 (0x00007f2f9ecab000)
libacl.so.1 => /lib64/libacl.so.1 (0x00007f2f9eaa1000)
libc.so.6 => /lib64/libc.so.6 (0x00007f2f9e6de000)
libpcre.so.1 => /lib64/libpcre.so.1 (0x00007f2f9e47c000)
libdl.so.2 => /lib64/libdl.so.2 (0x00007f2f9e277000)
/lib64/ld-linux-x86-64.so.2 (0x000055b2e31e1000)
libattr.so.1 => /lib64/libattr.so.1 (0x00007f2f9e072000)
libpthread.so.0 => /lib64/libpthread.so.0 (0x00007f2f9de56000)
“`
##### centos6自動掛在光盤
“`
[root@centos6 ~]#cd /misc
[root@centos6 misc]#cd cd/
[root@centos6 cd]#df
“`
##### 識別新增硬盤
“`
echo ‘- – -‘ >/sys/class/scsi_host/host2/scan
“`
##### centos7目錄的變化
“`
/bin和/usr/bin
/sbin和/usr/sbin
/lib和/usr/lib
/lib64和/usr/lib64
以上目錄都是以軟鏈接的形式,而在centos6則表示不同的目錄
“`
##### 字符設備與塊設備
“`
字符設備是順序訪問,塊設備是隨機訪問,塊設備有緩存,而字符設備沒有,字符設備如:/dev/zero.
[root@centos7 18:58:03 ~]#ll /dev/zero /dev/sda
brw-rw—-. 1 root disk 8, 0 Mar 30 16:03 /dev/sda
crw-rw-rw-. 1 root root 1, 5 Mar 30 16:03 /dev/zero
“`
##### 定義各類文件或目錄的顏色
“`
編輯配置文件vi /etc/DIR_COLORS
“`
##### 命令ls
“`
選項:
-R 目錄遞歸通過
-ld目錄和符號鏈接信息
-1文件分行顯示
-S按大到小排序
-t按Mtime排序
-u配合-t選項,顯示并按atime從新到舊排序
-U按目錄存放順序顯示(即文件創建的順序)
-X按文件后綴排序
-r倒序排列
例:-t
root@centos7 09:47:44 data]#vi file2
[root@centos7 09:49:31 data]#ls -t
file2 ? file41 ?file46 ?file18 ?file24 ?file29 ?file34 ?file10 ?file15 ?file9 ?file6
例:[root@centos7 19:36:48 data]#ls -1
file1
file10
file2
file3
file4
file5
file6
file7
例:按文件大小倒序排列
[root@centos7 19:41:08 /]#ls -Slr
total 20
dr-xr-xr-x. ?13 root root ? ?0 Mar 30 16:02 sys
dr-xr-xr-x. 198 root root ? ?0 Mar 30 16:02 proc
-rw-r–r–. ? 1 root root ? ?0 Mar 30 04:40 a
drwxr-xr-x. ? 2 root root ? ?6 Nov ?5 ?2016 srv
drwxr-xr-x. ? 2 root root ? ?6 Nov ?5 ?2016 mnt
drwxr-xr-x. ? 2 root root ? ?6 Nov ?5 ?2016 media
lrwxrwxrwx. ? 1 root root ? ?7 Mar 28 02:04 lib -> usr/lib
lrwxrwxrwx. ? 1 root root ? ?7 Mar 28 02:04 bin -> usr/bin
lrwxrwxrwx. ? 1 root root ? ?8 Mar 28 02:04 sbin -> usr/sbin
lrwxrwxrwx. ? 1 root root ? ?9 Mar 28 02:04 lib64 -> usr/lib64
例:按ctime排序
[root@centos7 19:53:58 data]#chown hehe. file3
[root@centos7 19:54:11 data]#chown hehe. file5
[root@centos7 19:54:16 data]#chown hehe. file6
[root@centos7 19:54:20 data]#ls -clt
total 0
-rw-r–r–. 1 hehe hehe ?0 Mar 30 19:54 file6
-rw-r–r–. 1 hehe hehe ?0 Mar 30 19:54 file5
-rw-r–r–. 1 hehe hehe ?0 Mar 30 19:54 file3
-rw-r–r–. 1 root root ?0 Mar 30 19:36 file1
-rw-r–r–. 1 root root ?0 Mar 30 19:36 file10
-rw-r–r–. 1 root root ?0 Mar 30 19:36 file2
-rw-r–r–. 1 root root ?0 Mar 30 19:36 file4
例:查看鏈接文件/bin加“/”與不加的區別
[root@centos7 09:38:24 data]#ll /bin/
-rwxr-xr-x. 1 root root ? ? ? 1118 Dec 29 ?2013 zenheiset
-rwxr-xr-x. 1 root root ? ? 103608 Aug ?2 ?2017 zenity
-rwxr-xr-x. 1 root root ? ? ? ?123 Aug ?2 ?2017 zfgrep
-rwxr-xr-x. 1 root root ? ? ? 2144 Aug ?2 ?2017 zforce
-rwxr-xr-x. 1 root root ? ? ? 5916 Aug ?2 ?2017 zgrep
-rwxr-xr-x. 1 root root ? ? 215840 Nov ?6 ?2016 zip
-rwxr-xr-x. 1 root root ? ? 100456 Nov ?6 ?2016 zipcloak
-rwxr-xr-x. 1 root root ? ? ?48451 Jun 10 ?2014 zipdetails
-rwxr-xr-x. 1 root root ? ? ? 2953 Oct 11 ?2008 zipgrep
-rwxr-xr-x. 2 root root ? ? 185488 Nov ?6 ?2016 zipinfo
-rwxr-xr-x. 1 root root ? ? ?95984 Nov ?6 ?2016 zipnote
-rwxr-xr-x. 1 root root ? ? 100096 Nov ?6 ?2016 zipsplit
-rwxr-xr-x. 1 root root ? ? ? 2041 Aug ?2 ?2017 zless
-rwxr-xr-x. 1 root root ? ? ? 2859 Aug ?2 ?2017 zmore
-rwxr-xr-x. 1 root root ? ? ? 5343 Aug ?2 ?2017 znew
lrwxrwxrwx. 1 root root ? ? ? ? ?6 Mar 28 02:05 zsoelim -> soelim
[root@centos7 09:38:24 data]#ll /bin
lrwxrwxrwx. 1 root root 7 Mar 28 02:04 /bin -> usr/bin
例:
[root@centos6 data]#ll -d /etc ? ? ?只顯示目錄本身
drwxr-xr-x. 118 root root 12288 Apr ?1 09:34 /etc
[root@centos6 data]#ll -d /etc/* ? ? 顯示目錄下文件和目錄
drwxr-xr-x. ?3 root root ? 4096 Mar 27 17:19 /etc/abrt
drwxr-xr-x. ?4 root root ? 4096 Mar 27 17:29 /etc/acpi
-rw-r–r–. ?1 root root ? ? 47 Mar 31 21:05 /etc/adjtime
-rw-r–r–. ?1 root root ? 1512 Jan 12 ?2010 /etc/aliases
[root@centos6 data]#ll -d /etc/*/ ? ?顯示目錄下目錄
drwxr-xr-x. ?3 root root 4096 Mar 27 17:19 /etc/abrt/
drwxr-xr-x. ?4 root root 4096 Mar 27 17:29 /etc/acpi/
drwxr-xr-x. ?2 root root 4096 Mar 27 17:26 /etc/alsa/
drwxr-xr-x. ?2 root root 4096 Mar 27 17:30 /etc/alternatives/
“`
##### 命令shutdown
“`
選項:
-r
centos6執行shutdown -r 后面要加時間
centos7則可以直接執行
“`
##### 文件通配符
“`
匹配零個或多個字符
? 匹配任何單個字符
~ 當前用戶家目錄
~mage 用戶mage家目錄
~+ 當前工作目錄
~- 前一個工作目錄
[0-9] 匹配數字范圍
[a-z]:字母
[A-Z]:字母
[wang] 匹配列表中的任何的一個字符
[^wang] 匹配列表中的所有字符以外的字符
例:通配符?
[root@centos7 20:01:30 data]#ls ?
你 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ###漢字也算一個字符
“`
“`
例:通配符[]
[root@centos7 20:29:40 data]#touch file{a..z} file{A..Z}
[root@centos7 20:30:16 data]#ls file[a-d]
filea ?fileA ?fileb ?fileB ?filec ?fileC ?filed
###字母排序以小大小大排序
“`
##### 文件通配符
“`
預定義的字符類:man 7 glob
[:digit:]:任意數字,相當于0-9
[:lower:]:任意小寫字母
[:upper:]: 任意大寫字母
[:alpha:]: 任意大小寫字母
[:alnum:]:任意數字或字母
[:blank:]:水平空白字符
[:space:]:水平或垂直空白字符
[:punct:]:標點符號
[:print:]:可打印字符
[:cntrl:]:控制(非打?。┳址?/div>
[:graph:]:圖形字符
[:xdigit:]:十六進制字符
“`
“`
例:只顯示隱藏文件,等價于l.
[root@centos7 20:39:01 ~]#ls -d .*
. ?.. ?.bash_history ?.bash_logout ?.bash_profile ?.bashrc ?.cache ?.config ?.cshrc ?.dbus ?.esd_auth ?.ICEauthority ?.lesshst ?.local ?.serverauth.3629 ?.tcshrc ?.Xauthority
“`
“`
例:顯示該目錄及子目錄下隱藏文件
[root@centos7 20:45:36 ~]#ls -R .[^.]*
.bash_history ?.bash_logout ?.bash_profile ?.bashrc ?.cshrc ?.esd_auth ?.ICEauthority ?.lesshst ?.serverauth.3629 ?.tcshrc ?.Xauthority
.cache:
abrt ? event-sound-cache.tdb.centos7.x86_64-redhat-linux-gnu ?gnome-control-center ?gnome-software ?imsettings ?thumbnails ?yelp
dconf ?evolution ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?gnome-shell ? ? ? ? ? gstreamer-1.0 ? media-art ? tracker
.cache/abrt:
applet_dirlist ?lastnotification
.cache/dconf:
user
.cache/evolution:
addressbook ?calendar ?mail ?memos ?sources ?tasks
注:目錄中含..這個目錄,表示上級目錄,因此第二個為字符為非.即[^.]
“`
“`
例:
[root@centos7 20:54:28 ~]#ls -ad .[^.]*
.aa ?.bash_history ?.bash_logout ?.bash_profile ?.bashrc ?.cache ?.config ?.cshrc ?.dbus ?.esd_auth ?.ICEauthority ?.lesshst ?.local ?.serverauth.3629 ?.tcshrc ?.Xauthority
“`
##### 命令touch
“`
touch [OPTION]… FILE…
-a 僅改變 atime和ctime
-m 僅改變 mtime和ctime
-t ?[[CC]YY]MMDDhhmm[.ss]
指定atime和mtime的時間戳
-c 如果文件不存在,則不予創建
注:touch創建新文件時,若文件名已存在,不會發生覆蓋,但改變時間戳,包括atime,ctime,mtime
例
[root@centos6 data]#touch -a file2
[root@centos6 data]#stat file2
? File: `file2′
? Size: 0 ? ? ? ? Blocks: 0 ? ? ? ? ?IO Block: 4096 ? regular empty file
Device: 803h/2051d Inode: 12 ? ? ? ? ?Links: 1
Access: (0644/-rw-r–r–) ?Uid: ( ? ?0/ ? ?root) ? Gid: ( ? ?0/ ? ?root)
Access: 2018-03-31 15:16:18.576995276 +0800
Modify: 2018-03-31 15:12:56.272000245 +0800
Change: 2018-03-31 15:16:18.576995276 +0800
[root@centos6 data]#touch -m file2
[root@centos6 data]#stat file2
? File: `file2′
? Size: 0 ? ? ? ? Blocks: 0 ? ? ? ? ?IO Block: 4096 ? regular empty file
Device: 803h/2051d Inode: 12 ? ? ? ? ?Links: 1
Access: (0644/-rw-r–r–) ?Uid: ( ? ?0/ ? ?root) ? Gid: ( ? ?0/ ? ?root)
Access: 2018-03-31 15:16:18.576995276 +0800
Modify: 2018-03-31 15:17:18.587996113 +0800
Change: 2018-03-31 15:17:18.587996113 +0800
“`
##### 命令cp
“`
常用選項:
-i:覆蓋前提示 –n:不覆蓋,注意兩者順序
-r, -R: 遞歸復制目錄及內部的所有內容
-a: 歸檔,相當于-dR –preserv=all
-d:–no-dereference –preserv=links 不復制原文件,只復制鏈接名
–preserv[=ATTR_LIST]
mode: 權限
ownership: 屬主屬組
timestamp:
links
xattr
context
all
-p: 等同–preserv=mode,ownership,timestamp
-v: –verbose
-f: –force
-u:–update 只復制源比目標更新文件或目標不存在的文件
–backup=numbered 目標存在,覆蓋前先備份加數字后綴
“`
“`
-f強制覆蓋(先刪除原文件,再創建新文件,所有者所屬主均發生變化)
“`
“`
[hehe@centos7 21:25:16 ~]$ll
total 4
-rw-r–r–. 1 root root 23 Mar 30 21:24 issue
[hehe@centos7 21:25:23 ~]$cp -f /etc/motd issue
[hehe@centos7 21:25:51 ~]$ll
total 4
-rw-r–r–. 1 hehe hehe 7 Mar 30 21:25 issue
“`
“`
-d保留軟鏈接
例:
[root@centos7 21:34:36 data]#ll /etc/grub2.cfg
lrwxrwxrwx. 1 root root 22 Mar 28 02:10 /etc/grub2.cfg -> ../boot/grub2/grub.cfg
[root@centos7 21:35:09 data]#cp -d /etc/grub2.cfg /data
[root@centos7 21:35:43 data]#ll
total 0
lrwxrwxrwx. 1 root root 22 Mar 30 21:35 grub2.cfg -> ../boot/grub2/grub.cfg
“`
“`
-a存檔,保留所有屬性,常用于備份,可和-v(顯示過程)一起使用,但容易造成終端死機
–backup=numbered
例:
[root@centos7 21:47:39 ~]#cp –backup=numbered f1 /data/f1
cp: overwrite ‘/data/f1’? y
[root@centos7 21:47:55 data]#ls
f1 ?f1.~1~ ?f1.~2~
可定義成別名,,防止誤覆蓋
“`
“`
復制特殊文件如:/dev/sda,r如果不加-a,會復制里面的數據,而不是文件名稱
cp -a /dev/sda ./
“`
“`
練習
1、定義別名命令baketc,每天將/etc/目錄下所有文件,備份到
/data獨立的子目錄下,并要求子目錄格式為 backupYYYY-mm-dd,
備份過程可見
練習
[root@centos7 22:00:09 data]#cp -av /etc /data/backup`date +%F`
[root@centos7 22:00:04 data]#ls
backup2018-03-30
[root@centos7 15:48:45 data]#alias backetc=’cp -av /etc /data/backup`date +%F`’
“`
“`
##### 命令rename
例:
[root@centos7 22:09:13 data]#touch {1..30}.conf
[root@centos7 22:09:28 data]#ls
10.conf ?13.conf ?16.conf ?19.conf ?21.conf ?24.conf ?27.conf ?2.conf ? 4.conf ?7.conf
11.conf ?14.conf ?17.conf ?1.conf ? 22.conf ?25.conf ?28.conf ?30.conf ?5.conf ?8.conf
12.conf ?15.conf ?18.conf ?20.conf ?23.conf ?26.conf ?29.conf ?3.conf ? 6.conf ?9.conf
[root@centos7 22:09:28 data]#rename “conf” “conf.bak” *.conf
[root@centos7 22:11:21 data]#ls
10.conf.bak ?14.conf.bak ?18.conf.bak ?21.conf.bak ?25.conf.bak ?29.conf.bak ?4.conf.bak ?8.conf.bak
11.conf.bak ?15.conf.bak ?19.conf.bak ?22.conf.bak ?26.conf.bak ?2.conf.bak ? 5.conf.bak ?9.conf.bak
12.conf.bak ?16.conf.bak ?1.conf.bak ? 23.conf.bak ?27.conf.bak ?30.conf.bak ?6.conf.bak
13.conf.bak ?17.conf.bak ?20.conf.bak ?24.conf.bak ?28.conf.bak ?3.conf.bak ? 7.conf.bak
“`
##### 命令rm防止誤刪除
“`
方法:定義別名
[root@centos7 08:28:26 ~]#alias rm=’mv -t /data’
并且寫入配置文件.bashrc
“`
##### 磁盤空間被占滿
“`
在boot目錄下生成一個大文件
[root@centos7 08:51:45 boot]#dd if=/dev/zero of=/boot/bigfile bs=1M count=800
[root@centos7 08:53:47 boot]#df -h
Filesystem ? ? ?Size ?Used Avail Use% Mounted on
/dev/sda3 ? ? ? ?30G ? 33M ? 30G ? 1% /data
/dev/sda1 ? ? ?1014M ?958M ? 57M ?95% /boot
此時/boot空間即將被占滿,假若有用戶正在訪問bigfile這個文件,用rm刪除文件,文件雖然被刪除,但并不能馬上釋放空間。
解決方法一:
lsof |grep deleted ?查看系統中被刪除,但還沒釋放空間的文件。
[root@centos7 09:12:18 boot]#lsof |grep deleted
single ? ?1691 2277 ? ? ? ? ? root ? 15r ? ? ?REG ? ? ? ? ? ? ? ?8,2 ? ? 32768 ? 68247442 /root/.local/share/gvfs-metadata/root-056956aa.log (deleted)
vi ? ? ? ?2935 ? ? ? ? ? ? ? ?root ? ?3r ? ? ?REG ? ? ? ? ? ? ? ?8,1 838860800 ? ? ? ? 79 /boot/bigfile (deleted)
[root@centos7 09:12:48 boot]#killall vi
[root@centos7 09:15:23 boot]#df -h
Filesystem ? ? ?Size ?Used Avail Use% Mounted on
tmpfs ? ? ? ? ? 609M ? ? 0 ?609M ? 0% /sys/fs/cgroup
/dev/sda3 ? ? ? ?30G ? 33M ? 30G ? 1% /data
/dev/sda1 ? ? ?1014M ?158M ?857M ?16% /boot
“`
“`
解決方法二:
[root@centos7 09:17:41 boot]#>bigfile
[root@centos7 09:17:55 boot]#df -h
Filesystem ? ? ?Size ?Used Avail Use% Mounted on
/dev/sda3 ? ? ? ?30G ? 33M ? 30G ? 1% /data
/dev/sda1 ? ? ?1014M ?158M ?857M ?16% /boot
tmpfs ? ? ? ? ? 122M ? 24K ?122M ? 1% /run/user/0
/dev/sr0 ? ? ? ?8.1G ?8.1G ? ? 0 100% /run/media/root/CentOS 7 x86_64
[root@centos7 09:18:14 boot]#\rm -rf bigfile
“`
##### 命令tree
“`
-L 顯示目錄的層級 ? ? ? 例:tree -L 2 /etc/
-d 只顯示目錄不顯示文件
-P pattern 只顯示由指定pattern匹配到的路徑
“`
##### 元數據與數據
“`
inode(index node)表中包含文件系統所有文件列表
一個節點 (索引節點)是在一個表項,包含有關文件的信息( 元數據 ),包
括:
文件類型,權限,UID,GID
鏈接數(指向這個文件名路徑名稱個數)
該文件的大小和不同的時間戳
指向磁盤上文件的數據塊指針
有關文件的其他數據
目錄的元數據:包括目錄自身權限,目錄類型,UID,GID等信息
目錄真實數據:存放的是文件i節點編號,通過節點編號找到節點表,通過節點表找到磁盤中文件數據,因此刪除目錄中的文件并不是刪除文件數據,只是刪除文件節點編號,數據并未被馬上刪除。
徹底刪除磁盤中的文件使用如下方法:
[root@centos7 10:36:51 data]#ll
total 4
-rw-r–r–. 1 root root 2059 Mar 31 10:36 passwd
[root@centos7 10:36:54 data]#shred -zv -n 8 passwd
shred: passwd: pass 1/9 (random)…
shred: passwd: pass 2/9 (555555)…
shred: passwd: pass 3/9 (000000)…
shred: passwd: pass 4/9 (random)…
shred: passwd: pass 5/9 (ffffff)…
shred: passwd: pass 6/9 (random)…
shred: passwd: pass 7/9 (aaaaaa)…
shred: passwd: pass 8/9 (random)…
shred: passwd: pass 9/9 (000000)…
[root@centos7 10:37:18 data]#ll
total 4
-rw-r–r–. 1 root root 4096 Mar 31 10:37 passwd
-z表示最后一次用0覆蓋,-n表示覆蓋的次數
“`
“`
模擬磁盤inode被用完,但磁盤空間并沒有滿(/boot/test)
[root@centos7 10:07:10 testdir]#df -i
Filesystem ? ? ? Inodes ?IUsed ? ?IFree IUse% Mounted on
/dev/sda3 ? ? ?15728640 ? ? 10 15728630 ? ?1% /data
/dev/sda1 ? ? ? ?524288 ? ?327 ? 523961 ? ?1% /boot
echo file{1..524288} |xargs touch ? ?xargs解決參數過多問題
touch: cannot touch ‘file524287’: No space left on device
touch: cannot touch ‘file524288’: No space left on device
[root@centos7 10:14:14 test]#df -h
/dev/sda1 ? ? ?1014M ?433M ?582M ?43% /boot
[root@centos7 10:13:13 test]#df -i
Filesystem ? ? ? Inodes ?IUsed ? ?IFree IUse% Mounted on
/dev/sda1 ? ? ? ?524288 524288 ? ? ? ?0 ?100% /boot
“`
“`
結果磁盤空間還剩余,但inode被用光,提示磁盤空間不足,刪除大量文件
echo file{1..524288} |xargs rm
“`
##### 軟硬鏈接
“`
鏈接數含義:同一個文件有幾個名,硬鏈接鏈接一次鏈接數增加一次,而軟鏈接不增加。
硬鏈接機制:一個節點編號,多個文件名
軟硬鏈接本質區別:
硬鏈接:同一個文件,多個名字
軟鏈接:一個文件對應快捷方式,不同文件
硬鏈接特點:
1.鏈接文件彼此獨立平等,刪除一個文件只是刪除一個名字,還可通過其他文件名訪問
2.不可以跨分區鏈接(硬鏈接原理是一個節點,如果跨分區了,可能出現多個節點)
3.不能對目錄創建
4.硬鏈接鏈接次數會增加
5.刪除原始文件,新鏈接文件不受影響
6.硬鏈接大小由文件由原文件大小決定
軟鏈接特點:
1.軟鏈接依賴原始文件
2.可以跨分區
3.可對目錄創建
4.鏈接數不增加
5.刪除原始文件,鏈接失效
6.軟鏈接的大小由原始文件的路徑決定
注:創建軟鏈接路徑,相對路徑不能使相對當前目錄路徑,而是相對軟鏈接的路徑
例:
[root@centos7 11:13:54 data]#ln -s ../data/fi /root/link2
[root@centos7 11:14:54 data]#ll /root/link2
lrwxrwxrwx. 1 root root 10 Mar 31 11:14 /root/link2 -> ../data/fi
“`
##### 命令readlink(查看鏈接對應的原始文件)
“`
例:
[root@centos7 11:30:25 ~]#readlink link2
../data/fi
“`
##### 利用軟鏈接實現軟件部署
“`
假設nginx有兩個版本,nginx1,nginx2,可實現兩個版本相互切換
[root@centos7 11:45:14 data]#ln -s nginx1 nginx
[root@centos7 11:45:37 data]#ll
total 0
lrwxrwxrwx. 1 root root 6 Mar 31 11:45 nginx -> nginx1
drwxr-xr-x. 2 root root 6 Mar 31 11:45 nginx1
drwxr-xr-x. 2 root root 6 Mar 31 11:45 nginx2
[root@centos7 11:46:06 data]#rm nginx
[root@centos7 11:46:55 data]#ln -s nginx2 nginx
[root@centos7 11:47:00 data]#ll
total 0
lrwxrwxrwx. 1 root root ?6 Mar 31 11:47 nginx -> nginx2
drwxr-xr-x. 2 root root 20 Mar 31 11:46 nginx1
drwxr-xr-x. 2 root root ?6 Mar 31 11:45 nginx2
“`
##### 命令file
“`
文件可以包含多種類型的數據
檢查文件的類型,然后確定適當的打開命令或應用程序使用
file [options] <filename>…
常用選項:
-b 列出文件辨識結果時,不顯示文件名稱
-f filelist 列出文件filelist中文件名的文件類型
-F 使用指定分隔符號替換輸出文件名后默認的”:”分隔符
-L 查看對應軟鏈接對應文件的文件類型
“`
##### I/O重定
“`
錯誤輸出和正確輸出分別輸入到對應文件中
“`
“`
[root@centos7 12:37:16 data]#ls /erro /data >true 2>erro
[root@centos7 13:41:55 data]#cat true
/data:
erro
link
links
nginx
[root@centos7 13:42:15 data]#cat erro
ls: cannot access /erro: No such file or directory
“`
“`
正確輸出和錯誤輸出都重定向到一個文件中
方法一:
[root@centos7 13:42:24 data]#ls /erro /data >f1 2>&1
[root@centos7 13:46:49 data]#cat f1
ls: cannot access /erro: No such file or directory
/data:
erro
f1
link
links
nginx
“`
“`
方法二:
[root@centos7 13:46:58 data]#ls /erro /data &>f1
[root@centos7 13:53:15 data]#cat f1
ls: cannot access /erro: No such file or directory
/data:
erro
f1
link
links
nginx
“`
“`
禁止覆蓋,強制覆蓋,允許覆蓋
[root@centos7 13:53:24 data]#hostname >f1
[root@centos7 13:56:47 data]#set -C
[root@centos7 13:56:56 data]#hostname >f1
-bash: f1: cannot overwrite existing file
[root@centos7 13:57:07 data]#ls >| f1
[root@centos7 13:57:58 data]#set +C f1
“`
“`
以下四個重定向,哪個與其它三個不同
[root@centos7 13:58:53 data]#ls /erro /data >log1 2>&1
[root@centos7 14:05:51 data]#ls /erro /data 2>&1 >log2
ls: cannot access /erro: No such file or directory
[root@centos7 14:07:39 data]#ls /erro /data &>log3
[root@centos7 14:08:12 data]#ls /erro /data 2>log4 >&2
其中第二個執行結果錯誤輸出未重定向到文件中
“`
“`
cat <f1 >f1 ? ?相當于清空f1
cat <f1 >f2 ? 相當復制f1到f2
cat <f1 >>f2 ? 無限追加f1內容到f2
“`
本文來自投稿,不代表Linux運維部落立場,如若轉載,請注明出處:http://www.www58058.com/94319
贊 (0)
liunx 命令變量
上一篇
2018-04-01
第二周筆記
下一篇
2018-04-01