linux history 重定向,history、重定向、alias

第八章 shell基础

8.1 shell介绍

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

8.2 命令历史(history)

history命令

语法: history [-c]

-c:=clear 清除内存中的命令,不能删除配置文件中的历史命令

[root@adai003 ~]# history

1 ls

2 ls /tmp/

3 ls /boot/

4 ls /

5 dhclient

……

[root@adai003 ~]# ls /root/.bash_history

/root/.bash_history history的家目录

显示使用过的命令历史,默认保存1000条使用过的命令(注:此令需要是在正常关机操作情况下的处1000条命)!

history环境变量

变量HISTSIZE

[root@adai003 ~]# echo $HISTSIZE

1000

该变量决定命令历史保存的命令的数目。

定义变量HISTSIZE

编辑其配置文件

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

……

HOSTNAME=`/usr/bin/hostname 2>/dev/null`

HISTSIZE=1000

……

[root@adai003 ~]# echo $HISTSIZE

1000

[root@adai003 ~]# source /etc/profile

[root@adai003 ~]# echo $HISTSIZE

2000

搜索关键字"HIST"找到‘HISTSIZE=1000’,在此更改其数字,保存退出,然后执行命令‘source /etc/profile’刷新该配置文件才会生效。

更改history显示格式

[root@adai003 ~]# echo $HISTTIMEFORMAT

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

[root@adai003 ~]# echo $HISTTIMEFORMAT

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

[root@adai003 ~]# history

1 2017/06/28 18:50:11 history

2 2017/06/28 18:51:32 echo $HISTTIMEFORMAT

3 2017/06/28 18:51:43 HISTTIMEFORMAT="%Y/%m/%d %H:%M:%S "

4 2017/06/28 18:51:45 echo $HISTTIMEFORMAT

5 2017/06/28 18:52:32 history

直接为‘HISTTIMEFORMAT’赋值即可,不过此时该格式只适用于当前终端。如果要其使用于所有用户,则需要将其写入history配置文件并刷新后生效。

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

……

HOSTNAME=`/usr/bin/hostname 2>/dev/null`

HISTSIZE=1000

HISTTIMEFORMAT="%Y/%m/%d %H:%M:%S "

……

保存退出!

[root@adai003 ~]# source /etc/profile

命令历史永久保存

即,使命令历史记录只能写入不能被删除!

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

使用文件特殊权限,为‘.bash_history’文件配置‘a’权限(只可追加,不可删除),限于正常关机操作。

‘!!’命令

[root@adai003 ~]# w

……

[root@adai003 ~]# !!

w

……

‘!’的用法:‘!n’(n代表数字),表示运行命令历史中的第n条命令;‘!word’,表示运行上一次以该word开头的命令。

eg:

[root@adai003 ~]# history

1 2017/06/28 18:50:11 history

2 2017/06/28 18:51:32 echo $HISTTIMEFORMAT

3 2017/06/28 18:51:43 HISTTIMEFORMAT="%Y/%m/%d %H:%M:%S "

4 2017/06/28 18:51:45 echo $HISTTIMEFORMAT

5 2017/06/28 18:52:32 history

[root@adai003 ~]# !4

echo $HISTTIMEFORMAT

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

[root@adai003 ~]# !HIST

HISTSIZE=1000

8.3 命令补全和别名

命令补全Tab

按一次tab可以补全一个命令或参数(需要安装包bash-completion,并重启系统);按两次tab可以显示以某字母开头的所有命令或文件名。

alias命令

语法: alias [命令别名]=[具体命令] 设置别名

取消别名:unalias [命令别名]

直接输入alias会显示系统所有的别名:

[root@adai003 ~]# 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'

[root@adai003 ~]#

系统别名存放在配置文件‘~/.bashrc’和‘ls /etc/profile.d/’下:

[root@adai003 ~]# cat !$

cat .bashrc

# .bashrc

# User specific aliases and functions

alias rm='rm -i'

alias cp='cp -i'

alias mv='mv -i'

# Source global definitions

if [ -f /etc/bashrc ]; then

. /etc/bashrc

fi

[root@adai003 ~]# ls /etc/profile.d/

256term.csh colorgrep.sh lang.sh qt-graphicssystem.sh which2.sh

256term.sh colorls.csh less.csh vim.csh

bash_completion.sh colorls.sh less.sh vim.sh

colorgrep.csh lang.csh qt-graphicssystem.csh which2.csh

8.4 通配符

通配符‘*’代表零个或多个任意字符

通配符‘?’代表一个任意字符

中括号‘[]’,“ls [0-9].txt”表示0-9区间内的任意.txt文件

花括号‘{}’,“ls {1,2,3}.txt”表示括号内任意.txt文件

输入输出重定向

“>,>>,,2>>”

‘>’:输出重定向

‘>>’:追加重定向

‘2>’:错误重定向

使用‘>’命令时会将文件内原有内容删除。

[root@adai003 tmp]# echo adaixuelinux > 1.txt

[root@adai003 tmp]# cat 1.txt

adaixuelinux

[root@adai003 tmp]# echo adaixu > 1.txt

[root@adai003 tmp]# cat 1.txt

adaixu

#####################################

[root@adai003 tmp]# echo adaixu >> 1.txt

[root@adai003 tmp]# cat 1.txt

adaixu

adaixu

#####################################

[root@adai003 tmp]# lsaaa

-bash: lsaaa: 未找到命令

[root@adai003 tmp]# lsaaa 2> 2.txt

[root@adai003 tmp]# cat 2.txt

-bash: lsaaa: 未找到命令

输入重定向:必须定向到(

[root@adai003 tmp]# wc -l 1.txt

“ wc -l”该命令用于查看文件行数

2 1.txt

应用

[root@adai003 tmp]# ls {1,2}.txt aaaa.txt > 1.txt 2> 3.txt

[root@adai003 tmp]# cat 1.txt

1.txt

2.txt

[root@adai003 tmp]# cat 3.txt

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

说明: 使用ls命令查看 {1,2}.txt aaaa.txt,1.txt和2.txt文件存在,可以使用ls查看,aaaa.txt不存在,使用ls查看会报错,‘> 1.txt 2> 3.txt’意思是将正确信息保存到1.txt,将错误信息保存到3.txt。

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值