命令历史
登陆 shell 时,会读取命令历史文件中记录下的命令: ~/.bash_history
。登陆进 shell 后,新执行的命令只会记录在缓存中,这些命令会在用户退出时追加保存到命令历史文件中。
history使用
查看所有历史
[root@zze ~]# history
1 cd ~
2 ll
3 history
查看最近#条历史
[root@zze ~]# history 2
3 history
4 history 2
删除历史中指定的第#条命令
[root@zze ~]# history -d 4
[root@zze ~]# history
cd ~
ll
history
history -d 4
history
清空历史
[root@zze ~]# history -c
[root@zze ~]# history
1 history
快捷操作
调用历史中第#条命令
[root@zze ~]# history
history
ll
history
[root@zze ~]# !2
ll
total 20
-rw-------. 1 root root 1197 Jan 14 04:52 anaconda-ks.cfg
-rw-r--r--. 1 root root 9458 Jan 14 04:52 install.log
-rw-r--r--. 1 root root 3161 Jan 14 04:51 install.log.syslog
调用历史中最近一个以string开头的命令
[root@zze ~]# !l
ll
total 20
-rw-------. 1 root root 1197 Jan 14 04:52 anaconda-ks.cfg
-rw-r--r--. 1 root root 9458 Jan 14 04:52 install.log
-rw-r--r--. 1 root root 3161 Jan 14 04:51 install.log.syslog
调用上一条命令
[root@zze ~]# !!
ll
total 20
-rw-------. 1 root root 1197 Jan 14 04:52 anaconda-ks.cfg
-rw-r--r--. 1 root root 9458 Jan 14 04:52 install.log
-rw-r--r--. 1 root root 3161 Jan 14 04:51 install.log.syslog
引用上一条命令最后一个参数
[root@zze ~]# ls -l /etc/profile
-rw-r--r--. 1 root root 2008 Jan 14 05:28 /etc/profile
[root@zze ~]# cat !$
使用
ESC + .
可直接显示上一条命令参数。
控制命令的记录方式
命令的记录方式通过环境变量 HISTCONTROL
控制,可选三个值:
ignoredups
:忽略重复(连续且相同的命令)。
ignorespace
:忽略以空白字符开头的命令。
ignoreboth
:忽略以上两项。
修改环境变量值的方式:
export 变量名="值"
,默认只对当前会话生效。
相关环境变量
HISTSIZE
:命令历史记录的条数。
HISTFILE
:命令历史文件路径,~/.bash_history
。
HISTFILESIZE
:命令历史文件记录历史的条数。
目录操作
切换目录工作目录
[root@zze etc]# cd /etc/
[root@zze etc]# pwd
/etc
切换到上级目录
[root@zze etc]# cd ..
[root@zze /]#
显示当前所在路径
[root@zze ~]# pwd
/root
显示文件
显示所有文件,包含隐藏文件,包括 "." 和 ".."
[root@zze ~]# ls -a
. .. anaconda-ks.cfg .bash_history .bash_logout .bash_profile .bashrc .cache .cshrc install.log install.log.syslog .mysql_history .mysql_secret .oracle_jre_usage .pip .rediscli_history .tcshrc
显示所有文件,包含隐藏文件,不包括 "." 和 ".."
[root@zze ~]# ls -A
anaconda-ks.cfg .bash_history .bash_logout .bash_profile .bashrc .cache .cshrc install.log install.log.syslog .mysql_history .mysql_secret .oracle_jre_usage .pip .rediscli_history .tcshrc
长格式显示文件,不包含隐藏文件
[root@zze ~]# ls -l
total 20
-rw-------. 1 root root 1197 Jan 14 04:52 anaconda-ks.cfg
-rw-r--r--. 1 root root 9458 Jan 14 04:52 install.log
-rw-r--r--. 1 root root 3161 Jan 14 04:51 install.log.syslog
显示文件大小单位,通常和 "-l" 一起使用
[root@zze ~]# ls -lh
total 20K
-rw-------. 1 root root 1.2K Jan 14 04:52 anaconda-ks.cfg
-rw-r--r--. 1 root root 9.3K Jan 14 04:52 install.log
-rw-r--r--. 1 root root 3.1K Jan 14 04:51 install.log.syslog
[root@zze ~]# ll -h
total 20K
-rw-------. 1 root root 1.2K Jan 14 04:52 anaconda-ks.cfg
-rw-r--r--. 1 root root 9.3K Jan 14 04:52 install.log
-rw-r--r--. 1 root root 3.1K Jan 14 04:51 install.log.syslog
显示目录自身相关属性,通常和 "-l" 一起使用
[root@zze local]# ls -ld /etc/
drwxr-xr-x. 55 root root 4096 Jan 14 06:35 /etc/
[root@zze local]# ll -d /etc/
drwxr-xr-x. 55 root root 4096 Jan 14 06:35 /etc/
[root@zze local]# ll -d
drwxr-xr-x. 15 root root 4096 Jan 14 05:46 .
反序显示
[root@zze local]# ls
bin etc games include java lib lib64 libexec redis sbin share src tomcat
[root@zze local]# ls -r
tomcat src share sbin redis libexec lib64 lib java include games etc bin
递归显示文件夹及子文件夹所有文件
[root@zze ~]# ls -R /root/
/root/:
anaconda-ks.cfg install.log install.log.syslog
创建目录
创建单级目录
[root@zze ~]# mkdir testdir
[root@zze ~]# ls
anaconda-ks.cfg install.log install.log.syslog testdir
递归创建多级目录
[root@zze testdir]# mkdir a/b
mkdir: cannot create directory `a/b': No such file or directory
[root@zze testdir]# mkdir -p a/b
创建目录时显示提示信息
[root@zze testdir]# mkdir -pv a/b
mkdir: created directory `a'
mkdir: created directory `a/b'
创建目录同时指定权限
[root@zze testdir]# mkdir -m 664 a
[root@zze testdir]# ll
total 4
drw-rw-r--. 2 root root 4096 Jan 15 11:51 a
同时创建多个目录
[root@zze testdir]# mkdir a/ a/b a/b/c
[root@zze testdir]# tree a/
a/
└── b
└── c
删除目录
删除空目录
[root@zze testdir]# rmdir a/
递归删除所有空目录
[root@zze testdir]# rmdir -p a/b/c/
树状显示目录结构
树状显示目录及目录下的文件
[root@zze testdir]# tree a/
a/
└── b
├── c
└── test.txt
directories, 1 file
树状显示目录结构,不包含文件
[root@zze testdir]# tree -d a/
a/
└── b
└── c
directories
树状显示指定层级目录结构
[root@zze testdir]# tree -L 1 a/
a/
└── b
查看文本文件
正序查看指定文件
[root@zze testdir]# cat test.txt
2
4
6
8
反序查看指定文件
[root@zze testdir]# tac test.txt
8
6
4
2
分页查看文件
按页查看指定文件
[root@zze ~]# more /etc/profile
从指定行开始查看
[root@zze temp]# more +5 test.txt
555555555555555556666666666666666666667777777777777777777788888888888888888888
操作方式:
B :向前翻页,SPACE :向后翻页,RETURN :下一行,Q:退出。
比例显示是按字符统计,一旦翻完不可回转。
查看文件头部
默认输出前10行
[root@zze temp]# head test.txt
22222222222222222222222222222222222
4444444444444444444
666666666666666666666
88888888888888888888
10
从头部开始输出指定大小字节内容
[root@zze temp]# head -c 10 test.txt
1111111111
指定输出前#行
[root@zze temp]# head -n 5 test.txt
22222222222222222222222222222222222
4444444444444444444
查看文件尾部
默认输出后 10 行
[root@zze temp]# tail test.txt
1222222222222222
1444444444444
16
18
2000
从尾部开始输出指定大小字节内容
[root@zze temp]# tail -c 10 test.txt
8
19
2000
指定输出后#行
[root@zze temp]# tail -n 5 test.txt
177777777
19
tail [-n #|-#] <filename>:指定输出后 # 行。
跟踪显示文件新追加的内容
[root@zze temp]# tail -f test.txt
1222222222222222
1444444444444
16
18
2000
new line
文件时间戳管理
文件的数据分为元数据和内容数据,元数据可以看做文件的状态描述信息,内容数据则是文件的真实内容。
查看文件状态
[root@zze temp]# stat test.txt
File: `test.txt'
Size: 292 Blocks: 8 IO Block: 4096 regular file
Device: fd00h/64768d Inode: 1966102 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2019-01-13 09:57:08.339394446 +0800
Modify: 2019-01-13 09:57:08.339394446 +0800
Change: 2019-01-13 09:57:08.339394446 +0800
三个时间戳:
access time (atime):访问时间,读取文件内容时。
modify time (mtime):修改时间,修改文件内容时。
change time (ctime):改变时间,元数据发生改变。
修改时间戳
修改atime和mtime为指定时间,不指定time则默认修改为当前时间
[root@zze temp]# stat test.txt
File: `test.txt'
Size: 292 Blocks: 8 IO Block: 4096 regular file
Device: fd00h/64768d Inode: 1966102 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2019-01-13 09:57:08.339394446 +0800
Modify: 2019-01-13 09:57:08.339394446 +0800
Change: 2019-01-13 09:57:08.339394446 +0800
[root@zze temp]# touch test.txt
[root@zze temp]# stat test.txt
File: `test.txt'
Size: 292 Blocks: 8 IO Block: 4096 regular file
Device: fd00h/64768d Inode: 1966102 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2019-01-13 10:10:07.019393669 +0800
Modify: 2019-01-13 10:10:07.019393669 +0800
Change: 2019-01-13 10:10:07.019393669 +0800
修改mtime为指定时间,不指定time则默认修改为当前时间
[root@zze temp]# touch -m -t 201810101200.59 test.txt
[root@zze temp]# stat test.txt
File: `test.txt'
Size: 292 Blocks: 8 IO Block: 4096 regular file
Device: fd00h/64768d Inode: 1966102 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2019-01-13 10:10:07.019393669 +0800
Modify: 2018-10-10 12:00:59.000000000 +0800
Change: 2019-01-13 10:18:00.999393544 +0800
修改atime为指定时间,不指定time则默认修改为当前时间
[root@zze temp]# touch -a -t 201811101200.59 test.txt
[root@zze temp]# stat test.txt
File: `test.txt'
Size: 292 Blocks: 8 IO Block: 4096 regular file
Device: fd00h/64768d Inode: 1966102 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2018-11-10 12:00:59.000000000 +0800
Modify: 2018-10-10 12:00:59.000000000 +0800
Change: 2019-01-13 10:23:43.380393790 +0800
指定的文件不存在时则创建文件
[root@zze temp]# ls
test.txt
[root@zze temp]# touch a.txt
[root@zze temp]# ls
a.txt test.txt
当指定的文件不存在时不创建文件
[root@zze temp]# touch -c b.txt
[root@zze temp]# ls
a.txt test.txt
time 的格式为
yyyyMMddHHmmss.ss
,例:2019年1月1号2点35分34秒
为201901010235.34
。
评论区