天天看点

shell基本知识

shell基础知识

history命令 查看历史记录

[root@localhost ~]# history

1 echo $PATH

2 echo $LANG

3 locale

4 locale -a |grep zh

5 locale -a|grep zhhistory -c 

6 locale

7 locale -a|grep zh

8 locale

9 yum groupinstall chinese-support

10 locale

echo $HISTSIZE 查看可以记录的历史条数

[root@localhost ~]# echo $HISTSIZE

1000

history -c 清空历史

HISTSIZE 在/etc/profile中定义

HOSTNAME=<code>/usr/bin/hostname 2&amp;gt;/dev/null</code>

HISTSIZE=1000 我们可以修改这个数值,执行source /etc/profile

if [ "$HISTCONTROL" = "ignorespace" ] ; then

export HISTCONTROL=ignoreboth

else

export HISTCONTROL=ignoredups

fi

HISTTIMEFORMAT="%Y/%m/%d %H:%M:%S " 定义时间

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

[root@localhost ~]# echo $HISTTIMEFORMAT

%Y/%m%d %H:%M:%S

1 2018/0111 06:49:18 HISTTIMEFORMAT="%Y/%m%d %H:%M:%S "

2 2018/0111 06:49:41 echo $HISTTIMEFORMAT

3 2018/0111 06:49:58 history

时间永久生效的做法:把定义的时间放到/etc/profile下

#vim /etc/profile

#HISTTIMEFORMAT="%Y/%m/%d %H:%M:%S" 这条语句和#HISTSIZE=1000 放在一起

#:wq

#source /etc/profile

chattr +a ~/.bash_history 命令,记录永远保存,不能被删除,只能追加

!!表示执行上一条指令

[root@localhost ~]# pwd

/root

[root@localhost ~]# !!

pwd

!n 表示执行历史中的第n条指令

1 2018/01/11 06:49:18 HISTTIMEFORMAT="%Y/%m%d %H:%M:%S "

2 2018/01/11 06:49:41 echo $HISTTIMEFORMAT

3 2018/01/11 06:49:58 history

4 2018/01/11 06:53:55 vim /etc/profile

5 2018/01/11 06:56:34 source /etc/profile

6 2018/01/11 06:57:59 echo $HISTTIMEFORMAT

7 2018/01/11 06:58:06 history

8 2018/01/11 06:58:16 ls

9 2018/01/11 06:58:23 history

10 2018/01/11 07:06:22 chattr +a ~/.bash_history

11 2018/01/11 07:08:34 pwd

12 2018/01/11 07:10:03 history 

[root@localhost ~]# !8

ls

!字符串表示历史执行最近一条以字符串开头的指令

tab 命令补全

alias 别名命令-把常用的长命令换成短命令

自定义别名 是在.bashrc ls/etc/profile.d/中定义

自定义的alias 放在~/.bashrc/

alias restartnet='systmectl restart network.service'

通配符

表示匹配零个或是多个字符

?匹配一个字符*

[root@localhost ~]# touch {1..5}.txt

[root@localhost ~]# ls

1.txt 3.txt 5.txt filename test.txt

2.txt 4.txt anaconda-ks.cfg test.tar

[root@localhost ~]# touch bb.txt

[root@localhost ~]# touch cc.txt

[root@localhost ~]# ls ?.txt

1.txt 2.txt 3.txt 4.txt 5.txt

[root@localhost ~]# ls [34].txt

3.txt 4.txt

重定向符号

表示输出重定向 &lt;表示输入重定向 2&gt;错误重定向 &gt;追加重定向 [root@localhost ~]# mkdir /tmp/10 [root@localhost ~]# cd /tmp/10 [root@localhost 10]# echo "123" &gt; 1.txt [root@localhost 10]# ls 1.txt [root@localhost 10]# vi 1.txt [root@localhost 10]# echo "123"&gt;&gt; 1.txt [root@localhost 10]# cat 1.tx cat: 1.tx: 没有那个文件或目录 [root@localhost 10]# cat 1.txt 123

[root@localhost 10]# dddddd

-bash: dddddd: 未找到命令

[root@localhost 10]# dddddd 2&gt; 1.txt

[root@localhost 10]# echo "12345" &gt; 1.txt

12345

正确和错误的输出 指定到一个文件中

[root@localhost ~]# ls {1..3}.txt aaa.txt &amp;&gt;5.txt

[root@localhost ~]# cat 5.txt

ls: 无法访问aaa.txt: 没有那个文件或目录

2.txt

3.txt

本文转自 yzllinux博客,原文链接:  http://blog.51cto.com/12947851/2059645      如需转载请自行联系原作者