shell介绍、命令历史、命令补全和别名、通配符、输入输出重定向

2018-10-11  本文已影响0人  XiaoMing丶

目录

一、shell介绍
二、命令历史
三、命令补全和别名
四、通配符
五、输入输出重定向

一、shell介绍

在计算机科学中,Shell俗称壳(用来区别于核),是指“为使用者提供操作界面”的软件(命令解析器)。它类似于[DOS]下的command.com和后来的cmd.exe。它接收用户命令,然后调用相应的应用程序。
用户直接面对的不是计算机硬件而是shell,用户把指令告诉shell,然后shell再传输给系统内核,接着内核再去支配计算机硬件去执行各种操作。

二、命令历史

我们执行过的命令Linux都会记录,预设可以记录1000条历史命令,该数量是由环境变量HISTSIZE进行控制。这些命令保存在用户的家目录的.bash_history文件中。但需要注意的是,只有当用户正常退出当前shell时,在当前shell中运行的命令才会保存至.bash_history文件中。

[root@minglinux-01 ~]# echo $HISTSIZE
1000
[root@minglinux-01 ~]# ls /root/.bash_history 
/root/.bash_history

环境变量HISTSIZE 可以在/etc/profile中修改,在文件中找到HISTSIZE=1000修改后面的数值,更改之后重启终端或者source /etc/profile 才会生效。

[root@minglinux-01 ~]# history 10
  994  reboot
  995  history 10
  996  ls /root/.bash_history 
  997  cat /root/.bash_history 
  998  echo $HISTSIZE
  999  ls /root/.bash_history 
 1000  echo $HISTTIMEFORMATE
 1001  echo $HISTTIMEFORMAT
 1002  history
 1003  history 10
[root@minglinux-01 ~]# HISTTIMEFORMAT="%Y/%m/%d %H:%M:%S"
[root@minglinux-01 ~]# history 10
  996  2018/10/11 22:24:19ls /root/.bash_history 
  997  2018/10/11 22:24:34cat /root/.bash_history 
  998  2018/10/11 22:31:30echo $HISTSIZE
  999  2018/10/11 22:32:19ls /root/.bash_history 
 1000  2018/10/11 22:41:05echo $HISTTIMEFORMATE
 1001  2018/10/11 22:41:10echo $HISTTIMEFORMAT
 1002  2018/10/11 22:43:59history
 1003  2018/10/11 22:44:11history 10
 1004  2018/10/11 22:44:27HISTTIMEFORMAT="%Y/%m/%d %H:%M:%S"
 1005  2018/10/11 22:44:29history 10

这个变量赋值只在当前终端生效,如果想让此环境变量全局生效,需要编辑 /etc/profile把HISTTIMEFORMAT="%Y/%m/%d %H:%M:%S "加到里面。

如果我们想永久保存命令历史,不让别人删除破坏,我们可以使用chattr +a ~/.bash_history指令设置了这个文件的权限只能追加不能删除。

history命令用于显示指定数目的指令命令。该命令单独使用时,显示全部历史命令。加上参数n则显示最近的n条历史命令。

[root@minglinux-01 ~]# history 10
  986  yum install apr apr-util apr-devel
  987  ./config --prefix=/usr/local/apace2
  988  ./configure --prefix=/usr/local/apace2
  989  yum install apr-util
  990  yum install apr-devel
  991  yum install apr
  992  yum install -y gcc
  993  ./configure --prefix=/usr/local/apace2
  994  reboot
  995  history 10

history命令的几个选项:

-c:清空当前历史命令,不会清空历史命令文件。
-a:将历史命令缓冲区中命令写入历史命令文件中。
-r:将历史命令文件中的命令读入当前历史命令缓冲区。
-w:将当前历史命令缓冲区命令写入历史命令文件中。

在命令行中,可以使用符号!执行指定序号的历史命令。!符号常用的应用有以下3个:

!!:连续两个!表示执行上一条指令。示例命令如下:

[root@minglinux /]# ls
bin   data  etc   lib    media  opt   root  sbin  sys  usr
boot  dev   home  lib64  mnt    proc  run   srv   tmp  var
[root@minglinux /]# !!
ls
bin   data  etc   lib    media  opt   root  sbin  sys  usr
boot  dev   home  lib64  mnt    proc  run   srv   tmp  var

!n:n是数字,表示执行命令历史中的第n条指令。示例命令如下:

[root@minglinux /]# history |grep 536
  536  pwd
  539  history |grep 536
[root@minglinux /]# !536
pwd
/

!字符串(字符串大于等于1):例如!pw表示执行命令历史中最近一次以pw开头的命令。示例命令如下:

[root@minglinux /]# !pw
pwd
/

三、命令补全和别名

按tab键可以帮我们补全一个指令、一个路径或者一个文件名。连续按两次tab键,系统则会把所有的命令或者文件名都列出来。

Centos7 支持参数补全,默认不可以,需要执行yum install -y bash-completion 安装相关软件包,安装完成后重启系统才能生效。

alias命令用来设置指令的别名。我们可以使用该命令可以将一些较长的命令进行简化。使用alias时,用户必须使用单引号''将原来的命令引起来,防止特殊字符导致错误。如果不想用了,还可以使用unalias命令解除别名功能。

直接执行alias命令,会看到目前系统预设的别名,如下所示:

[root@minglinux /]# alias
alias cp='cp -i'
alias egrep='egrep --color=auto'
alias fgrep='fgrep --color=auto'
alias grep='grep --color=auto'
alias l.='ls -d .* --color=auto'
alias ll='ls -l --color=auto'
alias ls='ls --color=auto'
alias mv='mv -i'
alias rm='rm -i'
alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'

自定义别名的格式为alias [命令别名]=['具体的命令'],示例命令如下:

[root@minglinux /]# alias ming='pwd'
[root@minglinux /]# ming
/
[root@minglinux /]# unalias ming
[root@minglinux /]# ming
-bash: ming: 未找到命令

alias命令的作用只局限于该次登入的操作。若要每次登入都能够使用这些命令别名,则可将相应的alias命令存放到bash的初始化文件/etc/bashrc中。

四、通配符

在bash下,可以使用*来匹配零个或多个字符,用?匹配一个字符。示例命令如下:

[root@minglinux dir1]# ls
test1  test123  test2  test3
[root@minglinux dir1]# ls test*
test1  test123  test2  test3
[root@minglinux dir1]# ls test?
test1  test2  test3

[]匹配一个范围的字符,如[123]匹配123其中任意一个,[0-9]匹配1至9当中任一个字符。示例命令如下:

[root@minglinux-01 ~]# ls
123         1_soft.txt  anaconda-ks.cfg  dir2  dir4
1_hard.txt  1.txt       dir1             dir3  快捷键.txt
[root@minglinux-01 ~]# ls -d dir[123]
dir1  dir2  dir3
[root@minglinux-01 ~]# ls -d dir[1-9]
dir1  dir2  dir3  dir4

五、输入输出重定向

输入重定向用于改变命令的输入,输出重定向用于改变命令的输出。输入重定向的命令是<,输出重定向的命令是>,还有错误重定向命令2>以及追加重定向命令>>,示例命令如下:

[root@minglinux-01 ~]# touch 2.txt
[root@minglinux-01 ~]# echo "123" > 2.txt 
[root@minglinux-01 ~]# cat 2.txt 
123
[root@minglinux-01 ~]# echo "123" >> 2.txt 
[root@minglinux-01 ~]# cat 2.txt 
123
123
[root@minglinux-01 ~]# ming
-bash: ming: 未找到命令
[root@minglinux-01 ~]# ming 2> 3.txt
[root@minglinux-01 ~]# cat 3.txt 
-bash: ming: 未找到命令
[root@minglinux-01 ~]# ming 2>> 3.txt 
[root@minglinux-01 ~]# cat 3.txt 
-bash: ming: 未找到命令
-bash: ming: 未找到命令
[root@minglinux-01 ~]# ls
123         1.txt  anaconda-ks.cfg  dir3
1_hard.txt  2.txt  dir1             dir4
1_soft.txt  3.txt  dir2             快捷键.txt
[root@minglinux-01 ~]# ls [123].txt abc.txt &> a.txt
[root@minglinux-01 ~]# cat a.txt 
ls: 无法访问abc.txt: 没有那个文件或目录
1.txt
2.txt
3.txt
[root@minglinux-01 ~]# ls [123].txt abc.txt > a.txt 2>b.txt
[root@minglinux-01 ~]# cat a.txt 
1.txt
2.txt
3.txt
[root@minglinux-01 ~]# cat b.txt 
ls: 无法访问abc.txt: 没有那个文件或目录
[root@minglinux-01 ~]# wc -l 2.txt 
2 2.txt
[root@minglinux-01 ~]# wc -l < 2.txt 
2
上一篇下一篇

猜你喜欢

热点阅读