目录
Bash的特性:
支持命令历史、命令补全、路径补全
[root@sh ~]# history
1 history
[root@sh ~]# e
e2freefrag eject esac
e2fsck elfedit ether-wake
e2image elif ethtool
e2label else eval
e2undo enable ex
e4defrag env exec
eapol_test envsubst exit
ebtables eqn expand
ebtables-restore era_check export
ebtables-save era_dump expr
echo era_invalidate
egrep era_restore
[root@sh ~]# echo "sss"
sss
[root@sh ~]# cd /etc/sysc
sysconfig/ sysctl.conf sysctl.d/
[root@sh ~]# cd /etc/sysc
sysconfig/ sysctl.conf sysctl.d/
[root@sh ~]# cd /etc/sysconfig/
支持管道、重定向
[root@sh ~]# cat anaconda-ks.cfg | head -1
#version=DEVEL
[root@sh ~]# cat anaconda-ks.cfg | head -1 > abc
[root@sh ~]# cat abc
#version=DEVEL
支持命令别名
[root@sh ~]# alias sh='cd /etc/sysconfig/'
[root@sh ~]# sh
[root@sh sysconfig]#
支持命令行编辑
支持命令行展开
[root@sh ~]# mkdir -p a/{b/{ac,c},c/{bc,c},d/{dc,c}}
[root@sh ~]# tree a
a
├── b
│ ├── ac
│ └── c
├── c
│ ├── bc
│ └── c
└── d
├── c
└── dc
9 directories, 0 files
支持文件名通配
支持变量
[root@sh ~]# a=10
[root@sh ~]# echo $a
10
支持编程
Bash支持的引号
名称:反引号
符号:``
作用:用于命令替换
效果:
[root@sh ~]# ls
a abc anaconda-ks.cfg common data storage zcyh
[root@sh ~]# echo `ls`
a abc anaconda-ks.cfg common data storage zcyh
名称:单引号
符号:’’
作用:表达强引用,不完成变量替换
效果:
[root@sh ~]# a=10
[root@sh ~]# echo '$a'
$a
名称:双引号
符号:""
作用:表达弱引用,完成变量替换
效果:
[root@sh ~]# a=20
[root@sh ~]# echo "$a"
20
Bash常用技巧光标跳转
Ctrl+a 跳转到命令行首
Ctrl+e 跳转到命令行尾
Ctrl+u 删除光标到命令行首的字符
Ctrl+k 删除光标到命令行尾的字符
Ctrl+<-- 跳转到光标前面一个字符
Ctrl+l 清屏
Bash之命令历史
名称:history
常用选项:
-c:清空命令历史
-d[n]:删除第n条命令历史
-w:保存所有命令历史到历史文件中
[root@sh ~]# history
1 ls
2 history
[root@sh ~]# history -c
[root@sh ~]# history
1 history
[root@sh ~]# ls
a abc anaconda-ks.cfg common data storage zcyh
[root@sh ~]# echo "123456"
123456
[root@sh ~]# history
1 history
2 ls
3 echo "123456"
4 history
[root@sh ~]# history -d2
[root@sh ~]# history
1 history
2 echo "123456"
3 history
4 history -d2
5 history
[root@sh ~]# history -w
[root@sh ~]# cat .
./ .bash_history .mysql_history
../ .bash_logout .tcshrc
.apache.sh.swn .bash_profile .viminfo
.apache.sh.swo .bashrc .zcyh.sh.swp
.apache.sh.swp .cshrc
[root@sh ~]# cat .bash_history
history
echo "123456"
history
history -d2
history
history -w
常用选项二:
!n:执行命令历史中第n条命令
!-n:执行命令历史中倒数第n条命令
!!:执行上一条命令
!string:执行命令历史中最近一个以string开头的命令
!$:引用前一条命令的最后一个参数
esc . :按下ESC松开后立即按.键 引用前一条命令的最后一个参数
[root@sh ~]# history
1 history
2 echo "123456"
3 history
4 history -d2
5 history
6 history -w
7 cat .bash_history
8 history
9 echo "123456"
10 history
11 ls
12 history
[root@sh ~]# !2
echo "123456"
123456
[root@sh ~]# history
1 history
2 echo "123456"
3 history
4 history -d2
5 history
6 history -w
7 cat .bash_history
8 history
9 echo "123456"
10 history
11 ls
12 history
13 echo "123456"
14 history
[root@sh ~]# !-4
ls
a abc anaconda-ks.cfg common data storage zcyh
[root@sh ~]# !!
ls
a abc anaconda-ks.cfg common data storage zcyh
[root@sh ~]# !cat
cat .bash_history
history
echo "123456"
history
history -d2
history
history -w
[root@sh ~]# !$
.bash_history
-bash: .bash_history: 未找到命令
[root@sh ~]# .bash_history
Bash环境变量介绍
PATH:命令搜索路径
HISTSIZE:定义命令历史最多能存多少条,默认为1000条
SHELL:当前系统使用的shell
命令类型
内部命令:shell内置
外部命令:在文件系统的某个路径下有一个与命令名称相对的可执行文件
[root@sh ~]# type ls
ls 是 `ls --color=auto' 的别名
[root@sh ~]# type cd
cd 是 shell 内嵌
基础命令
名称:ls
作用:用于列出指定目录的内容
常用选项:
-l:长格式显示
-h:做单位转换
-a:显示隐藏目录
-d:显示目录自身属性
-t:以时间排序
-r:逆序显示
[root@sh ~]# ls -l
总用量 8
drwxr-xr-x. 5 root root 33 9月 7 14:21 a
-rw-r--r--. 1 root root 15 9月 7 14:15 abc
-rw-------. 1 root root 1435 9月 3 11:33 anaconda-ks.cfg
drwxr-sr-x. 2 root admin 6 9月 5 18:51 common
drwxr-S---. 2 admin1 admin1 6 9月 5 18:51 data
drwxrwxrwt. 2 root root 6 9月 5 19:05 storage
drwxr-xr-x. 2 root root 80 9月 5 12:08 zcyh
[root@sh ~]# ls -lh
总用量 8.0K
drwxr-xr-x. 5 root root 33 9月 7 14:21 a
-rw-r--r--. 1 root root 15 9月 7 14:15 abc
-rw-------. 1 root root 1.5K 9月 3 11:33 anaconda-ks.cfg
drwxr-sr-x. 2 root admin 6 9月 5 18:51 common
drwxr-S---. 2 admin1 admin1 6 9月 5 18:51 data
drwxrwxrwt. 2 root root 6 9月 5 19:05 storage
drwxr-xr-x. 2 root root 80 9月 5 12:08 zcyh
[root@sh ~]# ls -a
. .apache.sh.swo common .viminfo
.. .apache.sh.swp .cshrc zcyh
a .bash_history data .zcyh.sh.swp
abc .bash_logout .mysql_history
anaconda-ks.cfg .bash_profile storage
.apache.sh.swn .bashrc .tcshrc
[root@sh ~]# ls -lhd a
drwxr-xr-x. 5 root root 33 9月 7 14:21 a
[root@sh ~]# ls -t
a abc storage data common zcyh anaconda-ks.cfg
[root@sh ~]# ls -lht
总用量 8.0K
drwxr-xr-x. 5 root root 33 9月 7 14:21 a
-rw-r--r--. 1 root root 15 9月 7 14:15 abc
drwxrwxrwt. 2 root root 6 9月 5 19:05 storage
drwxr-S---. 2 admin1 admin1 6 9月 5 18:51 data
drwxr-sr-x. 2 root admin 6 9月 5 18:51 common
drwxr-xr-x. 2 root root 80 9月 5 12:08 zcyh
-rw-------. 1 root root 1.5K 9月 3 11:33 anaconda-ks.cfg
[root@sh ~]# ls -lhr
总用量 8.0K
drwxr-xr-x. 2 root root 80 9月 5 12:08 zcyh
drwxrwxrwt. 2 root root 6 9月 5 19:05 storage
drwxr-S---. 2 admin1 admin1 6 9月 5 18:51 data
drwxr-sr-x. 2 root admin 6 9月 5 18:51 common
-rw-------. 1 root root 1.5K 9月 3 11:33 anaconda-ks.cfg
-rw-r--r--. 1 root root 15 9月 7 14:15 abc
drwxr-xr-x. 5 root root 33 9月 7 14:21 a
名称:cd
作用:切换目录
[root@sh ~]# cd a
[root@sh a]# cd
[root@sh ~]#
名称:pwd
作用:打印当前目录
[root@sh ~]# pwd
/root
名称:mkdir
作用:创建目录
选项:
-p:创建目录时若父目录不在则自动创建
-v:显示过程
[root@sh ~]# mkdir -pv q/{a,b}
mkdir: 已创建目录 "q"
mkdir: 已创建目录 "q/a"
mkdir: 已创建目录 "q/b"
[root@sh ~]# tree q
q
├── a
└── b
2 directories, 0 files
名称:touch
作用:创建空文件或文件更新时间戳
[root@sh ~]# touch we
[root@sh ~]# ls -lh we
-rw-r--r--. 1 root root 0 9月 7 15:16 we
[root@sh ~]# touch we
[root@sh ~]# ls -lh we
-rw-r--r--. 1 root root 0 9月 7 15:17 we
名称:stat
作用:显示文件或文件系统状态
[root@sh ~]# stat we
文件:"we"
大小:0 块:0 IO 块:4096 普通空文件
设备:fd00h/64768d Inode:33662161 硬链接:1
权限:(0644/-rw-r--r--) Uid:( 0/ root) Gid:( 0/ root)
环境:unconfined_u:object_r:admin_home_t:s0
最近访问:2019-09-07 15:17:13.124812387 +0800
最近更改:2019-09-07 15:17:13.124812387 +0800
最近改动:2019-09-07 15:17:13.124812387 +0800
创建时间:-
名称:rm
作用:删除文件
常用选项:
-r:递归删除
-f:强制删除
[root@sh ~]# ls
a abc anaconda-ks.cfg common data q storage we zcyh
[root@sh ~]# rm -rf a
[root@sh ~]# ls
abc anaconda-ks.cfg common data q storage we zcyh
名称:cp
作用:复制文件
常用选项:
-a:归档复制
-r:递归拷贝
-p:拷贝是保留权限
[root@sh ~]# ls
abc anaconda-ks.cfg common data q storage we zcyh
[root@sh ~]# cat abc
#version=DEVEL
[root@sh ~]# cp -a abc q
[root@sh ~]# cat q/abc
#version=DEVEL
[root@sh ~]# cp -r q data/
[root@sh ~]# ll data/
总用量 0
drwxr-sr-x. 4 root admin1 35 9月 7 15:20 q
[root@sh ~]# tree data/
data/
└── q
├── a
├── abc
└── b
3 directories, 1 file
[root@sh ~]# ll
总用量 8
-rw-r--r--. 1 root root 15 9月 7 14:15 abc
-rw-------. 1 root root 1435 9月 3 11:33 anaconda-ks.cfg
drwxr-sr-x. 2 root admin 6 9月 5 18:51 common
drwxr-S---. 3 admin1 admin1 15 9月 7 15:20 data
drwxr-xr-x. 4 root root 35 9月 7 15:18 q
drwxrwxrwt. 2 root root 6 9月 5 19:05 storage
-rw-r--r--. 1 root root 0 9月 7 15:17 we
drwxr-xr-x. 2 root root 80 9月 5 12:08 zcyh
[root@sh ~]# cp -rp data q
[root@sh ~]# ll q
总用量 4
drwxr-xr-x. 2 root root 6 9月 7 15:13 a
-rw-r--r--. 1 root root 15 9月 7 14:15 abc
drwxr-xr-x. 2 root root 6 9月 7 15:13 b
drwxr-S---. 3 admin1 admin1 15 9月 7 15:20 data
名称:mv
作用:移动文件
[root@sh ~]# ls
abc anaconda-ks.cfg common data q storage we zcyh
[root@sh ~]# mv q /data
[root@sh ~]# ls
abc anaconda-ks.cfg common data storage we zcyh
[root@sh ~]# ls data/
q
名称:cat
作用:拼接文件内容并输出至标准输出
常用选项:
-n:显示行号
[root@sh ~]# cat -n anaconda-ks.cfg
1 #version=DEVEL
2 # System authorization information
3 auth --enableshadow --passalgo=sha512
4 repo --name="Server-HighAvailability" --baseurl=file:///run/install/repo/addons/HighAvailability
5 repo --name="Server-ResilientStorage" --baseurl=file:///run/install/repo/addons/ResilientStorage
6 # Use CDROM installation media
7 cdrom
8 # Use graphical install
9 graphical
10 # Run the Setup Agent on first boot
名称:tac
作用:连接文件并倒序打印内容到标准输出
[root@sh ~]# tac anaconda-ks.cfg
%end
pwpolicy luks --minlen=6 --minquality=1 --notstrict --nochanges --notempty
pwpolicy user --minlen=6 --minquality=1 --notstrict --nochanges --emptyok
pwpolicy root --minlen=6 --minquality=1 --notstrict --nochanges --notempty
%anaconda
%end
名称:more
作用:全屏查看文本内容,只能从前往后看,看完自动退出
#version=DEVEL
# System authorization information
auth --enableshadow --passalgo=sha512
repo --name="Server-HighAvailability" --baseurl=file:///run/inst
all/repo/addons/HighAvailability
repo --name="Server-ResilientStorage" --baseurl=file:///run/inst
--More--(17%)
名称:less
作用:全屏查看文件内容,随便看,不会退出
#version=DEVEL
# System authorization information
auth --enableshadow --passalgo=sha512
repo --name="Server-HighAvailability" --baseurl=file:///run/install/repo/addons/HighAvailability
repo --name="Server-ResilientStorage" --baseurl=file:///run/instanaconda-ks.cfg
名称:head
作用:从文件首部开始打印文件内容
常用选项:
-n[n]:查看前n行内容
[root@sh ~]# cat -n anaconda-ks.cfg | head -n10
1 #version=DEVEL
2 # System authorization information
3 auth --enableshadow --passalgo=sha512
4 repo --name="Server-HighAvailability" --baseurl=file:///run/install/repo/addons/HighAvailability
5 repo --name="Server-ResilientStorage" --baseurl=file:///run/install/repo/addons/ResilientStorage
6 # Use CDROM installation media
7 cdrom
8 # Use graphical install
9 graphical
10 # Run the Setup Agent on first boot
名称:tail
作用:从文件尾部开始打印文件内容
常用选项:
-n[n]:查看尾部n行内容
-f:实时查看文件更新
[root@sh ~]# cat -n anaconda-ks.cfg | tail -n10
41
42 %addon com_redhat_kdump --enable --reserve-mb='auto'
43
44 %end
45
46 %anaconda
47 pwpolicy root --minlen=6 --minquality=1 --notstrict --nochanges --notempty
48 pwpolicy user --minlen=6 --minquality=1 --notstrict --nochanges --emptyok
49 pwpolicy luks --minlen=6 --minquality=1 --notstrict --nochanges --notempty
[root@hh ~]# echo "hao" > we
[root@hh ~]#
[root@sh ~]# tail -f we
aaaaaaa
hao
名称:wc
作用:文本统计
常用选项:
-c:统计文本字节数
-w:统计文本单词数
-l:统计文本行数
[root@hh ~]# wc -cwl anaconda-ks.cfg
50 127 1435 anaconda-ks.cfg
名称:du
作用:查看文件的目录占用的磁盘大小
常用选项:
-s:显示总的占用空间大小
-h:单位转换
[root@hh ~]# du -s anaconda-ks.cfg
4 anaconda-ks.cfg
[root@hh ~]# du -sh anaconda-ks.cfg
4.0K anaconda-ks.cfg
名称:df
作用:报告文件系统磁盘空间的使用情况
-h:单位转换
[root@hh ~]# df -h
文件系统 容量 已用 可用 已用% 挂载点
/dev/mapper/rhel-root 17G 4.2G 13G 25% /
devtmpfs 1.9G 0 1.9G 0% /dev
tmpfs 1.9G 0 1.9G 0% /dev/shm
tmpfs 1.9G 8.6M 1.9G 1% /run
tmpfs 1.9G 0 1.9G 0% /sys/fs/cgroup
/dev/sdb1 497M 26M 472M 6% /root/common
/dev/sda1 1014M 143M 872M 15% /boot
tmpfs 378M 0 378M 0% /run/user/0
/dev/sr0 3.8G 3.8G 0 100% /mnt
名称:hostname
作用:查看或临时修改主机名,重开终端有效,重启失效
[root@hh ~]# hostname jjyy
[root@hh ~]# bash
[root@jjyy ~]#
名称:hostnamectl
作用:查看或永久修改主机名,重开终端有效,重启依然有效
[root@jjyy ~]# hostnamectl set-hostname sh
[root@jjyy ~]# bash
[root@sh ~]#
名称:clear
作用:清屏
名称:whoami
作用:显示当前登录用户
[root@sh ~]# whoami
root
名称:w:
作用:显示当前在线用户并显示器正在执行的命令
[root@sh ~]# w
w: 60 column window is too narrow
名称:who
作用:显示当前在线用户
[root@sh ~]# who
root pts/0 2019-09-07 16:05 (192.168.120.1)
名称:which
作用:显示指定命令的绝对路径
[root@sh ~]# which ls
alias ls='ls --color=auto'
/usr/bin/ls
名称:cal
作用:打印日历
[root@sh ~]# cal
九月 2019
日 一 二 三 四 五 六
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
名称:ldd
作用:查看指定程序有哪些依赖库文件
linux-vdso.so.1 => (0x00007ffc9efec000)
libtinfo.so.5 => /lib64/libtinfo.so.5 (0x00007f51b4f02000)
libdl.so.2 => /lib64/libdl.so.2 (0x00007f51b4cfe000)
libc.so.6 => /lib64/libc.so.6 (0x00007f51b493a000)
/lib64/ld-linux-x86-64.so.2 (0x0000556c8b0d5000)
名称:date
作用:显示或设置日期与时间
常用选项:
-s:以字符串方式设置时间
[root@sh ~]# date
2019年 09月 07日 星期六 16:14:09 CST
[root@sh ~]# date -s 2000
2019年 09月 07日 星期六 20:00:00 CST
[root@sh ~]# date -s 1615
2019年 09月 07日 星期六 16:15:00 CST