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

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

[root@lizhipeng01 ~]# history
1 ls -l .bash_history
2 vim /etc/profile
3 ls /root/.bash_history
4 cat /root/.bash_history
5 history

[root@lizhipeng01 ~]# echo $HISTSIZE       之前是1000,这个已经被我改了
10000

[root@lizhipeng01 ~]# history -c   当前命令历史清空

[root@lizhipeng01 ~]# history
1 history
[root@lizhipeng01 ~]# cat .bash_history

[root@lizhipeng01 ~]# history -c
[root@lizhipeng01 ~]# ls -l .bash_history
-rw-------. 1 root root 26957 1月 8 06:40 .bash_history

[root@lizhipeng01 ~]# vim /etc/profile            把它给成了5000

[root@lizhipeng01 ~]# echo $HISTSIZE       但是并没有生效
10000

[root@lizhipeng01 ~]# source /etc/profile     source一下,生效了
[root@lizhipeng01 ~]# echo $HISTSIZE

5000

[root@lizhipeng01 ~]# HISTTIMEFORMAT="%Y/%m/%d %H:%M:%S "
[root@lizhipeng01 ~]# echo $HISTTIMEFORMAT
%Y/%m/%d %H:%M:%S

打开另一个终端,没有生效。仅在当前终端生效。

[root@lizhipeng01 ~]# history
1 2018/01/09 07:39:08 ls -l .bash_history
2 2018/01/09 07:39:57 vim /etc/profile
3 2018/01/09 07:43:35 echo $HISTSIZE
4 2018/01/09 07:44:43 source /etc/profile
5 2018/01/09 07:44:49 echo $HISTSIZE
6 2018/01/09 07:48:01 HISTTIMEFORMAT="%Y/%m/%d %H:%M:%S "
7 2018/01/09 07:49:22 echo $HISTTIMEFORMAT
8 2018/01/09 07:54:59 history

[root@lizhipeng01 ~]# vim /etc/profile
[root@lizhipeng01 ~]# source /etc/profile

[root@lizhipeng01 ~]# chattr +a ~/.bash_history只会追加,不会删除,用户用的命令,都会被记录下来。

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

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

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

[root@lizhipeng01 ~]# yum install -y bash-completion

[root@lizhipeng01 ~]# rpm -qa bash-completion
bash-completion-2.1-6.el7.noarch

[root@lizhipeng01 ~]# alias restartnet='systemctl restart network.service'

[root@lizhipeng01 ~]# restartnet

[root@lizhipeng01 ~]# vi .bashrc  这个里面配置了几个alias

[root@lizhipeng01 ~]# 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 restartnet='systemctl restart network.service'
alias rm='rm -i'
alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'

[root@lizhipeng01 ~]# cd /etc/profile.d/                          alias定义在两个地方,一个是 /etc/profile.d/  ,另一个是.bashrc
[root@lizhipeng01 profile.d]# ls
256term.csh bash_completion.sh colorgrep.sh colorls.sh lang.sh less.sh vim.sh which2.sh
256term.sh colorgrep.csh colorls.csh lang.csh less.csh vim.csh which2.csh

通配符

*来匹配零个或多个字符,用?匹配一个字符

[root@lizhipeng01 ~]# ls
111 234 a.txt dir3 test4 testc.txt 学习计划安排.txt
123 2.txt.bak bb dir4 test5 Thinking_In_Java(中文版_第四版).pdf
1_hard.txt anaconda-ks.cfg dir2 split_dir testb.txt yum.log
[root@lizhipeng01 ~]# ls *.txt
1_hard.txt a.txt testb.txt testc.txt 学习计划安排.txt

[root@lizhipeng01 ~]# ls ?.txt
a.txt

[root@lizhipeng01 ~]# ls [0-3].txt
ls: 无法访问[0-3].txt: 没有那个文件或目录
[root@lizhipeng01 ~]# touch 1.txt 2.txt 3.txt
[root@lizhipeng01 ~]# ls [0-3].txt
1.txt 2.txt 3.txt

[root@lizhipeng01 ~]# ls [123].txt
1.txt 2.txt 3.txt

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

输入输出重定向

[root@lizhipeng01 ~]# lsaaa 2> a.txt
[root@lizhipeng01 ~]# cat a.txt
-bash: lsaaa: 未找到命令

[root@lizhipeng01 ~]# lsaaa 2>> a.txt      追加
[root@lizhipeng01 ~]# cat a.txt
-bash: lsaaa: 未找到命令
-bash: lsaaa: 未找到命令

[root@lizhipeng01 ~]# ls [12].txt aaa.txt &> a.txt       &> 把正确和错误的信息输出定向到a.txt
[root@lizhipeng01 ~]# cat a.txt
ls: 无法访问aaa.txt: 没有那个文件或目录
1.txt
2.txt

[root@lizhipeng01 ~]# ls [12].txt aaa.txt &>> a.txt     追加
[root@lizhipeng01 ~]# cat a.txt
ls: 无法访问aaa.txt: 没有那个文件或目录
1.txt
2.txt
ls: 无法访问aaa.txt: 没有那个文件或目录
1.txt
2.txt

[root@lizhipeng01 ~]# ls [12].txt aaa.txt > 1.txt 2>a.txt       正确的定向到1.txt,错误的定向到a.txt
[root@lizhipeng01 ~]# cat 1.txt
1.txt
2.txt
[root@lizhipeng01 ~]# cat a.txt
ls: 无法访问aaa.txt: 没有那个文件或目录

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

 

posted @ 2018-01-09 01:01  sisul  阅读(156)  评论(0编辑  收藏  举报