8.1 shell介绍8.2 命令历史8.3 命令补全和别名8.4 通配符8.5 输入输出重定向

简介:

8.1 shell介绍

1. shell是一个命令解释器,提供用户和机器之前的交换

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

3. CentOS7默认shell是bash(Bourne Agin Shell); shell还有zsh、ksh等

zsh、ksh这两种shell命令没有安装,

可以用yum list搜索下这两个命令的安装包:

[root@hao-01 ~]# yum list |grep zsh

[root@hao-01 ~]# yum list |grep ksh

4. shell有自己的特定语法,比如逻辑判断、循环

8.2 命令历史

1. 查看命令历史内所有命令

[root@hao-01 ~]# history

2. 查看命令历史存放的文件路径(用户夹目录.bash_history):

ls /用户夹目录/.bash_history

[root@hao-01 ~]# ls /root/.bash_history

注意:非正常命令退出终端,本次输入过的命令,不会保存到这个文件里,

再次打开终端,命令历史里也找不到(不完整)!!!

3. 查看命令历史存放文件储存的命令最大数值

[root@hao-01 ~]# echo $HISTSIZE

clipboard.png

4. 修改 命令历史存放文件储存的命令最大数值

[root@hao-01 ~]# vi /etc/profile

修改存储最大数值:HISTSIZE=最大数值(数字)

clipboard.png

5. 即刻生效 /etc/profile的修改:

[root@hao-01 ~]# source /etc/profile

6. 环境变量,命令历史命令标记 年月日,时分秒(只限当前终端生效!) :

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

7. 修改 命令历史命令标记的日期时间(永久生效的!) :

[root@hao-01 ~]# vi /etc/profile

添加:命令历史命令标记日期时间的环境变量: HISTTIMEFORMAT="%Y/%m/%d %H:%M:%S "

clipboard.png

8. 即刻生效 /etc/profile的修改:

[root@hao-01 ~]# source /etc/profile

9. 查看命令历史内所有命令

[root@hao-01 ~]# history

clipboard.png

10. .bash_history存放历史命令文件添加特殊a权限(避免被修改删除):

chattr +a  /用户夹目录/.bash_history

[root@hao-01 ~]# chattr +a /root/.bash_history

11. 执行上一条命令!!

[root@hao-01 ~]# !!

clipboard.png

12. 执行命令历史编号对应命令

!命令历史命令编号

[root@hao-01 ~]# !978

13. 执行ls开头,最近执行过得命令:

[root@hao-01 ~]# !ls

8.3 命令补全和别名

1. 敲一下 Tab键,参数可补全,需要安装bash-completion包:

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

2. 重启系统(生效):

[root@hao-01 ~]# reboot

3. 敲一下 Tab键: 自动补全命令、路径 、centos7还可以补全参数

4. 敲两下 Tab键: 列出以输入的命令为开头的多个命令

5. 设定 自定义别名命令 

alias 自定义的别名命令='原命令'

[root@hao-01 ~]# alias haols='ls'

clipboard.png

6. 列出所有别名命令(alias) :

[root@hao-01 ~]# alias

clipboard.png

7. 查看 自定义alias(别名命令)存放文件:

ls  /用户夹目录/.bashrc                            

[root@hao-01 ~]# ls /root/.bashrc

8. 查看 别名命令存放脚步所在目录:

[root@hao-01 ~]# ls /etc/profile.d/

9. 取消(删除) 自定义别名命令

unalias  自定义的别名命令

[root@hao-01 ~]# unalias haols

clipboard.png

8.4 通配符

1. 当前目录列出,包含.txt的文件(*表示多个任意的字符) :

[root@hao-01 ~]# ls *.txt

clipboard.png

[root@hao-01 ~]# ls *txt

clipboard.png

2. 当前目录列出,(匹配)所有1开头的文件或目录 :

[root@hao-01 ~]# ls 1*

clipboard.png

3. 当前目录列出, 包含.txt文件(?表示一个任意的字符) :

[root@hao-01 ~]# ls ?.txt

clipboard.png

4. 当前目录列出,[]内范围数字包含.txt的文件(数字最大范围0-9) :

[root@hao-01 ~]# ls [0-9].txt

clipboard.png

匹配出指定数字包含.txt的文件:

[root@hao-01 ~]# ls [09].txt

clipboard.png

5. 当前目录列出,[]内范围字母包含.txt的文件(字母范围 a-z或A-Z) :

[root@hao-01 ~]# ls [a-z].txt

clipboard.png

匹配出指定字母包含.txt的文件:

[root@hao-01 ~]# ls [az].txt

clipboard.png

6. 当前目录列出,[]内范围数字和字母包含.txt的文 :

[root@hao-01 ~]# ls [0-9a-z].txt

clipboard.png

7. 当前目录列出,[]内数字开头带有.txt的文件 :

(方括号只支持单数,数字最大到9)注意:方括号每个字符之间可不加逗号分割!!!)

[root@hao-01 ~]# ls [1,2,3,4,11,22,33,44].txt

clipboard.png

8. 当前目录列出,{}内数字开头带有.txt的文件(花括号支持多位字符) :

注意:花括号每个字符之间必须加逗号分割

[root@hao-01 ~]# ls {1,2,3,4,11,22,33,44}.txt

clipboard.png

8.5 输入输出重定向

输出:命令结果,输出到右边文件

1. 输出重定向注意:>(一个大于号)后面文件原有内容会被删除

前面命令输出结果输出后面文件(原有内容被删): 前面命令 >  后面文件

[root@hao-01 ~]# cat 1.txt > 2.txt

2. 输出追加重定向:注意:>>(两个大于号)后面文件原有内容不会被删除

前面命令输出结果追加到 >后面文件(原有内容不会删): 前面命令 >>  后面文件

[root@hao-01 ~]# cat 1.txt >> 2.txt

3. 输出错误命令输出重定向

错误命令的输出结果,输出后面文件(原有内容被删):前面错误命令  2>  后面文件

[root@hao-01 ~]# cataa 1.txt  2>  2.txt

[root@hao-01 ~]# cat 2.txt

clipboard.png

4. 输出错误命令追加重定向

错误命令的输出结果追加后面文件(原有内容不会删):前面错误的命令 2>>  后面文件

[root@hao-01 ~]# cataa 1.txt  2>>  2.txt

clipboard.png

输入:文件内容,输入到左边命令

1. 1.txt内容,输入到wc -l命令执行:

命令 <  文件

[root@hao-01 ~]# wc -l <  2.txt

clipboard.png










本文转自 主内安详 51CTO博客,原文链接:http://blog.51cto.com/zhuneianxiang/2059488,如需转载请自行联系原作者
目录
相关文章
|
16天前
|
Web App开发 Java Linux
Linux之Shell基本命令篇
Linux之Shell基本命令篇
Linux之Shell基本命令篇
|
1月前
|
监控 Shell Linux
【Shell 命令集合 系统管理 】Linux 显示当前登录到系统的用户信息 who命令 使用指南
【Shell 命令集合 系统管理 】Linux 显示当前登录到系统的用户信息 who命令 使用指南
43 1
|
4天前
|
存储 Shell 数据安全/隐私保护
ZooKeeper【基础知识 04】控制权限ACL(原生的 Shell 命令)
【4月更文挑战第11天】ZooKeeper【基础知识 04】控制权限ACL(原生的 Shell 命令)
23 7
|
11天前
|
分布式计算 Hadoop Shell
Hadoop【基础知识 04】【HDFS常用shell命令】(hadoop fs + hadoop dfs + hdfs dfs 使用举例)
【4月更文挑战第4天】Hadoop【基础知识 04】【HDFS常用shell命令】(hadoop fs + hadoop dfs + hdfs dfs 使用举例)
26 5
|
29天前
|
存储 Shell 数据安全/隐私保护
【Shell 编程指南】Shell read命令 (从标准输入读取数值)
【Shell 编程指南】Shell read命令 (从标准输入读取数值)
23 0
|
1月前
|
网络协议 Shell Linux
【Shell 命令集合 系统管理 】Linux 查询域名的注册信息 whois命令 使用指南
【Shell 命令集合 系统管理 】Linux 查询域名的注册信息 whois命令 使用指南
48 1
|
1月前
|
存储 Shell Linux
【Shell 命令集合 系统管理 】Linux 显示当前登录用户的用户 whoami命令 使用指南
【Shell 命令集合 系统管理 】Linux 显示当前登录用户的用户 whoami命令 使用指南
49 1
|
23天前
|
弹性计算 Shell Perl
ecs服务器shell常用脚本练习(二)
【4月更文挑战第1天】shell代码训练(二)
106 1
|
26天前
|
Java Shell
SpringBoot启动脚本Shell
SpringBoot启动脚本Shell
17 0
|
3天前
|
Java 关系型数据库 MySQL
Elasticsearch【问题记录 01】启动服务&停止服务的2类方法【及 java.nio.file.AccessDeniedException: xx/pid 问题解决】(含shell脚本文件)
【4月更文挑战第12天】Elasticsearch【问题记录 01】启动服务&停止服务的2类方法【及 java.nio.file.AccessDeniedException: xx/pid 问题解决】(含shell脚本文件)
27 3