linux命令行改变重定向,Shell介绍,history,Tab键,通配符,重定向

[toc]

Shell介绍,history,Tab键,通配符,重定向

8.1 shell介绍

8.2 命令历史

8.3 命令补全和别名

8.4 通配符

8.5 输入输出重定向

shell介绍

shell是一个命令解释器,提供用户和机器之间的交互

支持特定语法,比如逻辑判断、循环

每个用户都可以有自己特定的shell

CentOS7默认shell为bash(Bourne Agin Shell)

还有zsh、ksh等

命令历史

history命令

[root@localhost ~]# history

1 vi /etc/sysconfig/network-scripts/ifcfg-ens33

2 systemctl restart network.service

3 ip addr

4 ping -c 4 www.baidu.com

5 yum groupinstall -y "GNOME Desktop"

6 init5

7 init 5

8 vi /root/.ssh/authorized_keys

9 setenforce 0

10 vi /etc/selinux/config

11 mkdir /root/.ssh

.bash_history 最大1000条

[root@localhost ~]# echo $HISTSIZE

1000 //运行的命令只能保存1000条

history -c,内存中的命令清空

[root@localhost ~]# history -c

[root@localhost ~]# history

1 history

cat .bash_history 查看history配置文件,并未被清空

[root@localhost ~]# cat .bash_history

vi /etc/sysconfig/network-scripts/ifcfg-ens33

systemctl restart network.service

ip addr

ping -c 4 www.baidu.com

yum groupinstall -y "GNOME Desktop"

init5

init 5

vi /root/.ssh/authorized_keys

setenforce 0

vi /etc/selinux/config

mkdir /root/.ssh

chmod 700 /root/.ssh

vi /root/.ssh/authorized_keys

ssh -v

yum install -y openssh-client

yum install -y openssh-clients

init 5

init 6

用!+命令在history的行数,来执行这条历史命令

9lFa6h462i.png?imageslim

变量HISTSIZE /etc/profile中修改,修改完成用source命令才能完成更新

B9jdFma399.png?imageslim HISTTIMEFORMAT="%Y/%m/%d %H:%M:%S "

0KI4gkD26G.png?imageslim

[root@localhost ~]# echo $HISTSIZE

1000

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

[root@localhost ~]# echo $HISTSIZE

5000

改变环境变量HISTIMEFORMAT的定义,从而改变history的格式

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

[root@localhost ~]# echo $HISTTIMEFORMAT

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

[root@localhost ~]# history

1 2018/01/10 23:18:28 history

2 2018/01/10 23:20:12 cat bash_history

3 2018/01/10 23:20:38 cat .bash_history

4 2018/01/10 23:25:45 ls -l .bash_history

5 2018/01/10 23:25:49 ls

6 2018/01/10 23:26:44 vi /etc/profile

7 2018/01/10 23:32:44 echo $HISTSIZE

8 2018/01/10 23:33:02 source /etc/profile

9 2018/01/10 23:33:05 echo $HISTSIZE

新建一个ssh channel后就不在有用了,为了彻底改变,需要对/etc/profile再次编辑

CkIIjihkJ3.png?imageslim

fIe2b0jf73.png?imageslim

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

[root@localhost ~]# source !$

source /etc/profile

aiJC9140GF.png?imageslim

永久保存 chattr +a ~/.bash_history

非正常关机退出,命令保存的不全

!! 表示执行上一条命令

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

!word 表示执行命令历史中最近一次以word开头的命令

命令补全及别名

tab键,敲一下,补全指令

敲两下,系统会把所有的命令文件名列出来

参数补全,安装bash-completion

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

当不清楚命令时,利用tab键来讲命令不全

[root@localhost ~]# systemctl res

rescue reset-failed restart

[root@localhost ~]# systemctl restart network

network-online.target network.service

[root@localhost ~]# systemctl restart network.service

alias别名给命令重新起个名字

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

[root@localhost ~]# restartnet

有哪些alias命令

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

各用户都有自己配置别名的文件 ~/.bashrc

dahmlF39cF.png?imageslim

ls /etc/profile.d/

自定义的alias放到~/.bashrc

dDFKdfkbm3.png?imageslim

unalias取消别名

[root@localhost profile.d]# unalias restartnet

[root@localhost profile.d]# restartnet

bash: restartnet: 未找到命令...

通配符、输入输出重定向

[ ] ls .txt //用来匹配零个或多个字符

[ ] ls ?.txt //用?匹配一个字符

[ ] ls [0-9].txt ; ls [12].txt //范围&或者

[ ] ls {1,2}.txt //

[ ] cat 1.txt >2.txt //前命令的结果定向到后面的文档中,原文档删除

[ ] cat 1.txt >> 2.txt //追加,原文档内容不变

[ ] ls aaa.txt 2>err

>输出重定向

[root@localhost ~]# echo "1111" > 1.txt

[root@localhost ~]# cat 1.txt

1111

2> 表示错误重定向命令

[root@localhost profile.d]# lsaa

bash: lsaa: 未找到命令...

[root@localhost profile.d]# lsaa 2>a.txt //将错误信息定向到a.txt中

[root@localhost profile.d]# cat a.txt

bash: lsaa: 未找到命令...

“>>”追加重定向,重定向会替代掉原文件内容,而追加这里只做追加内容。

[root@localhost ~]# echo "1111">>1.txt

[root@localhost ~]# cat 1.txt

2221

1111

[root@localhost ~]# echo "1111">1.txt

[root@localhost ~]# cat 1.txt

1111

追加错误重定向

[ ] ls aaa.txt 2>>err

[ ] wc -l < 1.txt

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

1 //wc有多少文件?

[ ] command >1.txt 2>&1

“>+2> == &>” 把错误和正确的信息定向到一个文件中去,也可用于追加

[root@localhost ~]# >+2> == &>^C

[root@localhost ~]# ls [12].txt aaa.txt &> a.txt

[root@localhost ~]# cat a.txt

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

1.txt

2.txt

[root@localhost ~]# ls [12].txt aaa.txt &>> a.txt

[root@localhost ~]# cat a.txt

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

1.txt

2.txt

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

1.txt

2.txt

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值