五周第三次课(3月7日)

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

8.1 shell介绍

Linux Shell基础

介绍shell的特性,用法。

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

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

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

CentOS7默认shell为bash(Bourne Agin Shell)

Bourne是一个用户,他开发的shell是sh.

后来CentOS7默认使用的shell是bash,基于sh优化而来。

shell还有zsh、ksh等

8.2 命令历史

history命令

.bash_historyhistory文件,存放命令

# cat /root/.bash_history

历史命令最大可以存1000条 (要修改最大值,可以修改/etc/profile文件)

查看变量HISTSIZE值,

# echo $HISTSIZE

1000

有时候敲命令,可以敲到1000+ ,这是因为这些命令还没有真正写到文件里,因为现在运行的命令是暂时存在内存中。

#history -c

清空当前命令历史,不能清空相关历史配置文件的,对配置文件无任何影响。只有退出终端,命令历史记录才会被写入文件中。

要改变$HISTSIZE,需在/etc/profile中修改

修改profile中的history参数之后,需要让修改生效,利用#source /etc/profile

[[email protected] ~]# vi /etc/profile

[[email protected] ~]# echo $HISTSIZE

1000

[[email protected] ~]# source /etc/profile

[[email protected] ~]# echo $HISTSIZE

5000

用时间显示使用过的命令历史记录。

使用参数:HISTTIMEFORMAT="%Y/%m/%d %H:%M:%S "

[[email protected] ~]# echo HISTTIMEFORMAT="%Y/%m/%d %H:%M:%S "Y年m月d日H时M分钟S秒

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

[[email protected] ~]# vi /etc/profile修改profile文件

[[email protected] ~]# source /etc/profile使参数修改生效

[[email protected] ~]# history

6  2018/03/07 15:08:26 history

7  2018/03/07 15:08:32 ls

8  2018/03/07 15:08:33 history

9  2018/03/07 15:09:16 cd /etc/

10  2018/03/07 15:09:19 cd -

11  2018/03/07 15:09:23 history

用久保存 chattr +a ~/.bash_history

如果不正常退出,保存的命令会不全。

!! 上一条命令,则history的最后一条命令。

!n  !接n条命令。例如!6 执行history的第6条命令

!word 从history倒退网上找最近一条相关word命令。例如!echo,从history里找最后一条echo的命令。

# history

26  2018/03/07 16:02:29 history

27  2018/03/07 16:04:17 echo HISTTIMEFORMAT="%Y/%m/%d %H:%M:%S "

28  2018/03/07 16:04:29 HISTTIMEFORMAT="%Y/%m/%d %H:%M:%S "

29  2018/03/07 16:06:06 history

# !echo

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

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

8.3 命令补全和别名

命令补全及别名

tab键,敲一下,敲两下

敲一下补全效果,

敲两下,列出相关补全文件或目录名

系统参数命令在未安装bash-completion之前,是不能补全的,例如敲入:

#systemctl restart network.service 会发现未安装之前是不能全部敲入的。

参数补全,安装bash-completion,安装完成后重启。

#yum install -y bash-completion

# rpm -qa bash-completion安装成功,

bash-completion-2.1-6.el7.noarch

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

给systemctl restart network.service做一个alias,名为:restartnet

[[email protected] ~]# alias restartnet='systemctl restart network.service'

[[email protected] ~]# restartnet

[[email protected] ~]# 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'

alias存放目录

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

ls /etc/profile.d/

自定义的alias放到 ~/.bashrc

~/.bashrc 部分在这

/etc/profile.d/  grep,colors

#unalias restartnet取消restartnet的alias

8.4 通配符

介绍

*用法

[[email protected] ~]# ls

1.txt  1.xtx  2.txt  a_(2).txt  anaconda-ks.cfg  anaconda-ks.cfg.1  a.txt  temp.1

[[email protected] ~]# ls *txt目录下所有TXT文件

1.txt  2.txt  a_(2).txt  a.txt

[[email protected] ~]# ls 1*txt目录下1开头的TXT文件

1.txt

[[email protected] ~]# ls *2*.txt目录下名称含有2的TXT文件

2.txt  a_(2).txt

?用法

[[email protected] ~]# touch 3.txt 4.txt a.txt bb.txt

[[email protected] ~]# ls

1.txt  1.xtx  2.txt  3.txt  4.txt  a_(2).txt  anaconda-ks.cfg  anaconda-ks.cfg.1  a.txt  bb.txt  temp.1

[[email protected] ~]# ls ?.txt文件名只有一个字的TXT文件名

1.txt  2.txt  3.txt  4.txt  a.txt

[[email protected] ~]# ls ??.txt文件名有两个字的TXT文件名

bb.txt

[]数字范围用法,支持特定数字,也支持特定范围。

[[email protected] ~]# ls [0-4].txt

1.txt  2.txt  3.txt  4.txt

[[email protected] ~]# ls [14].txt

1.txt  4.txt

[[email protected] ~]# ls [1234].txt

1.txt  2.txt  3.txt  4.txt

英文和数字用法

[[email protected] ~]# touch A.txt

[[email protected] ~]# ls [0-9a-zA-Z].txt

1.txt  2.txt  3.txt  4.txt  a.txt  A.txt

{}用法,例如{1,2,3}=[1-3]

[[email protected] ~]# ls {1,2,3}.txt

1.txt  2.txt  3.txt

[[email protected] ~]# ls [1-3].txt

1.txt  2.txt  3.txt

8.5 输入输出重定向

输出重定向>

cat 1.txt >2.txt把1.txt输出的内容结果重定向输入到2.txt的文件里。

cat 1.txt >>2.txt>>和>差不多,只是>>是追加,>>不把1.txt的文件内容删掉,会保留着,并且输入到2.txt,这个动作叫做追加。

>把前面输出的内容结果,重定向输入到后面的文件里。(前面被重定向的内容会被删掉)

ls aaa.txt 2>4.txt lsaaa.txt 产生的错误信息指定输出到4.txt里

[[email protected] ~]# ls aaa.txt 2> 4.txt

[[email protected] ~]# cat 4.txt

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

ls aaa.txt 2>>4.txtlsaaa.txt 产生的错误信息指定输出追加到4.txt里

2>产生的错误信息指定输出到指定文件里

2>>产生的错误信息指定输出追加到指定文件里

>正确的信息,2>错误的信息

>+2>=&> &>=>正确信息 2>的结合

&>举例演示

[[email protected] ~]# ls [1-2].txt aaa.txt &>a.txt

[[email protected] ~]# cat a.txt

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

1.txt

2.txt

&>>演示

[[email protected] ~]# ls [1-2].txt aaa.txt &>>a.txt

[[email protected] ~]# cat a.txt

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

1.txt

2.txt

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

1.txt

2.txt

用2个文件区分 正确与错误输出结果

[[email protected] ~]# ls [1-2].txt aaaa.txt >1.txt 2>a.txt

[[email protected] ~]# cat 1.txt

1.txt

2.txt

[[email protected] ~]# cat a.txt

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

输入重定向<,格式一般是:命令 < 文件 (这个比较少用)

[[email protected] ~]# wc -l < 1.txt

2

原文地址:http://blog.51cto.com/13578154/2083929

时间: 2024-08-02 04:50:53

五周第三次课(3月7日)的相关文章

三周第三次课(12月27日) 3.7 su命令 3.8 sudo命令 3.9 限制root远程登录

三周第三次课(12月27日)3.7 su命令3.8 sudo命令3.9 限制root远程登录 su命令: 用户和工作组管理: su命令用于切换当前用户身份到其他用户身份, 变更时须输入所要变更的用户帐号与密码. 语法: su(选项)(参数) 选项: -c<指令>或--command=<指令>:执行完指定的指令后,即恢复原来的身份: -f或--fast:适用于csh与tsch,使shell不用去读取启动文件: -l或--login:改变身份时,也同时变更工作目录,以及HOME,SHE

Linu20180415三周第三次课(4月4日)

3.7 su命令3.8 sudo命令3.9 限制root远程登录 su就是切换用户的命令 su - username 切记要使用 - . 我经常使用的时候是不加上-的,这样的话切换的不彻底,尚在之前用户的家目录下 可以采用 whoami来查看当前用户 -c 选项可以用来不登入用户,但是使用该用户来执行命令 su - -c "touch /tmp/3.33333" hello如下图所示 是否还记得 useradd -M user 是建立用户但是不生成家目录? 这样虽然可以切换到这个用户但

Linux学习笔记第三周第三次课(2月7日)

3.7 su命令 root用户切换到普通用户命令,su: 完全彻底切换 - ,连环境变量,家目录也切换,命令为#su - aming: 查看当前登陆用户,命令为#whoami: 查看当前目录,命令为#pwd: 查看用户UID,GID,组,家目录,用户shell目录,命令为#id: 切换用户,环境变量和家目录没切换,命令为#su aming: 退出切换的用户,命令为#exit: 当前用户下,不切换到aming用户,使用aming账号,执行touch命令,#su - -c "touch /tmp/a

五周第三次课(1月10日) 8.1 shell介绍 8.2 命令历史 8.3 命令补全和别名 8.4 通配符 8.5 输入输出重定向

五周第三次课(1月10日)8.1 shell介绍8.2 命令历史8.3 命令补全和别名8.4 通配符8.5 输入输出重定向 history命令: 用于显示指定数目的指令命令,读取历史命令文件中的目录到历史命令缓冲区和将历史命令缓冲区中的目录写入命令文件. 该命令单独使用时,仅显示历史命令,在命令行中,可以使用符号!执行指定序号的历史命令.例如,要执行第2个历史命令,则输入!2. 历史命令是被保存在内存中的,当退出或者登录shell时,会自动保存或读取.在内存中,历史命令仅能够存储1000条历史命

linux五周第三次课(3月7日)笔记

五周第三次课(3月7日)8.1 shell介绍8.2 命令历史8.3 命令补全和别名8.4 通配符8.5 输入输出重定向 搜索看一下有没有这两个shell,不安装. 8.2 命令历史 用过的命令,在/root/.bash_history存着 用cat命令查看一下/root/.bash_history 可以用命令history查看一下之前使用过的命令 history -c 修改了/etc/profile 配置文件,运行source命令,使其生效. 指定什么时候运行. 变量变了. 历史命令. 想要生

五周第三次课(1月10日)

8.1 shell介绍 Shell是系统的用户界面,提供了用户与内核进行交互操作的一种接口.它接收用户输入的命令并把它送入内核去执行.实际上Shell是一个命令解释器,它解释由用户输入的命令并且把它们送到内核. 8.2 命令历史 用户的命令历史配置文件 最大保存1000条,由系统内置环境变量配置,在/etc/profile中定义 修改之后要执行source /etc/profile history 查看命令历史 history -c 清空当前内存中保存的命令历史,并不清除配置文件 只有退出终端时

Linux学习笔记第五周第三次课(3月7日)

8.1 shell介绍 每个用户都有自己的shell: Bourne人名,为了纪念他: 搜索zsh命令,#yum list | grep zsh 搜索ksh命令,#yum list | grep ksh 逻辑判断if,for等等: 8.2 命令历史 输入过的命令,会记录下来,上下键进行查看: 历史命令存在.bash_history里面,最大1000条: 查看变量HISTSIZE条数,#echo $HISTSIZE 查看目前的历史命令内容,#history: 设置记录历史命令和所运行时间,#HIS

Linux20180422五周第三次课(4月20日

8.1 shell介绍8.2 命令历史8.3 命令补全和别名8.4 通配符8.5 输入输出重定向 shell介绍 Shell脚本只是一个表现,所谓的shell是一个命令解释器,用户和机器的一个交互 Shell我们主要使用的是bash shell 另外还有zsh ksh等 yum list |grep zsh yum list |grep ksh history查看历史命令 -c 是清空内存的命令历史 其实所有的历史命令都存在一个文件里 /root/.bash_histroy这个文件里 默认的存放

Linux学习笔记第三周第四次课(2月8日)

4.1 df命令 df,report file system disk space usage汇报文件系统磁盘空间使用情况: df命令格式:df [选项] df -a:all 显示所有文件系统的磁盘使用情况,包括0块(block)的文件系统. df -h:human readable以容易理解的格式输出文件系统大小,例如124KB.345MB.46GB. df -i:inodes 显示i节点使用情况. df -m:以MB为单位显示文件系统使用情况: df -t:type 显示各指定类型的文件系统