学习linux第二十四天

shell

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

centOS默认的shell是bash,还有zsh和ksh两个不一样的shell

 

history

[root@hanlin ~]# cat /root/.bash_history  (存储history历史记录的路径)

[root@hanlin ~]# history -c (清除内存里的命令,按方向键就没有命令了,输入history也是空的,但是不能清空配置文件里记录的历史)


history在退出终端的时候才会把内存里的历史命令保存到配置文件中

[root@hanlin ~]# vim /etc/profile (进入配置文件,修改默认的储存最大值)

HISTSIZE=10000
 

[root@hanlin ~]# echo $HISTSIZE
1000
[root@hanlin ~]# source /etc/profile (立即刷新配置)
[root@hanlin ~]# echo $HISTSIZE
[root@hanlin ~]# HISTTIMEFORMAT="%Y%m%d %H:%M:%S" (自定义环境变量,使历史有时间,换终端失效)
[root@hanlin ~]# echo $HISTTIMEFORMAT 
%Y%m%d %H:%M:%S
[root@hanlin ~]# history
3 20180607 01:13:20 history 
4 20180607 01:13:27cd
5 20180607 01:13:28 history 

 

要使环境变量永久生效,把命令加到配置文件中

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

HISTSIZE=10000
HISTTIMEFORMAT="%Y-%m-%d %H:%M:%S"
[root@hanlin ~]# source /etc/profile

[root@hanlin ~]# chattr +a /root/.bash_history (加特殊的a权限,只能追加,不能删除,永久保存该历史记录文件)
[root@hanlin ~]# lsattr /root/.bash_history
-----a---------- /root/.bash_history

 

31 2018-06-07 01:45:22lsattr /root/.bash_history
32 2018-06-07 01:47:36history 
[root@hanlin ~]# !! (表示history最后一条命令)
history


30 2018-06-07 01:45:12cd /root
31 2018-06-07 01:45:22lsattr /root/.bash_history
32 2018-06-07 01:47:36history 
[root@hanlin ~]# !30 (!n进入history第n条命令)
cd /root
 

28 2018-06-07 01:45:05lsattr -a /root/.bash_history%
29 2018-06-07 01:45:09cd root
30 2018-06-07 01:45:12cd /root
31 2018-06-07 01:45:22lsattr /root/.bash_history
32 2018-06-07 01:47:36history 
[root@hanlin ~]# !cd (!+命令会在历史命令里倒着找到第一个cd命令)

cd /root
 

命令、参数补全

摁一下tab键:当命令只有一个的,并且字母没有重复的时候

摁两下tab键:当命令有多个重复字母的时候,把命令全部显示出来

 

[root@hanlin tmp]# yum -y install bash-completion-extras.noarch (参数补全的命令,安装完reboot)

 

[root@hanlin tmp]# vim /root/.bashrc (alias的定义配置文件,个人的配置在用户家目录下的.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@hanlin tmp]# vim /etc/profile.d/lang.sh (全局的alias配置文件在/etc/profile.d下的左右.sh文件中)

想要alias[root@hanlin lvm]# wc -l < 1.txt
2
命[root@hanlin lvm]# cat 1.txt
1.txt
2.txt
令永久生效把alias信息存到/etc/bashrc里面或者家目录下面的.bashrc

 

通配符

[root@hanlin lvm]# ls
123 1.txt 2.txt 33.txt ab.txt a.txt 1.txt.doc lost+found
[root@hanlin lvm]# ls *txt (显示所有后面带txt的文件)
1.txt 2.txt 33.txt ab.txt a.txt
[root@hanlin lvm]# ls ?.txt (显示所有后缀为txt,前面仅有一个字符的文件)
1.txt 2.txt a.txt
[root@hanlin lvm]# ls [1-3].txt (方括号里显示范围,只能是一个字符,字母,数字,符号都可以,符号只能单个搜0-9a-zA-Z)
1.txt 2.txt
[root@hanlin lvm]# ls [123].txt
1.txt 2.txt
[root@hanlin lvm]# ls [a-c].txt
a.txt
[root@hanlin lvm]# ls {1,2,3}.txt (花括号相当于是[],但是要用,分割,也是只能查找单个字符)
ls: 无法访问3.txt: 没有那个文件或目录
1.txt 2.txt

 

输入输出重定向

[root@hanlin lvm]# echo "112233" > 1.txt 输入文本到文件
[root@hanlin lvm]# echo "1111" > 2.txt 
[root@hanlin lvm]# cat 2.txt > 1.txt   用2.txt的文件内容覆盖1.txt的内容
[root@hanlin lvm]# cat 1.txt
1111
[root@hanlin lvm]# cat 2.txt >> 1.txt 在1.txt文末追加2.txt的内容,不删除1.txt的内容
[root@hanlin lvm]# cat 1.txt
1111
1111
[root@hanlin lvm]# lsddd 1.txt
bash: lsddd: 未找到命令...
[root@hanlin lvm]# lsddd 1.txt 
2>3.txt (2>把命令的错误信息输入到指定的文件中)
[root@hanlin lvm]# cat 3.txt
bash: lsddd: 未找到命令...
[root@hanlin lvm]# lsddd 1.txt 2>>3.txt (2>>表示把命令的错误信息追加到指定的文件中)
[root@hanlin lvm]# cat 3.txt
bash: lsddd: 未找到命令...
bash: lsddd: 未找到命令...
[root@hanlin lvm]# ls *.txt www.txt &> 1.txt (&>表示把正确和错误的结果一起输出,也支持追加,&>>)
[root@hanlin lvm]# cat 1.txt
ls: 无法访问www.txt: 没有那个文件或目录
1.txt
2.txt
33.txt
3.txt
ab.txt
a.txt
,.txt
$.txt
[root@hanlin lvm]# ls [12].txt www.txt &> 1.txt 2>2.txt (把命令结果同时输出到多个文件中去)
[root@hanlin lvm]# cat 1.txt
1.txt
2.txt
[root@hanlin lvm]# cat 2.txt
ls: 无法访问www.txt: 没有那个文件或目录


[root@hanlin lvm]# cat 1.txt
1.txt
2.txt

[root@hanlin lvm]# wc -l < 1.txt (输入重定向,把右边的结果输入给左边的命令并且执行,只能命令到文件,用的很少)

2

转载于:https://my.oschina.net/u/3867255/blog/1837308

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值