前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >linux基础(day23)

linux基础(day23)

作者头像
运维小白
发布2022-01-06 13:50:20
4870
发布2022-01-06 13:50:20
举报
文章被收录于专栏:运维小白运维小白

8.1 shell介绍

shell介绍

  • shell是一个命令解释器,提供用户和机器之间的交互
  • 支持特定语法,比如逻辑判断、循环
  • 每个用户都可以有自己特定的shell
  • CentOS7默认shell为bash (Bourne Agin Shell)
  • 还有zsh、ksh等——>用法和bash相似,但是细节还是有一定差异
    • 查询zsh包——>yum list |grep zsh
    • 查询ksh包——>yum list |grep ksh

8.2 命令历史

命令历史

  • history //查看之前的命令
  • .bash_history //存放之前敲过的命令,在 /root/ 目录下
  • 最大1000条 //默认参数值是1000条
  • 变量HISTSIZE
  • /etc/profile中修改 //在其中可编辑HISTSIZE参数
  • HISTTIMEFORMAT="%Y/%m/%d %H:%M:%S"
  • 永久保存 chattr +a ~/.bash_history //增加隐藏权限,让他人无法删除 !! 表示执行最后一条命令
  • !n 表示运行第几条命令(n表示数字)
  • !echo 表示会在命令历史里面,倒着网上找第一个执行以 echo 开头的命令

查看敲过的命令存放位置

  • 在我们使用过的命令,会存放在用户的家目录下 /root/.bash_history
代码语言:javascript
复制
[root@localhost ~]# ls /root/.bash_history
/root/.bash_history
[root@localhost ~]# cat !$
cat /root/.bash_history
init 0
ping www.baidu.com
dhclient
ping www.baidu.com
yum install -y net-tools

等等等

查看之前敲过的命令

  • history //查看之前敲过的命令
代码语言:javascript
复制
[root@localhost ~]# history
    1  init 0
    2  ping www.baidu.com
    3  dhclient
    4  ping www.baidu.com
    5  yum install -y net-tools
    6  ifconfig

history命令中最多存放1000条历史命令

代码语言:javascript
复制
[root@localhost ~]# history -c
[root@localhost ~]# history
    1  history
代码语言:javascript
复制
但不会清空 .bash_history 配置文件,仅仅是把历史命令给清空

在敲完命令后,直接到配置文件中查看,会发现其中并没有存在 这是因为仅存在内存中,只有在退出终端的时候,才能够保存到配置文件中去

环境变量HISTSIZE在 /etc/profile 中配置

  • vim /etc/profile //在文件中修改环境变量的参数
    • 进入后 /HISTSIZE 搜索,并修改参数 HISTSIZE=5000
    • 在修改完参数后,要使参数生效,需
      • 方法1:需要重进一下终端
      • 方法二:执行 source /etc/profile
代码语言:javascript
复制
[root@localhost ~]# vim /etc/profile    //在里面编辑文件,改变参数

改变参数后,可以重启终端,或者source /etc/profile,发现参数生效
[root@localhost ~]# source !$        //执行命令后,会发现HISTSIZE值变化了
source /etc/profile
[root@localhost ~]# echo $HISTSIZE
5000

记录history查看历史命令时间

  • 记录命令使用的时间,给变量重新赋值,执行 HISTTIMEFORMAT="%Y/%m/%d %H:%M:%S" 即可
    • 指定变量的格式
代码语言:javascript
复制
[root@localhost ~]# history
    1  history
    2  vim /etc/profile
    3  yum provides "/*/vim"
    4  yum install -y vim-enhanced
    5  vim /etc/profile
    6  source /etc/profile
    7  echo $HISTSIZE
    8  HISTIMEFORMAT="%Y/%m/%d %H:%M:%S"
    9  history
[root@localhost ~]# HISTTIMEFORMAT="%Y/%m/%d %H:%M:%S"
[root@localhost ~]# history
    1  2017/11/15 23:25:28history
    2  2017/11/15 23:35:08vim /etc/profile
    3  2017/11/15 23:35:29yum provides "/*/vim"
    4  2017/11/15 23:53:58yum install -y vim-enhanced
    5  2017/11/15 23:59:04vim /etc/profile
    6  2017/11/16 00:07:14source /etc/profile
    7  2017/11/16 00:07:31echo $HISTSIZE
    8  2017/11/16 00:13:45history
    9  2017/11/16 00:14:49HISTTIMEFORMAT="%Y/%m/%d %H:%M:%S"
   10  2017/11/16 00:14:51history
   11  
[root@localhost ~]# echo $HISTTIMEFORMAT
%Y/%m/%d %H:%M:%S
代码语言:javascript
复制
这个环境变量仅仅在当前窗口下的终端生效,在打开另一个终端的时候,就会显示空的
也就是说,系统默认这个环境变量是不存在的
    1. 若想这个环境变量一直生效,则需要编辑 /etc/profile
代码语言:javascript
复制
[root@hf-01 ~]# vim /etc/profile

进入配置文件中,在变量HISTSIZE下放入
HISTTIMEFORMAT="%Y/%m/%d %H:%M:%S"
然后 :wq 保存退出
[root@hf-01 ~]# source !$        
source /etc/profile
    1. 在编辑完保存退出后,再source /etc/profile
    1. 这时候,在打开另一个终端
    • 执行echo $HISTTIMEFORMAT 会出现%Y/%m/%d %H:%M:%S
    • 这就表示执行成功,在其他终端下也生效了

命令历史永久保存

  • chattr +a ~/.bash_history //增加隐藏权限,让别人无法删除
    • 增加权限后,就只能追加,而不能被删除
代码语言:javascript
复制
[root@hf-01 ~]# chattr +a ~/.bash_history
[root@hf-01 ~]# 

在运行很多命令后,未正常退出(exit或logout正常退出),直接关闭终端,那刚刚敲的命令就不会完整的保存到 .bash_history 中去

  • !! 表示执行最后一条命令
  • !n 表示运行第几条命令(n表示数字)
  • !echo 表示会在命令历史里面,倒着网上找第一个执行以 echo 开头的命令

8.3 命令补全和别名

命令补全及别名目录概要

  • tab键,敲一下,敲两下
  • 参数补全,安装 bash-completion
  • alias别名给命令重新起个名字
  • 各用户都有自己配置别名的文件 ~/.bashrc
  • ls /etc/profile.d/
  • 自定义的alias 放到 ~/.bashrc

命令补全

在centos6中,命令补全仅支持命令本身,参数是不能补全的

在centos7中,支持命令的参数补全

alias别名

    1. 若是命令较长,可以设置别名
代码语言:javascript
复制
[root@hf-01 ~]# systemctl restart network.service        //重启网络服务
[root@hf-01 ~]# alias restartnet='systemctl restart network.service'
[root@hf-01 ~]# restartnet        //设置别名后,重启网络服务
[root@hf-01 ~]# 
代码语言:javascript
复制
- 取消别名unalias
    - 在取消别名后,在输入别名,就会提示未找到命令
代码语言:javascript
复制
[root@hf-01 profile.d]# unalias restartnet
[root@hf-01 profile.d]# restartnet
-bash: restartnet: 未找到命令
[root@hf-01 profile.d]# 
    1. 直接输入alias 会看到系统中所有的别名(包括自己自定义的alias)
代码语言:javascript
复制
[root@hf-01 ~]# 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 restarnet='systemctl restart network.service'
alias rm='rm -i'
alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'
[root@hf-01 ~]# 
    1. alias存放位置
    • 第一个是存放在用户家目录下的 .bashrc 文件中
    • 第二个是存放在 /etc/profile.d 目录下的 colorls.sh 和 colorgrep.sh 脚本中定义的

8.4 通配符

  • ls *.txt
    • *表示通配,不分字符,也不分几个
代码语言:javascript
复制
[root@hf-01 ~]# ls
111  123  1_heard.txt  1_sorft.txt  234  2.txt.bak  3.txt  anaconda-ks.cfg
[root@hf-01 ~]# ls *.txt        //以.txt结尾的文件都会列出来
1_heard.txt  1_sorft.txt  3.txt
[root@hf-01 ~]# ls *txt        //以txt结尾的文件都会列出来
1_heard.txt  1_sorft.txt  3.txt
[root@hf-01 ~]# ls *txt*        //包含txt的都会列出来
1_heard.txt  1_sorft.txt  2.txt.bak  3.txt
[root@hf-01 ~]# ls 1*        //只要1开头的都会列出来
1_heard.txt  1_sorft.txt

111:

123:
[root@hf-01 ~]# 
  • ls ?.txt
    • ?与* 相对比,? 表示一个任意的字符
    • 会看到(例子)只列出一个字符的.txt文件
代码语言:javascript
复制
[root@hf-01 ~]# touch 1.txt 2.txt
[root@hf-01 ~]# ls ?.txt
1.txt  2.txt  3.txt
[root@hf-01 ~]# touch a.txt bb.txt
[root@hf-01 ~]# ls ?.txt
1.txt  2.txt  3.txt  a.txt
[root@hf-01 ~]# 
  • ls [0-9].txt
    • []这里面可以写一个范围
代码语言:javascript
复制
[root@hf-01 ~]# ls
111  1_heard.txt  1.txt  2.txt      3.txt            a.txt
123  1_sorft.txt  234    2.txt.bak  anaconda-ks.cfg  bb.txt
[root@hf-01 ~]# ls [0-3].txt
1.txt  2.txt  3.txt

    可以把0,1,2,3这四个数字,任意一个都会满足这个条件,[]方括号中的字符只会取一个,就是“或者”的意思

[root@hf-01 ~]# ls [23].txt
2.txt  3.txt
[root@hf-01 ~]# ls [13].txt
1.txt  3.txt

在方括号中可以写范围[0-9a-zA-Z]
  • ls {1,2}.txt
    • 也是或者的意思,这个范围当中的一个
代码语言:javascript
复制
[root@hf-01 ~]# ls {1,2,3}.txt
1.txt  2.txt  3.txt
[root@hf-01 ~]# 

{1,2,3}.txt和[1-3].txt表达意思一样,或者。只是在{}需要用  ,  逗号隔开

8.5 输入输出重定向

大于号,重定向

代码语言:javascript
复制
> 正确输出
>> 追加重定向
2> 错误重定向
2>> 错误追加重定向

>+2>等于&>        表示结合了正确和错误
  • cat 1.txt > 2.txt
    • 一个大于号表示正确的输出
    • 大于号>,表示前面的命令输出,直接输入到后面的文件中去
    • 就会把1.txt文件内容重定向到2.txt文件中,而2.txt之前的文件就会删除掉,重新写入1.txt文件内容
  • cat 1.txt >> 2.txt
    • 两个大于号>>,就是追加,不会删除2.txt文件内容,而是在原有的基础上将1.txt文件内容写入到2.txt文件中去
  • ls aaa.txt 2> err
    • 2大于号表示错误的输出(错误信息)
    • 2> 表示它会把命令产生的错误信息指定输入到一个文件中去
代码语言:javascript
复制
[root@hf-01 ~]# laaa
-bash: laaa: 未找到命令
[root@hf-01 ~]# laaa 2> a.txt
[root@hf-01 ~]# cat a.txt
-bash: laaa: 未找到命令
[root@hf-01 ~]# 
  • ls aaa.txt 2>> err
  • ls [12].txt aaa.txt &> a.txt //正确和错误的输出信息都输出到a.txt中
代码语言:javascript
复制
[root@hf-01 ~]# ls [12].txt aaa.txt &> a.txt
[root@hf-01 ~]# cat a.txt
ls: 无法访问aaa.txt: 没有那个文件或目录
1.txt
2.txt
[root@hf-01 ~]# 
  • 把正确和错误的输出到文件中,方法一 ls [12].txt aaa.txt &>> a.txt
代码语言:javascript
复制
[root@hf-01 ~]# ls [12].txt aaa.txt &>> a.txt
[root@hf-01 ~]# cat !$
cat a.txt
ls: 无法访问aaa.txt: 没有那个文件或目录
1.txt
2.txt
ls: 无法访问aaa.txt: 没有那个文件或目录
1.txt
2.txt
  • 把正确和错误的输出到文件中,方法二 ls [12].txt aaa.txt >1.txt 2>a.txt
代码语言:javascript
复制
[root@hf-01 ~]# ls [12].txt aaa.txt >1.txt 2>a.txt
[root@hf-01 ~]# cat 1.txt
1.txt
2.txt
[root@hf-01 ~]# cat a.txt
ls: 无法访问aaa.txt: 没有那个文件或目录
代码语言:javascript
复制
既可以写入一个文件中,也可以分开写入

小于号,重定向

  • 小于号< ,输入重定向
  • wc -l < 1.txt //把1.txt文件内容输入重定向到命令wc -l 中去
代码语言:javascript
复制
[root@hf-01 ~]# wc -l < 1.txt
2
[root@hf-01 ~]# 2.txt < 1.txt
-bash: 2.txt: 未找到命令
[root@hf-01 ~]# 
  • 输入重定向,左边必须是命令,不支持文件输入重定向到文件中的
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2017/11/15 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 8.1 shell介绍
    • shell介绍
    • 8.2 命令历史
      • 命令历史
        • 查看敲过的命令存放位置
        • 查看之前敲过的命令
        • history命令中最多存放1000条历史命令
        • 环境变量HISTSIZE在 /etc/profile 中配置
        • 记录history查看历史命令时间
        • 命令历史永久保存
    • 8.3 命令补全和别名
      • 命令补全及别名目录概要
        • 命令补全
        • alias别名
        • 大于号,重定向
        • 小于号,重定向
    • 8.4 通配符
    • 8.5 输入输出重定向
    领券
    问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档