基礎命令
命令歷史
-
命令歷史的管理
登陸 shell 時,會讀取命令歷史文件中記錄下的命令: ~/.bash_history 。
登陸進 shell 后,新執行的命令只會記錄在緩存中,這些命令會在用戶退出時追加保存到命令歷史文件中。
-
history使用
[root@zze ~]# history1 cd ~2 ll3 history
[root@zze ~]# history 23 history4 history 2
[root@zze ~]# history -d 4 [root@zze ~]# history1 cd ~2 ll3 history4 history -d 45 history
[root@zze ~]# history -c [root@zze ~]# history1 history
-
快捷操作
[root@zze ~]# history1 history2 ll3 history [root@zze ~]# !2 ll total 20 -rw-------. 1 root root 1197 Jan 14 04:52 anaconda-ks.cfg -rw-r--r--. 1 root root 9458 Jan 14 04:52 install.log -rw-r--r--. 1 root root 3161 Jan 14 04:51 install.log.syslog
[root@zze ~]# !l ll total 20 -rw-------. 1 root root 1197 Jan 14 04:52 anaconda-ks.cfg -rw-r--r--. 1 root root 9458 Jan 14 04:52 install.log -rw-r--r--. 1 root root 3161 Jan 14 04:51 install.log.syslog
[root@zze ~]# !! ll total 20 -rw-------. 1 root root 1197 Jan 14 04:52 anaconda-ks.cfg -rw-r--r--. 1 root root 9458 Jan 14 04:52 install.log -rw-r--r--. 1 root root 3161 Jan 14 04:51 install.log.syslog
[root@zze ~]# ls -l /etc/profile -rw-r--r--. 1 root root 2008 Jan 14 05:28 /etc/profile [root@zze ~]# cat !$
使用?ESC,.?可直接顯示上一條命令參數。 -
控制命令的記錄方式
命令的記錄方式通過環境變量?HISTCONTROL?控制,可選三個值:
ignoredups?:忽略重復(連續且相同的命令)。
shell常用的命令。ignorespace?:忽略以空白字符開頭的命令。
ignoreboth?:忽略以上兩項。
修改環境變量值的方式:?export 變量名="值"?,默認只對當前會話生效。[root@zze ~]# ls -l /etc/profile -rw-r--r--. 1 root root 2008 Jan 14 05:28 /etc/profile [root@zze ~]# history1 history
-
相關環境變量
HISTSIZE?:命令歷史記錄的條數。
HISTFILE?:命令歷史文件路徑,?~/.bash_history?。
HISTFILESIZE?:命令歷史文件記錄歷史的條數。
目錄操作
-
切換目錄-cd
[root@zze etc]# cd /etc/ [root@zze etc]# pwd /etc
[root@zze etc]# cd .. [root@zze /]#
-
顯示當前所在路徑-pwd
[root@zze ~]# pwd /root
-
顯示文件-ls
[root@zze ~]# ls -a . .. anaconda-ks.cfg .bash_history .bash_logout .bash_profile .bashrc .cache .cshrc install.log install.log.syslog .mysql_history .mysql_secret .oracle_jre_usage .pip .rediscli_history .tcshrc
[root@zze ~]# ls -A anaconda-ks.cfg .bash_history .bash_logout .bash_profile .bashrc .cache .cshrc install.log install.log.syslog .mysql_history .mysql_secret .oracle_jre_usage .pip .rediscli_history .tcshrc
[root@zze ~]# ls -l total 20 -rw-------. 1 root root 1197 Jan 14 04:52 anaconda-ks.cfg -rw-r--r--. 1 root root 9458 Jan 14 04:52 install.log -rw-r--r--. 1 root root 3161 Jan 14 04:51 install.log.syslog
[root@zze ~]# ls -lh total 20K -rw-------. 1 root root 1.2K Jan 14 04:52 anaconda-ks.cfg -rw-r--r--. 1 root root 9.3K Jan 14 04:52 install.log -rw-r--r--. 1 root root 3.1K Jan 14 04:51 install.log.syslog [root@zze ~]# ll -h total 20K -rw-------. 1 root root 1.2K Jan 14 04:52 anaconda-ks.cfg -rw-r--r--. 1 root root 9.3K Jan 14 04:52 install.log -rw-r--r--. 1 root root 3.1K Jan 14 04:51 install.log.syslog
[root@zze local]# ls -ld /etc/ drwxr-xr-x. 55 root root 4096 Jan 14 06:35 /etc/ [root@zze local]# ll -d /etc/ drwxr-xr-x. 55 root root 4096 Jan 14 06:35 /etc/ [root@zze local]# ll -d drwxr-xr-x. 15 root root 4096 Jan 14 05:46 .
[root@zze local]# ls bin etc games include java lib lib64 libexec redis sbin share src tomcat [root@zze local]# ls -r tomcat src share sbin redis libexec lib64 lib java include games etc bin
[root@zze ~]# ls -R /root/ /root/: anaconda-ks.cfg install.log install.log.syslog
-
創建目錄-mkdir
[root@zze ~]# mkdir testdir [root@zze ~]# ls anaconda-ks.cfg install.log install.log.syslog testdir
[root@zze testdir]# mkdir a/b mkdir: cannot create directory `a/b': No such file or directory [root@zze testdir]# mkdir -p a/b [root@zze testdir]# ls -R .: a./a: b
[root@zze testdir]# ll total 0 [root@zze testdir]# mkdir -pv a/b mkdir: created directory `a' mkdir: created directory `a/b'
[root@zze testdir]# mkdir -m 664 a [root@zze testdir]# ll total 4 drw-rw-r--. 2 root root 4096 Jan 15 11:51 a
[root@zze testdir]# mkdir a/ a/b a/b/c [root@zze testdir]# tree a/ a/ └── b└── c
-
刪除目錄-rmdir
[root@zze testdir]# ls -R a/ a/: ba/b: [root@zze testdir]# rmdir a/ rmdir: failed to remove `a/': Directory not empty [root@zze testdir]# rmdir a/b/ [root@zze testdir]# rmdir a/ [root@zze testdir]# ll total 0
[root@zze testdir]# rmdir -p a/b/c/ [root@zze testdir]# ll total 0
-
樹狀顯示目錄結構-tree
[root@zze testdir]# tree a/ a/ └── b├── c└── test.txt2 directories, 1 file
[root@zze testdir]# tree -d a/ a/ └── b└── c2 directories
[root@zze testdir]# tree -L 1 a/ a/ └── b
查看文本文件
-
正序輸出文件內容-cat
[root@zze testdir]# cat test.txt 1 2 3 4 5 6 7 8 9
-
反序輸出文件內容-tac
[root@zze testdir]# tac test.txt 9 8 7 6 5 4 3 2 1
-
按頁查看-more
[root@zze ~]# more /etc/profile # /etc/profile# System wide environment and startup programs, for login setup # Functions and aliases go in /etc/bashrc# It's NOT a good idea to change this file unless you know what you # are doing. It's much better to create a custom.sh shell script in # /etc/profile.d/ to make custom changes to your environment, as this # will prevent the need for merging in future updates.pathmunge () {case ":${PATH}:" in*:"$1":*);;*)if [ "$2" = "after" ] ; thenPATH=$PATH:$1elsePATH=$1:$PATHfiesac }if [ -x /usr/bin/id ]; thenif [ -z "$EUID" ]; then# ksh workaroundEUID=`id -u`UID=`id -ru`fiUSER="`id -un`"LOGNAME=$USERMAIL="/var/spool/mail/$USER" fi# Path manipulation if [ "$EUID" = "0" ]; thenpathmunge /sbinpathmunge /usr/sbinpathmunge /usr/local/sbin elsepathmunge /usr/local/sbin afterpathmunge /usr/sbin afterpathmunge /sbin after fiHOSTNAME=`/bin/hostname 2>/dev/null` HISTSIZE=1000 if [ "$HISTCONTROL" = "ignorespace" ] ; thenexport HISTCONTROL=ignoreboth elseexport HISTCONTROL=ignoredups fiexport PATH USER LOGNAME MAIL HOSTNAME HISTSIZE HISTCONTROL --More--(63%)
[root@zze ~]# more -d /etc/rc.d/rc.sysinit #!/bin/bash # # /etc/rc.d/rc.sysinit - run once at boot time # # Taken in part from Miquel van Smoorenburg's bcheckrc. #HOSTNAME=$(/bin/hostname)set -mif [ -f /etc/sysconfig/network ]; then. /etc/sysconfig/network fi if [ -z "$HOSTNAME" -o "$HOSTNAME" = "(none)" ]; thenHOSTNAME=localhost fiif [ ! -e /proc/mounts ]; thenmount -n -t proc /proc /procmount -n -t sysfs /sys /sys >/dev/null 2>&1 fi if [ ! -d /proc/bus/usb ]; thenmodprobe usbcore >/dev/null 2>&1 && mount -n -t usbfs /proc/bus/usb /proc/bus/usb elsemount -n -t usbfs /proc/bus/usb /proc/bus/usb fi#remount /dev/shm to set attributes from fstab #669700 mount -n -o remount /dev/shm >/dev/null 2>&1 #remount /proc to set attributes from fstab #984003 mount -n -o remount /proc >/dev/null 2>&1. /etc/init.d/functionsPLYMOUTH= [ -x /bin/plymouth ] && PLYMOUTH=yes# Check SELinux status SELINUX_STATE= if [ -e "/selinux/enforce" ] && [ "$(cat /proc/self/attr/current)" != "kernel" ]; thenif [ -r "/selinux/enforce" ] ; thenSELINUX_STATE=$(cat "/selinux/enforce")else# assume enforcing if you can't read itSELINUX_STATE=1fi fiif [ -n "$SELINUX_STATE" -a -x /sbin/restorecon ] && __fgrep " /dev " /proc/mounts >/dev/null 2>&1 ; then/sbin/restorecon -R -F /dev 2>/dev/null fidisable_selinux() {echo $"*** Warning -- SELinux is active"
[root@zze temp]# more +5 test.txt 55555555555555555 666666666666666666666 77777777777777777777 88888888888888888888
- 操作方式:
- B :向前翻頁,SPACE :向后翻頁,RETURN :下一行,Q:退出。
- 比例顯示:
- 按字符統計。
-
按頁查看-less
shell和bash、按頁查看,操作方式和 man 一致,因為 man 內部實際上也是使用 less 打開文本文件。
- 操作:
- Space,^V,^F:向文件尾部翻屏。
- b,^B:向文件首部翻屏。
- d,^D:向文件尾部翻半屏。
- u,^U:向文件首部翻半屏。
- Return,^N,e,^E,j,^J:向文件尾部翻一行。
- y,^Y,^P,k,^K:向文件首部翻一行。
- q:退出。
- #:跳轉到第 # 行。
- 1G:回到文件首部。
- G:跳至文件尾部。
- /KEYWORD:以 KEYWORD 指定的字符串為關鍵字,從當前位置向文件尾部搜索,不區分大小寫。n:下一個,N:上一個。
- ?KEYWORD:以 KEYWORD 指定的字符串為關鍵字,從當前位置向文件首部搜索,不區分大小寫。n:下一個,N:上一個。
-
查看文件頭部-head
[root@zze temp]# head test.txt 11111111111111111111111111111111111 22222222222222222222222222222222222 333333333333333 4444444444444444444 55555555555555555 666666666666666666666 77777777777777777777 88888888888888888888 9999999999 10
[root@zze temp]# head -c 10 test.txt 1111111111
[root@zze temp]# head -n 5 test.txt 11111111111111111111111111111111111 22222222222222222222222222222222222 333333333333333 4444444444444444444 55555555555555555
-
?查看文件尾部-tail
[root@zze temp]# tail test.txt 11 1222222222222222 1333333333333333 1444444444444 15 16 177777777 18 19 2000
[root@zze temp]# tail -c 10 test.txt 8 19 2000
[root@zze temp]# tail -n 5 test.txt 16 177777777 18 19 2000
[root@zze temp]# tail -f test.txt 11 1222222222222222 1333333333333333 1444444444444 15 16 177777777 18 19 2000 new line
文件時間戳管理工具
-
數據
文件的數據分為元數據和內容數據,元數據可以看做文件的狀態描述信息,內容數據則是文件的真實內容。
-
查看文件狀態-stat
[root@zze temp]# stat test.txt File: `test.txt' Size: 292 Blocks: 8 IO Block: 4096 regular file Device: fd00h/64768d Inode: 1966102 Links: 1 Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root) Access: 2019-01-13 09:57:08.339394446 +0800 Modify: 2019-01-13 09:57:08.339394446 +0800 Change: 2019-01-13 09:57:08.339394446 +0800
- 三個時間戳:
-
access time (atime):訪問時間,讀取文件內容時。
modify time (mtime):修改時間,修改文件內容時。
change time (ctime):改變時間,元數據發生改變。
-
-
修改時間戳-touch
[root@zze temp]# stat test.txt File: `test.txt' Size: 292 Blocks: 8 IO Block: 4096 regular file Device: fd00h/64768d Inode: 1966102 Links: 1 Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root) Access: 2019-01-13 09:57:08.339394446 +0800 Modify: 2019-01-13 09:57:08.339394446 +0800 Change: 2019-01-13 09:57:08.339394446 +0800 [root@zze temp]# touch test.txt [root@zze temp]# stat test.txt File: `test.txt' Size: 292 Blocks: 8 IO Block: 4096 regular file Device: fd00h/64768d Inode: 1966102 Links: 1 Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root) Access: 2019-01-13 10:10:07.019393669 +0800 Modify: 2019-01-13 10:10:07.019393669 +0800 Change: 2019-01-13 10:10:07.019393669 +0800
[root@zze temp]# touch -m -t 201810101200.59 test.txt [root@zze temp]# stat test.txt File: `test.txt' Size: 292 Blocks: 8 IO Block: 4096 regular file Device: fd00h/64768d Inode: 1966102 Links: 1 Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root) Access: 2019-01-13 10:10:07.019393669 +0800 Modify: 2018-10-10 12:00:59.000000000 +0800 Change: 2019-01-13 10:18:00.999393544 +0800
[root@zze temp]# touch -a -t 201811101200.59 test.txt [root@zze temp]# stat test.txt File: `test.txt' Size: 292 Blocks: 8 IO Block: 4096 regular file Device: fd00h/64768d Inode: 1966102 Links: 1 Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root) Access: 2018-11-10 12:00:59.000000000 +0800 Modify: 2018-10-10 12:00:59.000000000 +0800 Change: 2019-01-13 10:23:43.380393790 +0800
[root@zze temp]# ls test.txt [root@zze temp]# touch a.txt [root@zze temp]# ls a.txt test.txt
[root@zze temp]# touch -c a.txt [root@zze temp]# ls a.txt test.txt [root@zze temp]# touch -c b.txt [root@zze temp]# ls a.txt test.txt
time 的格式為 "yyyyMMddHHmmss.ss" ,例:“2019年1月1號2點35分34秒”為 "201901010235.34"。
bash的基礎特性
補全-TAB鍵
-
命令補全
一次 TAB:如果用戶給定的字符串只有一條唯一對應的命令,一次 TAB 則直接補全。
shell基礎?兩次 TAB:如果用戶給定的字符串開頭對應的命令不唯一,再次 TAB 則會顯示所有以該字符串開頭的命令。
[root@zze ~]# cl clear clock clockdiff cloog
-
路徑補全
把用戶給定的字符串當做路徑開頭,并在其指定目錄下搜索以指定字符串開頭的文件名。如果唯一,則直接補全,否則再次 TAB 顯示列表。
命令行展開
[root@zze testdir]# cd ~
[root@zze ~]#
[root@zze testdir]# cd ~root/
[root@zze ~]#
/tmp/{a,b} -> /tmp/a , /tmp/b
/tmp/{a,b}/c -> /tmp/a/c , /tmp/b/c
[root@zze testdir]# mkdir tmp/x/{y1,y2}/{a,b} mkdir: cannot create directory `tmp/x/y1/a': No such file or directory mkdir: cannot create directory `tmp/x/y1/b': No such file or directory mkdir: cannot create directory `tmp/x/y2/a': No such file or directory mkdir: cannot create directory `tmp/x/y2/b': No such file or directory [root@zze testdir]# mkdir -p tmp/x/{y1,y2}/{a,b} [root@zze testdir]# tree tmp/ tmp/ └── x├── y1│?? ├── a│?? └── b└── y2├── a└── b7 directories, 0 files
練習2:如何一次性創建 'x_m' , 'y_m' , 'x_n' , 'y_n' ?
[root@zze testdir]# mkdir -p {x,y}_{m,n} [root@zze testdir]# ls x_m x_n y_m y_n
練習3:如何一次性創建 'tmp/bin' , 'tmp/sbin' , 'tmp/usr/bin' , 'tmp/usr/sbin' ?
[root@zze testdir]# mkdir -p tmp/{bin,sbin,usr/{bin,sbin}} [root@zze testdir]# tree tmp tmp ├── bin ├── sbin └── usr├── bin└── sbin5 directories, 0 files
命令的執行狀態結果
bash 使用特殊變量?$??保存最近一條命令的執行狀態結果:
[root@zze testdir]# mkdir tmp [root@zze testdir]# echo $? 0 [root@zze testdir]# mkdir tmp mkdir: cannot create directory `tmp': File exists [root@zze testdir]# echo $? 1