linux学习lesson23

目录

1 shell介绍

2 命令历史

3 命令补全和别名

4 通配符

5 输入输出重定向


1 shell介绍

shell是系统跟计算机硬件交互时使用的中间介质,它只是系统的一个工具。实际上,在shell和计算机硬件之间还有一层东西——系统内核。如果把计算机硬件比作一个人的躯体,那系统内核就是人的大脑。至shell,把它比作人的五官似乎更贴切些。言归正传,用户直接面对的不是计算机硬件而shell,用户把指令告诉shell,然后shell再传输给系统内核,接着内核再去支配计算机硬件去执行各种操作

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

 

2 命令历史

~/.bash_history命令历史记录文件,需要正常退出系统才会记录的

[root@worker1 ~]# cat ~/.bash_history | tail -20
ls
tar -zcvf dir123.tar.gz 123/
ls
tar -xcvf dir123.tar.gz
tar -zxvf dir123.tar.gz
ls
tar -jcvf dir.tar.bz2 dir123
ls
tar -jxvf dir.tar.bz2
ls
clear
ls
tar -Jcvf dir123.tar.xz dir123/
ls
tar -Jxvf dir123.tar.xz dir123/
ls
tar -tf dir123.tar.gz
tar -tf dir.tar.bz2
tar -tf dir123.tar.xz
init 0

变量HISTSIZE是设置记录历史命令的数量(默认是1000)

[root@worker1 ~]# echo $HISTSIZE
1000
  • 当前内存的历史命令
[root@worker1 ~]# history | tail -20
  975  yum install -y gcc
  976  ./configure --prefix=/usr/local/apache2.2
  977  echo $?
  978  make
  979  echo $?
  980  make install
  981  ls /usr/local/apache2.2/
  982  ls
  983  ls /usr/local/apache2.2/
  984  clear
  985  cd
  986  ls ~/.bash_history
  987  cat ~/.bash_history | wc -l | head
  988  cat ~/.bash_history | head -20
  989  cat ~/.bash_history | tail -20
  990  echo #HISTSIZE
  991  echo $HISTSIZE
  992  history
  993  history | head -20
  994  history | tail -20
  • 清除内存历史命令
[root@worker1 ~]# history -c
[root@worker1 ~]# history
    1  history

/etc/profile中可以修改存的命令历史数量

[root@worker1 ~]# vim /etc/profile

    MAIL="/var/spool/mail/$USER"
fi

# Path manipulation
if [ "$EUID" = "0" ]; then
    pathmunge /usr/sbin
    pathmunge /usr/local/sbin
else
    pathmunge /usr/local/sbin after
    pathmunge /usr/sbin after
fi

HOSTNAME=`/usr/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

# By default, we want umask to get set. This sets it for login shell
# Current threshold for system reserved uid/gids is 200
# You could check uidgid reservation validity in
# /usr/share/doc/setup-*/uidgid file
if [ $UID -gt 199 ] && [ "`id -gn`" = "`id -un`" ]; then

其中HISTSIZE=1000就是修改记录历史命令数量 

  • 设置记录历史命令附加上时间

永久生效:
/etc/profile配置文件添加上HISTTIMEFORMAT="%Y/%m/%d %H:%M:%S "
需要退出终端,重新登陆即可生效

格式2:HISTTIMEFORMAT="%Y%m%d %H:%M:%S  "

格式3:HISTTIMEFORMAT="%F %T "

[root@worker1 ~]# history | tail -20
  952  2018/10/10 01:37:55 tar -Jcvf dir123.tar.xz dir123/
  953  2018/10/10 01:37:55 ls
  954  2018/10/10 01:37:55 tar -Jxvf dir123.tar.xz dir123/
  955  2018/10/10 01:37:55 ls
  956  2018/10/10 01:37:55 tar -tf dir123.tar.gz
  957  2018/10/10 01:37:55 tar -tf dir.tar.bz2
  958  2018/10/10 01:37:55 tar -tf dir123.tar.xz
  959  2018/10/10 01:37:55 init 0
  960  2018/10/10 01:37:55 history
  961  2018/10/10 01:37:55 vim /etc/profile
  962  2018/10/10 01:37:55 ls
  963  2018/10/10 01:37:55 history
  964  2018/10/10 01:37:55 logout
  965  2018/10/10 01:37:59 history
  966  2018/10/10 01:38:11 history | tail -20
  967  2018/10/10 01:38:35 vim /etc/profile
  968  2018/10/10 01:38:46 source $!
  969  2018/10/10 01:38:56 source /etc/profile
  970  2018/10/10 01:38:59 history
  971  2018/10/10 01:39:26 history | tail -20

临时生效: 

[root@worker1 ~]# HISTTIMEFORMAT="%Y/%m/%d %H:%M:%S "
[root@worker1 ~]# history | tail -20
  980  2018/10/10 01:40:10   957  2018/10/10 01:37:55 tar -tf dir.tar.bz2
  981  2018/10/10 01:40:10   958  2018/10/10 01:37:55 tar -tf dir123.tar.xz
  982  2018/10/10 01:40:10   959  2018/10/10 01:37:55 init 0
  983  2018/10/10 01:40:10   960  2018/10/10 01:37:55 history
  984  2018/10/10 01:40:10   961  2018/10/10 01:37:55 vim /etc/profile
  985  2018/10/10 01:40:10   962  2018/10/10 01:37:55 ls
  986  2018/10/10 01:40:10   963  2018/10/10 01:37:55 history
  987  2018/10/10 01:40:10   964  2018/10/10 01:37:55 logout
  988  2018/10/10 01:40:10   965  2018/10/10 01:37:59 history
  989  2018/10/10 01:40:10   966  2018/10/10 01:38:11 history | tail -20
  990  2018/10/10 01:40:10   967  2018/10/10 01:38:35 vim /etc/profile
  991  2018/10/10 01:40:10   968  2018/10/10 01:38:46 source $!
  992  2018/10/10 01:40:10   969  2018/10/10 01:38:56 source /etc/profile
  993  2018/10/10 01:40:10   970  2018/10/10 01:38:59 history
  994  2018/10/10 01:40:10   971  2018/10/10 01:39:26 history | tail -20
  995  2018/10/10 01:40:14 HISTTIMEFORMAT="%Y/%m/%d %H:%M:%S "
  996  2018/10/10 01:40:17 history
  997  2018/10/10 01:41:00   971  2018/10/10 01:39:26 history | tail -20
  998  2018/10/10 01:41:08 hist| tail -20
  999  2018/10/10 01:41:12 history | tail -20

若想永久保存历史命令,设置追加特殊权限

[root@worker1 ~]# chattr +a ~/.bash_history

常见快捷命令操作

!! 最后一条命令

!n n代表数字,表示第几条命令

!word word表示以word开头的命令从后往前找并执行

 

 

3 命令补全和别名

tab键

敲一下:如果输入字符是唯一,可以立刻补全

敲两下:如果输入字符有相同的多个,列出具有相同字符的命令

  • 参数补全
[root@worker1 ~]# yum install -y bash-completion   //需要重启系统
  • alias别名给命令重新起个名字
[root@worker1 ~]# alias 'restartnet=systemctl restart network.service'
[root@worker1 ~]# restartnet
  • 永久定义别名,设置配置文件即可

法1:

各用户都有自己配置别名的文件 ~/.bashrc

法2:

cd /etc/profile.d/

根据模板添加自定义的sh配置文件

  • 取消别名
[root@worker1 ~]# unalias restartnet
[root@worker1 ~]# restartnet
-bash: restartnet: command not found

 

4 通配符

"*"表任意字符

[root@worker1 ~]# ls *.txt
1.txt  2.txt  3.txt  test.txt

"?"表示任意一个字符

[root@worker1 ~]# ls ?.txt
1.txt  2.txt  3.txt

"[0-9]"表示0-9的数字范围

[root@worker1 ~]# ls [0-9].txt
1.txt  2.txt  3.txt

{1,2}表示1或2

[root@worker1 ~]# ls {1,2}.txt
1.txt  2.txt

 

5 输入输出重定向

">"重定向

[root@worker1 ~]# cat 1.txt
hello world
[root@worker1 ~]# cat 1.txt > 2.txt
[root@worker1 ~]# cat 2.txt
hello world

">>"追加重定向

[root@worker1 ~]# cat 2.txt
hello world
[root@worker1 ~]# cat 1.txt >> 2.txt
[root@worker1 ~]# cat 2.txt
hello world
hello world

"2>"错误重定向

[root@worker1 ~]# ls a.txt 2> err
[root@worker1 ~]# ls
1.txt  2.txt  3.txt  anaconda-ks.cfg  dir1  dir6  err  test.txt
[root@worker1 ~]# cat err
ls: cannot access a.txt: No such file or directory

"2>>"错误追加重定向

[root@worker1 ~]# ls a.txt 2>> err
[root@worker1 ~]# cat err
ls: cannot access a.txt: No such file or directory
ls: cannot access a.txt: No such file or directory

"<"输入重定向

[root@worker1 ~]# wc -l < 2.txt
2

"&>"表示错误和正确的同时输出到一个文件

[root@worker1 ~]# cat 1.txt a.txt &> 2.txt
[root@worker1 ~]# cat 2.txt
hello world
cat: a.txt: No such file or directory
等价于
[root@worker1 ~]# cat 1.txt a.txt > 2.txt 2>&1

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值