2019独角兽企业重金招聘Python工程师标准>>>

Shell介绍

  • shell是一个命令解释器,提供用户和机器之间的交互
  • 支持特定语法,比如逻辑判断、循环(if for whell)
  • 每个用户都可以有自己特定的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 命令

显示历史命令

[root@linux-128 ~]# history1  mkdir /tmp/2222  rmdir /tmp/111/ /tmp/222/...995  echo $?996  ls /usr/local/apache2/997  init 0998  ls ./.bash_history999  vim ./.bash_history1000  history

3. .bash_history 文件

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


4. history –c

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

[root@linux-128 ~]# history -c
[root@linux-128 ~]# history2  history
[root@linux-128 ~]# head -10 .bash_history
mkdir /tmp/222
rmdir /tmp/111/ /tmp/222/
ls
lsls tmp
ls /tmp
mkdir /tmp/111
mkdir /tmp/222
ls
ls /tmp
rmdir /tmp/111/ /tmp/222/

  • 只有当用户正常退出当前shell时,在当前shell中运行的命令才会保存至.bash_history文件中。

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

[root@linux-128 ~]# vim /etc/profile
搜索HISTSIZE 默认值是1000,可以修改成5000条,然后按esc,:wq 保存退出。
然后运行命令:source /etc/profile
[root@linux-128 ~]# source /etc/profile
[root@linux-128 ~]# echo $HISTSIZE
5000

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

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

[root@linux-128 ~]# vim /etc/profile
[root@linux-128 ~]# source /etc/profile
[root@linux-128 ~]# history2  2017/11/15 21:49:44 history3  2017/11/15 21:50:20 head -10 .bash_history4  2017/11/15 21:55:05 vim /etc/profile5  2017/11/15 21:57:52 source /etc/prfile6  2017/11/15 21:58:04 source /etc/profile7* 2017/11/15 21:58:16 echo $HIST8  2017/11/15 22:01:02 vim /etc/profile9  2017/11/15 22:03:49 history10  2017/11/15 22:03:57 source /etc/profile11  2017/11/15 22:03:59 history12  2017/11/15 22:04:20 vim /etc/profile13  2017/11/15 22:05:14 source /etc/profile14  2017/11/15 22:05:17 history

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

  • 给.bash_history加一个a权限,不能删除,可以追加
[root@linux-128 ~]# chattr +a .bash_history

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

[root@linux-128 ~]# pwd
/root
[root@linux-128 ~]# !!
pwd
/root

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

[root@linux-128 ~]# history2  2017/11/15 21:49:44 history3  2017/11/15 21:50:20 head -10 .bash_history4  2017/11/15 21:55:05 vim /etc/profile5  2017/11/15 21:57:52 source /etc/prfile6  2017/11/15 21:58:04 source /etc/profile7* 2017/11/15 21:58:16 echo $HIST8  2017/11/15 22:01:02 vim /etc/profile9  2017/11/15 22:03:49 history10  2017/11/15 22:03:57 source /etc/profile11  2017/11/15 22:03:59 history12  2017/11/15 22:04:20 vim /etc/profile13  2017/11/15 22:05:14 source /etc/profile14  2017/11/15 22:05:17 history15  2017/11/15 22:07:56 chattr +a .bash_history16  2017/11/15 22:08:10 vim .bash_history17  2017/11/15 22:09:25 pwd18  2017/11/15 22:10:31 history
[root@linux-128 ~]# !17
pwd
/root

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

[root@linux-128 ~]# !pw
pwd
/root

命令和文件名补全

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

  1. 按tab键可以帮我们补全一个指令,一个路径或者一个文件名
  2. Centos6中tab键只能补全本身;Centos7中tab键支持命令参数补全,需要安装一个包bash-completion
[root@linux-128 ~]# yum install -y bash-completion
重启服务器 reboot

别名

  1. Alias也是bash所特有的功能之一;直接执行alias命令会看到目前系统预设的所有别名。
[root@linux-128 ~]# 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
# .bashrc# User specific aliases and functionsalias 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@linux-128 ~]# 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.shroot@linux-128 ~]# vim /etc/profile.d/colorls.sh 


通配符

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

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

[root@linux-128 ~]# ls *txt
11.txt  1a.txt  23.txt  321.txt  ab.txt  a.txt  b.txt  工号.txt
[root@linux-128 ~]# ls 1*
11.txt  11.txt.bak  12  1a.txt  1.log
123:
[root@linux-128 ~]# ls *1*
11.txt  11.txt.bak  12  1a.txt  1.log  321.txt  321.txt.bak
123:
[root@linux-128 ~]# ls *a*
11.txt.bak  1a.txt  321.txt.bak  ab.txt  anaconda-ks.cfg  a.txt

?用来匹配一个字符

[root@linux-128 ~]# ls ?.txt
a.txt  b.txt

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

[root@linux-128 ~]# ls [0-3].txt
1.txt  2.txt
[root@linux-128 ~]# ls [a-z].txt
a.txt  b.txt
[root@linux-128 ~]# ls [0-9a-zA-Z].txt
1.txt  2.txt  a.txt  b.txt

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

[root@linux-128 ~]# ls {1,2}.txt
1.txt  2.txt[root@linux-128 ~]# ls {1,2,3}.txt
ls: 无法访问3.txt: 没有那个文件或目录
1.txt  2.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@linux-128 ~]# echo "112233">1.txt
[root@linux-128 ~]# cat 1.txt
112233

>>追加重定向

[root@linux-128 ~]# echo "aabbcc">>1.txt
[root@linux-128 ~]# cat 1.txt
112233
Aabbcc

2> 错误重定向

[root@linux-128 ~]# ls aaa.txt 2>2.txt
[root@linux-128 ~]# cat 2.txt
ls: 无法访问aaa.txt: 没有那个文件或目录

2>>错误追加重定向

[root@linux-128 ~]# ls aaa.txt 2>>2.txt
[root@linux-128 ~]# cat 2.txt
ls: 无法访问aaa.txt: 没有那个文件或目录
ls: 无法访问aaa.txt: 没有那个文件或目录

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

[root@linux-128 ~]# ls 1.txt aaa.txt &> 1.txt
[root@linux-128 ~]# cat 1.txt
ls: 无法访问aaa.txt: 没有那个文件或目录
1.txt

&>>

[root@linux-128 ~]# ls 1.txt aaa.txt &>> 1.txt
[root@linux-128 ~]# cat 1.txt
ls: 无法访问aaa.txt: 没有那个文件或目录
1.txt
ls: 无法访问aaa.txt: 没有那个文件或目录
1.txt

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

[root@linux-128 ~]# ls 1.txt aaa.txt > 1.txt 2>2.txt[root@linux-128 ~]# cat 1.txt
1.txt
[root@linux-128 ~]# cat 2.txt
ls: 无法访问aaa.txt: 没有那个文件或目录

< 输入重定向

[root@linux-128 ~]# wc -l < /etc/passwd
27
左边只能是命令,不能是文件。

转载于:https://my.oschina.net/u/3866516/blog/1837947

8.1-5shell介绍 ,命令历史 ,命令补全和别名,通配符,输入输出重定向相关推荐

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

    8.1 shell介绍 shell 是一个命令解释器,提供用户和机器之间的交互 支持特定语法,比如逻辑判断,循环 每个用户都可以有自己特定的shell CentOS7默认shell 为bash(Bou ...

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

    2019独角兽企业重金招聘Python工程师标准>>> shell介绍 shell是一个命令解释器,提供用户和机器之间的交互: 支持特定语法,比如逻辑判断.循环: 每个用户都可以有自 ...

  3. 8.1shell介绍 8.2命令历史 8.3命令补全和别名 8.4通配符 8.5输入输出重定向

    8.1 shell介绍 . 查找一下有没有这2个安装文件 8.2 命令历史 环璄1000,所以只能存1000条记录 ' 有时候查到比1000多了,那是因为输入的命令还没有写进这个文件,只存在内存中 h ...

  4. 8.1-8.5 shell介绍,命令历史,命令补全和别名,通配符,输入输出重定向

    8.1 shell介绍 8.2 命令历史 8.3 命令补全和别名 8.4 通配符 8.5 输入输出重定向 8.1 shell介绍 Linux Shell基础 介绍shell的特性,用法. shell是 ...

  5. Linux |奇怪的知识---complete命令---你不知道的命令参数补全---kubectl命令的参数补全

    前言: linux下有非常多的命令,其中有一些命令是比较冷门的,但比较有意思,比如,这个complete命令. complete命令定义命令或者脚本内的方法的参数自动补全内容是什么(例如,定义某个命令 ...

  6. ubuntu使用zsh进行命令行自动补全

    文章目录 ubuntu使用zsh进行命令行自动补全 1.zsh下载及配置 安装zsh 安装ohmyzsh 安装必要的插件 修改zshrc 使能代码提示 2.安装terminator终端 3.进入zsh ...

  7. 8.1 shell介绍 8.2 命令历史 8.3 命令补全和别名 8.4 通配符 8.5 输入输出重定向

    执行过的命令Linux都会记录,预设可以记录1000条历史命令.这些命令保存在用户的家目录的.bash_history文件中.只有当用户正常退出当前shell时,在当前shell中运行的命令才会保存至 ...

  8. Python命令行自动补全和记录历史命令

    2019独角兽企业重金招聘Python工程师标准>>> ~$ cat .pythonstartup import os import readline import rlcomple ...

  9. readline库实现命令行自动补全

    Table of Contents part1: readline安装 part2:readline使用举例 part3: readline下的IO复用 http://m.blog.chinaunix ...

最新文章

  1. python基础语法手册format-python的格式化输出(format,%)实例详解
  2. 一个时代的终结:为什么是时候放弃ITOM四大巨头了?这对IT领导者来说意味着什么?...
  3. 如何设计企业内部的数据平台?
  4. C++服务器设计(七):聊天系统服务端实现
  5. python 战舰_简单Python战舰
  6. 记录一下git 的常用命令
  7. 第五章:1.数组和广义表 -- 数组
  8. 静态页面 调用php数据,静态html页面如何更新点击数
  9. win2008 时间同步命令
  10. linux 实现离线迅雷,Linux下使用wget/aria2进行离线迅雷批量下载
  11. linux清理tmp垃圾文件夹,Linux tmp目录自动清理总结
  12. 虚拟服务器和vdi,如何搭建高效虚拟桌面架构(VDI)
  13. 浏览器下载集合(最新版本)
  14. 数字图像处理复习(part1)
  15. Word 2016 及以上版本如何设置分别自动插入可以链接到正文的图目录和表目录
  16. 什么是PHP?它的擅长领域是什么?它的工作原理是什么?
  17. 大数据在人际关系管理领域,主要有哪些价值优势?
  18. 苹果计算机软件安装打不开,iPad2安装的软件怎么打不开
  19. 高中数学基础-1.2.1函数的概念(下)函数的区间,求定义域和求值域
  20. IT猎头的几天生活感想((做IT的一定要看啊~~看了真是。。。)

热门文章

  1. Python语法基础:面向对象2
  2. 专技天下河北省2016年专业技术人员继续教育公需科目题库答案(答题器)
  3. 使用struts中的DisPatchAction的时候需要用到的jar包
  4. HDU 2087 剪花布条 KMP入门
  5. WebDevHelper -- RESTful服务和Ajax开发时的利器
  6. C/C++基础一:stack heap
  7. 自动驾驶算法-滤波器系列(七)——ESKF(error-state Kalman Filter)介绍
  8. 数据结构笔记(三十三)--二叉排序树的插入与生成
  9. android scalex中心,androidmatrix android怎么算matrix中心点
  10. android gps导航省电,具有省电功能的gps导航系统及其省电方法