Linux centos7 shell 介绍、 命令历史、命令补全和别名、通配符、输入输出重定向

摘要:
事实上,外壳和计算机硬件之间还有另一层——系统内核。CentOS安装的shell版本是bash,这是sh的增强版本。BourneShell是最早的流行版本。其创始人是史蒂文·伯恩。为了纪念他的名字,BourneShell被缩写为sh.2。命令历史将被记录下来。默认情况下,可以记录1000个历史命令。这些命令保存在中。用户的主目录_历史文件的bash。只有当用户正常退出当前shell时,在当前shell中运行的命令才会保存到.bash_ history!

一、shell介绍

shell脚本是日常Linux系统管理工作中必不可少的,不会shell,就不是一个合格管理员。

shell是系统跟计算机硬件交互使用的中间介质,一个系统工具。实际上在shell和计算机硬件之间还有一层——系统内核。如果吧计算机比作人的躯体,那系统内核就是人的大脑,至于shell,把它比做人的五官更贴切。

其实,用户直接面对的不是计算机硬件而是shell,用户把指令告诉shell,然后shell再传给系统内核,接着内核再去支配计算机硬件去执行各种操作。

CentOS安装的shell版本是bash(Bourne Again Shell),是sh(Bourne Shell)的增强版。Bourne Shell是最早流行起来的版本,创始人是Steven Bourne,为了纪念他将其命名为Bourne Shell 简称sh

二、命令历史

我们执行过的命令都会记录,预设可记录1000条历史命令,这些命令保存在用户的家目录的.bash_history文件中。

只有当用户正常退出当前shell时,在当前shell中运行的命令才会保存至.bash_history

!是与命令历史有关的一个特殊字符

!!:连续连个表示执行上一条指令

!n:这里的n是数字,表示执行命令史中的第n条指令

!字符串(字符串大于等于1):!pw表示执行历史最近一次以pw开头的命令

[root@davery ~]# ls /root/.bash_history
/root/.bash_history
[root@davery ~]#
[root@davery ~]# cat !$       可存1000条
cat /root/.bash_history
ping wwwbaiducom
vi /etc/sysconfig/network-scripts/ifcfg-ens33
systemctl restart network.service
ifconfig
route -n
ping www.baidu.com
if config
ifconfig
ping wwww.baudu.com
systemctl restsrt network.service
systemctl restart network.service
journactl -xe
systemctl status network.service
etc/profile
systemctl restart network.service
ssh 192.168.1.106

....

[root@davery ~]# echo $HISTSIZE  查看可保存多少
1000
[root@davery ~]#history -c 清除历史命令,但不会删除命令,依旧可以使用命令

[root@davery ~]# history  查看使用过的命令

[root@davery ~]# vi /etc/profile 查看文本

HOSTNAME=`/usr/bin/hostname 2>/dev/null`
HISTSIZE=1000  最大存储量

改为500条

HISTSIZE=5000

[root@davery ~]# echo $HISTSIZE
1000
[root@davery ~]# source /etc/profile       刷新一下
[root@davery ~]# echo $HISTSIZE
5000
[root@davery ~]#

[root@davery ~]# HISTTIMEFORMAT="%Y/%m/%d %H:%M:%S " 指定格式

[root@davery ~]# echo $HISTTIMEFORMAT
%Y/%m/%d %H:%M:%S

[root@davery ~]# vi /etc/profile

HOSTNAME=`/usr/bin/hostname 2>/dev/null`
HISTSIZE=5000
HISTTIMEFORMAT="%Y/%m/%d %H:%M:%S "     粘贴变量

[root@davery ~]#

[root@davery ~]# vi /etc/profile
[root@davery ~]# source /etc/profile
[root@davery ~]# echo $HISTTIMEFORMAT
%Y/%m/%d %H:%M:%S
[root@davery ~]#

chattr +a ~ /.bash_history   永久保存

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

[root@davery ~]# init 0  正常关机才能保存

[root@davery ~]# pwd

/root
[root@davery ~]# !! 执行上一条命令
pwd
/root

[root@davery ~]# !555 执行第555条命令
fdisk -l

[root@davery ~]# !pw 执行最近一次以pw开头的命令
pwd
/root
[root@davery ~]#

三、命令补全和别名

1.命令补全:Tab,敲一下,可以帮我们补全一个指令、一个路径或者一个文件名;敲两下,系统会把所有的命令或者文件都列出来

参数补全,安装bash-completion

[root@davery ~]# yum install -y bash-completion
已加载插件:fastestmirror
base | 3.6 kB 00:00:00
extras | 3.4 kB 00:00:00
updates | 3.4 kB 00:00:00
Loading mirror speeds from cached hostfile
* base: mirrors.cn99.com
* extras: mirrors.aliyun.com
* updates: mirrors.aliyun.com
正在解决依赖关系
--> 正在检查事务
---> 软件包 bash-completion.noarch.1.2.1-6.el7 将被 安装
--> 解决依赖关系完成

依赖关系解决

=========================================================================================================================================
Package 架构 版本 源 大小
=========================================================================================================================================
正在安装:
bash-completion noarch 1:2.1-6.el7 base 85 k

事务概要
=========================================================================================================================================
安装 1 软件包

总下载量:85 k
安装大小:259 k
Downloading packages:
bash-completion-2.1-6.el7.noarch.rpm | 85 kB 00:00:00
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
正在安装 : 1:bash-completion-2.1-6.el7.noarch 1/1
验证中 : 1:bash-completion-2.1-6.el7.noarch 1/1

已安装:
bash-completion.noarch 1:2.1-6.el7

完毕!
[root@davery ~]# reboot

[root@davery ~]# system
systemctl systemd-coredumpctl systemd-inhibit systemd-run
systemd-analyze systemd-delta systemd-loginctl systemd-stdio-bridge
systemd-ask-password systemd-detect-virt systemd-machine-id-setup systemd-sysv-convert
systemd-cat systemd-escape systemd-notify systemd-tmpfiles
systemd-cgls systemd-firstboot systemd-nspawn systemd-tty-ask-password-agent
systemd-cgtop systemd-hwdb systemd-path
[root@davery ~]# systemctl res
rescue reset-failed restart

2.别名

alias给命令重新起名字

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

或者在ls /etc/profile.d/

取消自定义别名

unalias 

自定义alias放到~/.bashrc

[root@davery ~]# systemctl restart network.service
[root@davery ~]# alias restartsev='systemctl restart network.service'
[root@davery ~]# restartsev
[root@davery ~]# 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 restartsev='systemctl restart network.service'
alias rm='rm -i'
alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'
[root@davery ~]#

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

[root@davery ~]# vi .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@davery ~]# unalias restartsev 取消别名
[root@davery ~]#
[root@davery ~]# restartsev
-bash: restartsev: 未找到命令
[root@davery ~]#

四、通配符、、输入输出重定向

ls *.txt

ls ?.txt

ls [0-9].txt

ls {1,2}.txt

cat 1.txt > 2.txt     把1.txt指定输入到2.txt

cat 1.txt >> 2.txt   把1.txt追加到2.txt

ls aaa.txt 2 >err

ls aaa.txt 2 >>err

wc -| < 1.txt

command >1.txt 2>&1

[root@davery ~]# ls *.txt

[root@davery ~]# ls *
0.txt.gz 4913 5036 anaconda-ks.cfg.01 anaconda-ks.cfg.1 davery davery~ user1

1.txt:

make:

uear1:
[root@davery ~]#

[root@davery ~]# touch 1.txt
[root@davery ~]# touch 2.txt
[root@davery ~]# touch 3.txt
[root@davery ~]# ls ?.txt
2.txt 3.txt

1.txt:
[root@davery ~]#

[root@davery ~]# ls [0-3].txt
2.txt 3.txt

1.txt:
[root@davery ~]# ls [1].txt
[root@davery ~]# ls [123].txt
2.txt 3.txt

1.txt:
[root@davery ~]# ls [13].txt
3.txt

1.txt:
[root@davery ~]# ls [23].txt
2.txt 3.txt
[root@davery ~]#

[root@davery ~]# ls [0-9a-zA-Z].txt
2.txt 3.txt

1.txt:
[root@davery ~]#

[root@davery ~]# ls {1,2,3,}.txt
ls: 无法访问.txt: 没有那个文件或目录
2.txt 3.txt

1.txt:
[root@davery ~]#

输入、输出重定向

[root@davery ~]# cat 1.txt > 2.txt

[root@davery ~]# cat 1.txt >> 2.txt

[root@davery ~]# ls [12].txt aaa.txt &> 2.txt
[root@davery ~]# ls 2.txt
2.txt

[root@davery ~]# ls [12].txt aaa.txt &>> 2.txt

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

免责声明:文章转载自《Linux centos7 shell 介绍、 命令历史、命令补全和别名、通配符、输入输出重定向》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇GitHub安装教程双拼自然码下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

Linux系统下安装zookeeper教程

环境: 1.VMware® Workstation 12 Pro 2.CentOS7 3.zookeeper-3.4.6 安装步骤 1.下载zookeeper 本文使用的zookeeper下载地址如下(大家也可以下载其它版本) 链接:https://pan.baidu.com/s/1Ab9F53jNy7upsrYHCacWrw     提取码:jqyn 2...

Linux运维 -- 文件备份同步系列

【1、】文件备份与恢复 #(1)整盘数据备份-->另一个盘/一个image文件 dd if=/dev/sdb of=/dev/sde #备份到指定的image文件中 dd if=/dev/sdb of=/path/to/image dd if=/home/data/1.gz of=/back/data/1.gz #备份整盘数据,并压缩,...

【linux】【qt5】【将linux下的qt程序打包发布(完全适用于中标麒麟)】

前言: 最近因项目需求,需要打包linux-qt程序给客户先用一下子。百度一大堆终于找了几个靠谱的来综合一下,留为备用吧。 由于是先遣版所以仅制作为免安装程序的格式。 正文: 博主的qt是5.9.2的,程序名称为ocs,下面看到这个名称替换成自己的就是了。 1.先找个地方,比如桌面创建一个名字为OCS的空文件夹用于放打包后的文件。 2.将项目使用Relea...

Linux declare命令

[ Linux 命令大全Linux declare命令用于声明 shell 变量。 declare为shell指令,在第一种语法中可用来声明变量并设置变量的属性([rix]即为变量的属性),在第二种语法中可用来显示shell函数。若不加上任何参数,则会显示全部的shell变量与函数(与执行set指令的效果相同)。 语法 declare [+/-][rxi...

debian环境下 l7filter 的安装教程

debian环境下 l7-filter 的安装教程本文欢迎大家转载,请保留作者信息。请注明转自linuxsir.org,作者memory。l7-filter(Application Layer Packet Classifier for Linux)是linux下的开源项目,它是基于应用层数据识别的分类器。它可以分类数据包而不管你使用的是哪些端口,这对那些...

Uboot--Linux参数传递--ATAG【转】

转自:https://blog.csdn.net/gx19862005/article/details/28596539 Linux内核源码分析--内核启动命令行的传递过程(Linux-3.0 ARMv7) Linux内核在启动的时候需要一些参数,以获得当前硬件的信息或者启动所需资源在内存中的位置等等。这些信息可以通过bootloader传递给内核,比较常...