linux文件搜索及其它基础命令介绍(3)

2019-09-23 09:15:16于丽

如输出距离1970-01-01零点到现在时间的秒数:

[root@centos7 temp]# date +%s
1477987540
[root@centos7 temp]#

如输出今天星期几:

[root@centos7 temp]# date +%A
星期二
[root@centos7 temp]# 

其他格式请自行man

4、gzip 压缩或解压文件

gzip [OPTION]... [FILE]...

当命令后直接跟文件时,表示压缩该文件:

[root@centos7 temp]# ls -l file1*
-rw-r--r-- 1 root root 132 10月 27 13:28 file10
-rw-r--r-- 1 root root 64 10月 27 15:06 file11
-rw-r--r-- 1 root root 22 10月 26 21:31 file12
-rw-r--r-- 1 root root 137 10月 12 16:42 file13
[root@centos7 temp]# 
[root@centos7 temp]# gzip file10 file11 file12 file13 
[root@centos7 temp]# ls -l file1*      
-rw-r--r-- 1 root root 75 10月 27 13:28 file10.gz
-rw-r--r-- 1 root root 49 10月 27 15:06 file11.gz
-rw-r--r-- 1 root root 44 10月 26 21:31 file12.gz
-rw-r--r-- 1 root root 109 10月 12 16:42 file13.gz

压缩后的文件以.gz结尾,gzip是不保留源文件的

选项-d表示解压缩

[root@centos7 temp]# gzip -d *.gz
[root@centos7 temp]# ls -l file1*
-rw-r--r-- 1 root root 132 10月 27 13:28 file10
-rw-r--r-- 1 root root 64 10月 27 15:06 file11
-rw-r--r-- 1 root root 22 10月 26 21:31 file12
-rw-r--r-- 1 root root 137 10月 12 16:42 file13

选项-r可以递归地进入目录并压缩里面的文件
选项-n指定压缩级别,n为从1-9的数字。1为最快压缩,但压缩比最小;9的压缩速度最慢,但压缩比最大。默认时n为6。

[root@centos7 temp]# gzip -r9 ./tmp

当gzip后没有文件或文件为-时,将从标准输入读取并压缩:

[root@centos7 temp]# echo "hello world" | gzip >hello.gz
[root@centos7 temp]# ls -l *.gz
-rw-r--r-- 1 root root 32 11月 1 16:40 hello.gz

注意例子中gzip的输出被重定向到文件hello.gz中,如果对此文件进行解压,将会生成文件hello。如果被重定向的文件后缀不是.gz,文件名在被改成.gz后缀之前将不能被解压。

5、zcat 将压缩的文件内容输出到标准输出

[root@centos7 temp]# zcat hello.gz 
hello world
[root@centos7 temp]#

zcat读取被gzip压缩的文件,只需文件格式正确,不需要文件名具有.gz的后缀。

6、bzip2 压缩解压文件

bzip2 [OPTION]... [FILE]...
命令bzip2和gzip类似都是压缩命令,只是使用的压缩算法不一样,通常bzip2的压缩比较高。本命令默认同样不保留源文件,默认文件名后缀为.bz2:

[root@centos7 temp]# bzip2 file11
[root@centos7 temp]# ls -l file11.bz2 
-rw-r--r-- 1 root root 61 10月 27 15:06 file11.bz2

选项-k可使源文件保留:

[root@centos7 temp]# bzip2 -k file10 
[root@centos7 temp]# ls -l file10*
-rw-r--r-- 1 root root 132 10月 27 13:28 file10
-rw-r--r-- 1 root root 96 10月 27 13:28 file10.bz2