任务:

  1. shell介绍
  1. 命令历史
  2. 命令补全和别名
  3. 通配符
  4. 输入输出重定向

shell介绍
shell是一个命令解释器,提供用户和机器之间的交互
作为命令语言,它交互式解释和执行用户输入的命令或者自动地解释和执行预先设定好的一连串的命令
支持特定的语法,比如逻辑判断,循环等
每个用户都可以有自己的特定shell
centos7默认shell为bash(bourne agin shell)
还有zsh、ksh等

命令历史
history:
作用:显示历史执行过的命令
[root@tk ~]# history 
1    history
2    ls

.bash_history
作用:历史命令存放在这个配置文件里,可以通过cat或vim .bash_history来查看 
[root@tk ~]# vim .bash_history 
cd phpwind/
ls
cd ..

HISTSIZE
作用:内置的环境变量,默认历史命令最大存1000条,可以通过修改环境变量来修改存历史命令的条数
在/etc/profile里面修改,如下:
[root@tk ~]# echo $HISTSIZE # 通过这个命令可以看到最多存1000条
1000
[root@tk ~]# vim /etc/profile #找到HISTSIZE,把后面的数字修改成想要改的数就可以
HOSTNAME=`/usr/bin/hostname 2>/dev/null'
HISTSIZE=1000 #把1000改成想要的就可以,我这里改成2000,改完后要退出终端或者执行这个文件才生效
如下:
[root@tk ~]# source /etc/profile
[root@tk ~]# echo $HISTSIZE
2000

HISTTIMEFORMAT
作用:让历史命令显示的时候有完整的时间格式
问题:history这个命令在看历史记录的时候只显条数,能不能让其在显示的时候把命令操作时间也显示上,
答案是可以的
在/etc/profile配置文件里把HISTTIMEFORMAT="%Y-%m-%d %H:%M:%S"添加到HISISIZE=1000下面就可以 
如下:
[root@tk ~]# history  #没有改变变量时如下显示
1    history 
2    ls
[root@tk ~]# vim /etc/profile  #在HISTSIZE=1000下面添加HISTTIMEFORMAT="%Y-%m-%d %H:%m:%S "
HISTSIZE=1000
HISTTIMEFORMAT="%Y-%m-%d %H:%M:%S " #注意,分号(%S”)后面有一个空格,作用是格式更好看,
如果不加空格会感觉很紧俏
[root@tk ~]# source /etc/profile
[root@tk ~]# history #完整的时间格式
1  2019-11-14 15:11:30 exit
2  2019-11-14 15:12:35 cat /var/log/
3  2019-11-14 15:13:36 which log

history -c
作用:清除当前终端的历史命令,但是不会清除配置文件下保存的命令
[root@tk ~]# history 
1  2019-11-15 12:11:35 history 
2  2019-11-15 13:11:11 vim .bash_history 
3  2019-11-15 14:11:55 history
4  2019-11-15 14:11:19 vim /etc/profile
5  2019-11-15 14:11:50 vim .bash_history 
6  2019-11-15 14:11:51 history 
[root@tk ~]# history -c #清除当前终端历史命令
[root@tk ~]# history  #查看的时候发现只有刚刚执行过的命令
1  2019-11-15 14:11:04 history 
[root@tk ~]# vim .bash_history  #查看配置文件,里面的命令并没有被清除了
ll
service mysqld restart
service phpd restart
ls
cd sh

chattr +a .bash_history
作用:锁定文件,历史命令永久保存,只会追加不会覆盖
[root@tk ~]# chattr +a .bash_history

命令补全
tab键
作用:补全命令或者文件,在centos7 可以补全命令参数但是需要安装bash_completion,安装完后
需要重启系统才生效
[root@tk ~]# ser #如果输入ser按一下tab键就会出来service,作用补全单个没有与之前缀相同的命令
[root@tk ~]# service 
[root@tk ~]# se#如果输入se按两下会出现与之前缀相同的命令,作用是在不知道具体命令怎么拼写
#的时候可以用
[root@tk ~]# se#输入se后按两下tab键会出现如下前缀相同的命令
secon                    selinux_restorecon     setfiles
sed                        semodule                    setfont

别名
alias
作用:把复杂的命令用一个简单命令来命名,让其书写记忆方便,各用户都有配置自己别名的文件 ~/.bashrc
[root@tk ~]#alias network=”systemctl restart network.service”
[root@tk ~]# alias network="systemctl restart network.service"
[root@tk ~]# network
怎么取消自定义的别名  用unalias 来取消  如  unalias network  就可以 
如下:
[root@tk ~]# alias network="systemctl restart network.service"
[root@tk ~]# network
[root@tk ~]# unalias network 
[root@tk ~]# network  #取消后就在运行的时候报错
-bash: network: command not found
Alias定义在哪些文件里呢,在用户的家目录~/.bashrc和/etc/profile.d/ 里,自定义在~/.bashrc里定义
[root@tk ~]# vim .bashrc 
#.bashrc
#user specific aliases and functions
alias rm='rm -i'
alias cp='cp -i'
alias mv='mv -i'
#注:如果要自定义别名永久有效需要到配置文件~/.bashrc里面定义,否则退出终端后自定义的别名失效

通配符
ls *
作用:通配所有    
[root@tk ~]# ls *.txt
1.txt  3.txt  aa.txt  bb.txtab.txt  linux.txt
2.txt  4.txt  a.txt   b.txt         person.txt
ls ?
作用:一个任意的字符
[root@tk ~]# ls ?.txt
1.txt  2.txt  3.txt  4.txt  a.txt  b.txt
Ls [ ] 
作用:表示范围 [0-9a-z]
[root@tk ~]# ls [13].txt
1.txt  3.txt
[root@tk ~]# ls [1-3].txt
1.txt  2.txt  3.txt
[root@tk ~]# ls [0-9a-z].txt
1.txt  2.txt  3.txt  4.txt  a.txt  b.txt
Ls { }
作用:表示范围中的一个 或范围
[root@tk ~]# ls {1,3}.txt
1.txt  3.txt
[root@tk ~]# ls {1,3,a}.txt
1.txt  3.txt  a.txt

输入输出重定向
>   重定向,会覆盖原来的文件
>> 追加,不会覆盖原来的文件
cat 1.txt>2.txt  #重定向 会覆盖原来的文件
[root@tk ~]# echo aa>2.txt 
[root@tk ~]# cat 2.txt 
aa
[root@tk ~]# echo bb>2.txt 
[root@tk ~]# cat 2.txt
bb
cat 1.txt >>2.txt  #追加重定向  不会覆盖原来的文件
[root@tk ~]# cat 2.txt 
bb
aa
[root@tk ~]# echo cc>>2.txt 
[root@tk ~]# cat 2.txt 
bb
aa
cc
Ls aa.txt 2>err  #错误重定向 会把错误的信息重定向到指定文件里
[root@tk ~]# ls aaa.txt 2>1.txt 
[root@tk ~]# cat 1.txt 
ls: 无法访问aaa.txt: 没有那个文件或目录
ls aa.txt 2>>err# 错误追加 
[root@tk ~]# ls aab.txt 2>>1.txt 
[root@tk ~]# cat 1.txt 
ls: 无法访问aaa.txt: 没有那个文件或目录
ls: 无法访问aab.txt: 没有那个文件或目录
&>  #正确和错误重定向
[root@tk ~]# ls 2.txt abc.txt &>1.txt 
[root@tk ~]# cat 1.txt 
ls: 无法访问abc.txt: 没有那个文件或目录
2.txt 
&>> #正确错误追加
[root@tk ~]# ls 3.txt abcd.txt &>>1.txt 
[root@tk ~]# cat 1.txt 
ls: 无法访问abc.txt: 没有那个文件或目录
2.txt
ls: 无法访问abcd.txt: 没有那个文件或目录
3.txt
command>correct.txt 2>error.txt#正确和错误命令指定到不同文件
[root@tk ~]# ls
1.txt   2.txt   3.txt   4.txt   aa.txt
correct.txt     error.txt
[root@tk ~]# ls 2.txt 3.txt aa.txt bbb.txt >correct.txt 2>error.txt  
[root@tk ~]# cat correct.txt 
2.txt
3.txt
aa.txt
[root@tk ~]# cat error.txt 
ls: 无法访问bbb.txt: 没有那个文件或目录
[root@tk ~]# cat passwd 
wc -l < 1.txt    输入重定向 查看行数  左边要是命令 
[root@tk ~]# cp /etc/passwd ./
[root@tk ~]# wc -l < passwd 
25
wc -l >1.txt # 输出 重定向  左边要是命令 
[root@tk ~]# wc -l passwd >passwd 
[root@tk ~]# cat passwd 
0 passwd

以上内容来自陶奎个人博客,初学者,欢迎一块交流:

http://linuxstudying.cn https://blog.51cto.com/linuxstudying