history:
1.查看命令历史记录:
[root@localhost ~]# history
2.清空历史记录:
[root@localhost ~]# history -c
3.历史记录默认保存1000条:
[root@localhost ~]# echo $HISTSIZE
1000
4.查看保存历史记录的文件:
[root@localhost ~]# cat /root/.bash_history
*history命令中的记录保存在内存中,当前用户本次连接终端后的操作记录需要在关闭终端时,才会被写入到.bash_history文件中,使用history -c清空历史记录不会清空用户目录下的.bash_history文件内容,只能清空当前用户本次连接终端后的操作记录
5.修改保存历史记录的数量为2000条:
[root@localhost ~]# vi /etc/profile
修改如下内容: 保存后重新连接终端或执行source /etc/profile使配置生效:
[root@localhost ~]# echo $HISTSIZE
1000
[root@localhost ~]# source /etc/profile
[root@localhost ~]# echo $HISTSIZE
2000
6.使history查看历史记录时显示时间:
[root@localhost ~]# vi /etc/profile
添加:HISTTIMEFORMAT="%Y-%m-%d %H:%M:%S " 保存后重新连接终端或执行source /etc/profile使配置生效:
[root@localhost ~]# history
1 2019-09-16 19:44:30 vi /etc/profile
2 2019-09-16 19:46:11 echo $HISTSIZE
3 2019-09-16 19:46:18 source /etc/profile
4 2019-09-16 19:46:19 echo $HISTSIZE
5 2019-09-16 19:50:16 vi /etc/profile
6 2019-09-16 19:52:37 history
7 2019-09-16 19:52:46 source /etc/profile
8 2019-09-16 19:52:47 history
9 2019-09-16 19:55:06 vi /etc/profile
10 2019-09-16 19:56:16 source /etc/profile
11 2019-09-16 19:56:19 history
防止.bash_history文件内容被删除可对该文件增加隐藏权限a(只能追加内容,不能删除文件内容以及文件)
[root@localhost ~]# chattr +a /root/.bash_history
[root@localhost ~]# lsattr /root/.bash_history
-----a---------- /root/.bash_history
*需要指定单个用户,对单个用户的用户目录下的该文件进行权限设置
!!命令:(执行上一条命令,也就是history记录中的最后一条)
[root@localhost ~]# date
2019年 09月 16日 星期一 20:13:47 CST
[root@localhost ~]# !!
date
2019年 09月 16日 星期一 20:14:07 CST
!n命令:执行history记录中的第n条命令
28 2019-09-16 20:10:13 lsattr /root/.bash_history
29 2019-09-16 20:12:20 ls
30 2019-09-16 20:12:25 mkdir test
31 2019-09-16 20:12:29 ls /etc/
32 2019-09-16 20:12:43 ls /tmp/
33 2019-09-16 20:12:50 ls /root/
34 2019-09-16 20:13:47 date
35 2019-09-16 20:14:29 history
[root@localhost ~]# !28
lsattr /root/.bash_history
-----a---------- /root/.bash_history
!xxx:执行history记录中以xxx开头的命令
33 2019-09-16 20:12:50 ls /root/
34 2019-09-16 20:13:47 date
35 2019-09-16 20:14:29 history
36 2019-09-16 20:14:47 lsattr /root/.bash_history
37 2019-09-16 20:16:24 history
[root@localhost ~]# !33
ls /root/
anaconda-ks.cfg test
命令补齐:
1.tab键可自动补齐命令,当需要补齐的对象名存在多个可能时,按两下tab可显示具体内容:
[root@localhost ~]# ls vmware
vmware1.test vmware2.test
2.对参数补全: 安装bash-completion包:
[root@localhost ~]# yum -y install bash-completion
*安装完后需重启系统才能生效
[root@localhost ~]# systemctl re
reboot reload reload-or-try-restart reset-failed
reenable reload-or-restart rescue restart
[root@localhost ~]# systemctl restart network
network-online.target network.service
通配符:
*号:(查找所有以.txt结尾的文件)
[root@localhost ~]# ls
1.txt 5.txt abc.txt a.txt vmware1.txt vmware2.txt
[root@localhost ~]# ls *.txt
1.txt 5.txt abc.txt a.txt vmware1.txt vmware2.txt
?号:(一个问号代表一个字符)
[root@localhost ~]# ls ?.txt
1.txt 5.txt a.txt
[root@localhost ~]# ls ???.txt
abc.txt
[]号:(取一个范围,单个字符,可以是0-9,a-z)
[root@localhost ~]# ls [0-9].txt #相当于ls [0,1,2,3,4,5,6,7,8,9]
1.txt 5.txt
[root@localhost ~]# ls [0-3].txt #相当于ls [0,1,2,3]
1.txt
{}号:(查看多个文件,指定文件名,','分割)
[root@localhost ~]# ls {1,a,abc}.txt
1.txt abc.txt a.txt
输出重定向:
重定向符号:> (将符号左边内容添加到右边文件中,并清空目标文件原内容)
[root@localhost ~]# cat a.txt
1111111
[root@localhost ~]# cat b.txt
2222222
[root@localhost ~]# cat a.txt >b.txt
[root@localhost ~]# cat b.txt
1111111
追加重定向符号:>> (将符号左边内容添加到右边文件中,不会清空目标文件原内容)
[root@localhost ~]# cat b.txt
1111111
[root@localhost ~]# cat a.txt >> b.txt
[root@localhost ~]# cat b.txt
1111111
1111111
错误重定向:2> (将左边命令的错误信息添加到右边文件夹中,并清空目标文件原内容)
[root@localhost ~]# ls
1.txt 5.txt abc.txt a.txt b.txt
[root@localhost ~]# cat aaaaaa.txt 2> b.txt
[root@localhost ~]# cat b.txt
cat: aaaaaa.txt: 没有那个文件或目录
错误追加重定向:2>> (将左边命令的错误信息添加到右边文件夹中,不会清空目标文件原内容)
[root@localhost ~]# cat aaaaaa.txt 2>> b.txt
[root@localhost ~]# cat b.txt
cat: aaaaaa.txt: 没有那个文件或目录
cat: aaaaaa.txt: 没有那个文件或目录
合并使用:
[root@localhost ~]# cat a.txt aaa.txt >>1.txt 2>>5.txt
[root@localhost ~]# cat 1.txt
1111111
[root@localhost ~]# cat 5.txt
cat: aaa.txt: 没有那个文件或目录
&> 符号:(正确信息和错误信息都输出到同一文件)
[root@localhost ~]# cat a.txt aaa.txt &> 1.txt
[root@localhost ~]# cat 1.txt
1111111
cat: aaa.txt: 没有那个文件或目录
管道符 | :(将左边命令输出的结果交给右边命令处理)
[root@localhost ~]# cat 1.txt
1111111
cat: aaa.txt: 没有那个文件或目录
[root@localhost ~]# cat 1.txt |grep aaa
cat: aaa.txt: 没有那个文件或目录
[root@localhost ~]# ls
1.txt 5.txt abc.txt a.txt b.txt
[root@localhost ~]# ls | wc -l
5
作业控制:
*在linux系统上作业时,可以临时暂停当前作业,回到命令行界面
ctrl+z:暂停当前作业(例如在使用vi 编辑文件时,需要返回命令行执行其他命令时,使用ctrl+z暂停作业,类似于Windows中当前窗口最小化,但窗口中的程序会暂停)
[root@localhost ~]# vi 1.txt
[1]+ 已停止 vi 1.txt
fg:恢复最后一次暂停的作业(将暂停的作业调至前台运行) bg:将暂停的作业调至后台运行(在后台运行中依然可以使用命令行执行命令) jobs:查看当前暂停或后台运行的作业
[root@localhost ~]# jobs
[1]- 已停止 vi 1.txt
[2]+ 已停止 vi a.txt
[root@localhost ~]# fg 2
*当jobs列表有多个任务时,fg、bg命令可指定序号操作指定任务
执行命令的同时将命令直接调至后台:(命令后加 & 符号)
[root@localhost ~]# jobs
[2]+ 已停止 vi a.txt
[root@localhost ~]# vi 1.txt &
[3] 7008
[root@localhost ~]# jobs
[2]- 已停止 vi a.txt
[3]+ 已停止 vi 1.txt