基础命令
命令历史
-
命令历史的管理
登陆 shell 时,会读取命令历史文件中记录下的命令: ~/.bash_history 。
登陆进 shell 后,新执行的命令只会记录在缓存中,这些命令会在用户退出时追加保存到命令历史文件中。
-
history使用
[root@zze ~]# history 1 cd ~ 2 ll 3 history
[root@zze ~]# history 2 3 history 4 history 2
[root@zze ~]# history -d 4 [root@zze ~]# history 1 cd ~ 2 ll 3 history 4 history -d 4 5 history
[root@zze ~]# history -c [root@zze ~]# history 1 history
-
快捷操作
[root@zze ~]# history 1 history 2 ll 3 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 :忽略重复(连续且相同的命令)。
ignorespace :忽略以空白字符开头的命令。
ignoreboth :忽略以上两项。
修改环境变量值的方式: export 变量名="值" ,默认只对当前会话生效。[root@zze ~]# ls -l /etc/profile -rw-r--r--. 1 root root 2008 Jan 14 05:28 /etc/profile [root@zze ~]# history 1 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/: b a/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.txt 2 directories, 1 file
[root@zze testdir]# tree -d a/ a/ └── b └── c 2 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" ] ; then PATH=$PATH:$1 else PATH=$1:$PATH fi esac } if [ -x /usr/bin/id ]; then if [ -z "$EUID" ]; then # ksh workaround EUID=`id -u` UID=`id -ru` fi USER="`id -un`" LOGNAME=$USER MAIL="/var/spool/mail/$USER" fi # Path manipulation if [ "$EUID" = "0" ]; then pathmunge /sbin pathmunge /usr/sbin pathmunge /usr/local/sbin else pathmunge /usr/local/sbin after pathmunge /usr/sbin after pathmunge /sbin after fi HOSTNAME=`/bin/hostname 2>/dev/null` HISTSIZE=1000 if [ "$HISTCONTROL" = "ignorespace" ] ; then export HISTCONTROL=ignoreboth else export HISTCONTROL=ignoredups fi export 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 -m if [ -f /etc/sysconfig/network ]; then . /etc/sysconfig/network fi if [ -z "$HOSTNAME" -o "$HOSTNAME" = "(none)" ]; then HOSTNAME=localhost fi if [ ! -e /proc/mounts ]; then mount -n -t proc /proc /proc mount -n -t sysfs /sys /sys >/dev/null 2>&1 fi if [ ! -d /proc/bus/usb ]; then modprobe usbcore >/dev/null 2>&1 && mount -n -t usbfs /proc/bus/usb /proc/bus/usb else mount -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/functions PLYMOUTH= [ -x /bin/plymouth ] && PLYMOUTH=yes # Check SELinux status SELINUX_STATE= if [ -e "/selinux/enforce" ] && [ "$(cat /proc/self/attr/current)" != "kernel" ]; then if [ -r "/selinux/enforce" ] ; then SELINUX_STATE=$(cat "/selinux/enforce") else # assume enforcing if you can't read it SELINUX_STATE=1 fi fi if [ -n "$SELINUX_STATE" -a -x /sbin/restorecon ] && __fgrep " /dev " /proc/mounts >/dev/null 2>&1 ; then /sbin/restorecon -R -F /dev 2>/dev/null fi disable_selinux() { echo $"*** Warning -- SELinux is active"
[root@zze temp]# more +5 test.txt 55555555555555555 666666666666666666666 77777777777777777777 88888888888888888888
- 操作方式:
- B :向前翻页,SPACE :向后翻页,RETURN :下一行,Q:退出。
- 比例显示:
- 按字符统计。
-
按页查看-less
按页查看,操作方式和 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 则直接补全。
两次 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 └── b 7 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 └── sbin 5 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