history linux 脚本,shell介绍、history历史命令及配置文件、tab补全、通配符和输出重定向...

本文介绍了Linux shell的基本概念,包括bash、历史命令的管理(history、HISTSIZE、HISTTIMEFORMAT),命令补全功能,以及别名的设置。还详细阐述了通配符的使用和输入输出重定向,如>>、>>、2>、2>>等,展示了如何控制命令输出和错误输出。通过实例演示了如何配置和使用这些功能,帮助用户更高效地在Linux环境中工作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

10月11日任务

8.1 shell介绍

8.2 命令历史

8.3 命令补全和别名

8.4 通配符

8.5 输入输出重定向

8.1 shell介绍

什么是shell

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

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

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

CentOS7默认shell为bash(Bourne Agin  Shell)

还有zsh/ksh等

8.2 命令历史

history命令

.bash_history

最大1000条

变量HISTSIZE

/etc/prefile中修改                #vim /etc/prefile 来修改变量HISTSIZE值

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

永久保存 chattr +a ~/.bash_history

!!         #表示上一条命令

!n        #表示第n条命令

!word     #表示执行最后一条word命令

#查看历史命令

[root@centos6 ~]# history

#历史命令变量

[root@centos6 ~]# echo $HISTSIZE

1000

#history -c清空内存中的命令历史,但不能修改配置文件 .bash_history里的内容

[root@centos6 ~]# history -c

[root@centos6 ~]# history

1 history

[root@centos6 ~]# ls -l .bash_history

-rw-------. 1 root root 1043 Oct 10 17:43 .bash_history

#编辑配置文件/etc/profile中HISTSIZE值修改为5000保存退出,想立即生效需要运行source /etc/profile   。

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

[root@centos6 ~]# echo $HISTSIZE

1000

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

[root@centos6 ~]# echo $HISTSIZE

5000

#编辑配置文件/etc/profile,在文档中HISTSIZE值下边一行添加HISTTIMEFORMAT="%Y/%m/%d% H:%M:%S",表示以年月日时间格式显示命令历史。

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

HISTSIZE=5000

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

[root@centos6 ~]# history

1 2018/10/11 10:04:15vi /etc/sysconfig/network-scripts/ifcfg-eth0

#给历史命令文件添加隐藏权限a,表示只能追加内容,不能删除。这样就可以永久保存历史记录。

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

[root@centos6 ~]# lsattr .bash_history

-----a-------e- .bash_history

8.3 命令补全和别名

tab键,敲一下,敲两下

参数不全,安装bash-completion

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

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

ls /etc/profile.d/

自定义的alias放到~/.bashrc

8.4 通配符

ls *.txt                 # 表示通配

ls ?.txt                 #表示一个任意的数字或英文字符

ls [0-9].txt             #表示0-9的数字范围中的一个

ls {1,2}.txt              #表示1或者2范围中的一个

cat 1.txt >2.txt        #>前边命令的结果输入到后边文件,后边文件原来的文件会被覆盖

cat 1.txt >>2.txt          #>>前边命令的结果追加到后边文件

ls aaa.txt 2>err           #  2>表示把错误的输出结果输出到err

ls aaa.txt 2>>err         # 2>表示把错误的输出结果追加到err

wc -l <1.txt                 #< 表示输入重定向,不常用,了解就行

command >1.txt 2>&1

#范例 :&>表示把错误和正确的结果都输出到a文件   , 同理&>>表示追加。

[root@centos6 ~]# touch 1.txt

[root@centos6 ~]# ls

1.txt anaconda-ks.cfg install.log install.log.syslog

[root@centos6 ~]# ls 1.txt aaa.txt &> a.txt

[root@centos6 ~]# cat a.txt

ls: cannot access aaa.txt: No such file or directory

1.txt

#把正确和错误的输出结果分别保存到不同文件。

[root@centos6 ~]# ls 1.txt aaa.txt > a.txt 2>b.txt

[root@centos6 ~]# cat a.txt

1.txt

[root@centos6 ~]# cat b.txt

ls: cannot access aaa.txt: No such file or directory

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值