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

Shell介绍

  • shell是一个命令解释器,提供用户和机器之间的交互
  • 支持特定语法,好比逻辑判断、循环(if for where)
  • 每一个用户均可以有本身特定的shell
  • CentOS7默认shell为bash(Bourne Agin Shell)
  • 还有zsh、ksh等

命令历史

  • history命令
  • bash_history
  • 最大1000条
  • 变量HISTSIZE
  • /etc/profile中修改
  • HISTTIMEFORMAT="%Y/%m/%d %H:%M:%S "
  • 永久保存 chattr +a ~/.bash_history
  • !!
  • !n
  • !word

1 .history 命令

显示历史命令 后10个shell

[root@yong-02 ~]# history |tail
  327  ls
  328  cd apr-util-1.6.1
  329  ls /usr/local/apache2/
  330  cd
  331  ls
  332  zsh
  333  eix
  334  yum list | grep zsh
  335  history
  336  history |tail

2. echo $HISTSIZE 显示存放最大多少条历史记录

[root@yong-02 ~]# echo $HISTSIZE
1000

3. .bash_history 文件

历史命令保存在用户家目录的.bash_history文件中apache

4. history –c

清空内存中的历史记录;不会清除.bash_history文件中的记录。vim

[root@yong-02 ~]# history -c
[root@yong-02 ~]# history
    1  history
[root@yong-02 ~]# head -10 .bash_history 
ifconfig
ls
ls /tmp
df -h
ls -toot
ls /root
dhclient
ifconfig
ls
ifconfig
  • 只有当用户正常退出当前shell时,在当前shell中运行的命令才会保存至.bash_history文件中。

5. 定义HISTSIZE值,在配置文件/etc/profile中修改

[root@yong-02 ~]# vim /etc/profile
搜索HISTSIZE 默认值是1000,能够修改为5000条,而后按esc,:wq 保存退出。
而后运行命令:source /etc/profile
[root@yong-02 ~]# source /etc/profile
[root@yong-02 ~]# echo $HISTSIZE
5000

6. 定义格式:HISTTIMEFORMAT="%Y/%m/%d %H:%M:%S "

在配置文件/etc/profile中修改,在HISTSIZE=5000下面,添加一行HISTTIMEFORMAT="%Y/%m/%d %H:%M:%S "
输入图片说明bash

[root@yong-02 ~]# vim /etc/profile
[root@yong-02 ~]# source /etc/profile
[root@yong-02 ~]# history
    1  2018/04/19 22:37:56 history
    2  2018/04/19 22:38:18 head -10 .bash_profile 
    3  2018/04/19 22:38:28 head -10 .bash_history 
    4  2018/04/19 22:39:24 vim /etc/profie
    5  2018/04/19 22:39:38 vim /etc/profile
    6  2018/04/19 22:41:23 source /etc/profile
    7  2018/04/19 22:41:37 echo $HISTSIZE
    8  2018/04/19 22:42:15 vim /etc/profile
    9  2018/04/19 22:42:53 source /etc/profile
   10  2018/04/19 22:43:07 history

6. 永久保存 chattr +a ~/.bash_history

  • 给.bash_history加一个a权限,不能删除,能够追加
[root@yong-02 ~]# chattr +a .bash_history 
[root@yong-02 ~]# lsattr .bash_history 
-----a---------- .bash_history

7. !!:连续两个!表示执行上一条命令。

[root@yong-02 ~]# lsattr .bash_history 
-----a---------- .bash_history
[root@yong-02 ~]# !!
lsattr .bash_history 
-----a---------- .bash_history

8. !n:这里的n是数字,表示执行命令历史中的第n条命令。

[root@yong-02 ~]# history
    1  2018/04/19 22:37:56 history
    2  2018/04/19 22:38:18 head -10 .bash_profile 
    3  2018/04/19 22:38:28 head -10 .bash_history 
    4  2018/04/19 22:39:24 vim /etc/profie
    5  2018/04/19 22:39:38 vim /etc/profile
    6  2018/04/19 22:41:23 source /etc/profile
    7  2018/04/19 22:41:37 echo $HISTSIZE
    8  2018/04/19 22:42:15 vim /etc/profile
    9  2018/04/19 22:42:53 source /etc/profile
   10  2018/04/19 22:43:07 history
   11  2018/04/19 22:43:56 chattr +a .bash_history 
   12  2018/04/19 22:44:12 lsattr .bash_history 
   13  2018/04/19 22:45:07 history
   14  2018/04/19 22:45:25 ls
   15  2018/04/19 22:45:28 pws
   16  2018/04/19 22:45:31 pwd
   17  2018/04/19 22:45:48 history
[root@yong-02 ~]# !14
ls
anaconda-ks.cfg         a.txt  c.txt             multi-user.target  reboot.target
apr-util-1.6.1.tar.bz2  b.txt  graphical.target  poweroff.target    rescue.target

9. !字符串: !pw表示执行命令历史中最近一次以pw开头的命令。

[root@yong-02 ~]# !pw
pwd
/root

命令和文件名补全

  • tab键,敲一下,敲两下
  • 参数补全,安装bash-completion
  • alias别名给命令从新起个名字
  • 各用户都有本身配置别名的文件 ~/.bashrc
  • ls /etc/profile.d/
  • 自定义的alias放到~/.bashrc
  1. 按tab键能够帮咱们补全一个指令,一个路径或者一个文件名
  2. Centos6中tab键只能补全自己;Centos7中tab键支持命令参数补全,须要安装一个包bash-completion  重启才能生效
  3. 例如 systemctl restart network.service 
[root@yong-02 ~]# yum install -y bash-completion
重启服务器 reboot

别名

  1. alias也是bash所特有的功能之一;直接执行alias命令会看到目前系统预设的全部别名。
[root@yong-02 ~]# 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'
  1. 用户家目录下.bashrc文件中只配置了几个alias
[root@yong-02 ~]# cat .bashrc 
# .bashrc

# User specific aliases and functions

alias rm='rm -i'
alias cp='cp -i'
alias mv='mv -i'

# Source global definitions
if [ -f /etc/bashrc ]; then
	. /etc/bashrc
fi
  1. 其余的不少别名在/etc/profile.d/目录下,有不少.sh文件中定义;
[root@yong-02 ~]# ls /etc/profile.d/
256term.csh         colorgrep.csh  colorls.sh  less.csh  vim.sh
256term.sh          colorgrep.sh   lang.csh    less.sh   which2.csh
bash_completion.sh  colorls.csh    lang.sh     vim.csh   which2.sh

vim/etc/profile.d/colorls.sh 服务器

通配符

  • ls *.txt
  • ls ?.txt
  • ls [0-9].txt
  • ls {1,2}.txt

*用来匹配零个或多个任意字符

[root@yong-02 test]# ls *.txt
11.txt  1.txt  23.txt  2.txt  3.txt  ab.txt  a.txt  b.txt
[root@yong-02 test]# ls *txt
11.txt  1.txt  23.txt  2.txt  3.txt  ab.txt  a.txt  b.txt
[root@yong-02 test]# ls *txt*
11.txt  1.txt  23.txt  2.txt  3.txt  ab.txt  a.txt  b.txt  cc.txt.bak

?用来匹配一个字符

[root@yong-02 test]# ls ?.txt
1.txt  2.txt  3.txt  a.txt  b.txt

ls [0-3].txt [ ]里面表示范围,只要是在这个范围内都列出来

[root@yong-02 test]# ls [0-3].txt
1.txt  2.txt  3.txt
[root@yong-02 test]# ls [123].txt
1.txt  2.txt  3.txt
[root@yong-02 test]# ls [23].txt
2.txt  3.txt
[root@yong-02 test]# ls [a-z].txt
a.txt  b.txt
[root@yong-02 test]# ls [az].txt
a.txt
[root@yong-02 test]# ls [0-9a-z].txt
1.txt  2.txt  3.txt  a.txt  b.txt

ls {1,3}.txt { }花括号里面要用“,”隔开,1.txt或者3.txt;

[root@yong-02 test]# ls {1,3}.txt
1.txt  3.txt
[root@yong-02 test]# ls {1,3,2,a}.txt
1.txt  2.txt  3.txt  a.txt

输入输出重定向

  • cat 1.txt >2.txt
  • cat 1.txt >> 2.txt
  • ls aaa.txt 2>err
  • ls aaa.txt 2>>err
  • wc -l < 1.txt
  • command >1.txt 2>&1

>输出重定向

[root@yong-02 test]# echo "aabbcc123" >1.txt 
[root@yong-02 test]# cat 1.txt 
aabbcc123

>>追加剧定向

[root@yong-02 test]# echo "56789" >>1.txt 
[root@yong-02 test]# cat 1.txt 
aabbcc123
56789

2> 错误重定向

[root@yong-02 test]# ls ccc.txt 2>2.txt
[root@yong-02 test]# cat 2.txt 
ls: 没法访问ccc.txt: 没有那个文件或目录

2>>错误追加剧定向

[root@yong-02 test]# ls bbb.txt 2>>2.txt
[root@yong-02 test]# cat 2.txt 
ls: 没法访问ccc.txt: 没有那个文件或目录
ls: 没法访问bbb.txt: 没有那个文件或目录

&> == >+2>正确和错误重定向

[root@yong-02 test]# ls 1.txt aaa.txt &>3.txt
[root@yong-02 test]# cat 3.txt 
ls: 没法访问aaa.txt: 没有那个文件或目录
1.txt

&>>

[root@yong-02 test]# ls 1.txt aaa.txt &>>3.txt
[root@yong-02 test]# cat 3.txt 
ls: 没法访问aaa.txt: 没有那个文件或目录
1.txt
ls: 没法访问aaa.txt: 没有那个文件或目录
1.txt

能够将正确输出到一个文件中,错误输入到另外一个文件中,也能够同时输出到一个文件中(用&>)。

[root@yong-02 test]# ls 23.txt aaa.txt > 1.txt 2>2.txt 
[root@yong-02 test]# cat 1.txt 
23.txt
[root@yong-02 test]# cat 2.txt 
ls: 没法访问aaa.txt: 没有那个文件或目录

< 输入重定向

[root@yong-02 test]# wc -l </etc/passwd
25
[root@yong-02 test]# 2.txt < 3.txt
-bash: 2.txt: 未找到命令
左边只能是命令,不能是文件。
相关文章
相关标签/搜索