linux基础指令2

一.文件打包与压缩工具

1.zip可以压缩多个文件或目录

压缩多个文件
zip  /tmp/backp.zip  /etc/hosts  /etc/inittab

案例如下
[root@localhost tmp]# zip backup.zip file5 file6
  adding: file5 (deflated 63%)
  adding: file6 (stored 0%)
[root@localhost tmp]# ll backup.zip 
-rw-r--r--. 1 root root 1144 Mar  7 11:14 backup.zip
[root@localhost tmp]#

-r为递归压缩,dir1为目录,若不带-r,则只会压缩dir1目录
zip  -r  /tmp/backup1.zip  /tmp/dir1

案例如下
[root@localhost tmp]# zip -r song.zip songxiaolong/ file5
updating: songxiaolong/ (stored 0%)
updating: file5 (deflated 63%)
  adding: songxiaolong/fil2 (stored 0%)
  adding: songxiaolong/file1 (stored 0%)
[root@localhost tmp]# 

解压到非当前目录,使用-d指定解压目录
unzip  /tmp/backup.zip  -d  /home/dir3

案例如下
[root@localhost tmp]# unzip backup.zip  -d /home/
Archive:  backup.zip
  inflating: /home/file5             
 extracting: /home/file6             
[root@localhost tmp]# 

2.gzip/bzip2/xz

gzip/bzip2/xz用法基本相同,只能压缩单个文件,压缩后源文件就没有了,只有解压才会出现
gzip 使用-r可以递归压缩目录中的文件,但是一个一个文件压缩

gzip  file1 或gzip  file1  file2或gzip  -r  dir(目录)

bzip2/xz file1 或bzip2/xz  file1  file2

-d 解压  gzip -d或gunzip   bzip2 -d或bunzip2  xz -d或unxz

案例如下
gzip工具的压缩与解压
[root@localhost tmp]# gzip file5
[root@localhost tmp]# ll file5.gz 
-rw-r--r--. 1 root root 841 Mar  7 09:42 file5.gz
[root@localhost tmp]# gunzip file5.gz

bzip2工具的压缩与解压
[root@localhost tmp]# bzip2 file6
[root@localhost tmp]# ll file6.bz2 
-rw-r--r--. 1 root root 66 Mar  7 09:44 file6.bz2
[root@localhost tmp]# 
[root@localhost tmp]# bzip2 -d file6.bz2 
[root@localhost tmp]# ll file6.bz2
ls: cannot access file6.bz2: No such file or directory
[root@localhost tmp]# ll file6
-rw-r--r--. 1 root root 29 Mar  7 09:44 file6
[root@localhost tmp]# 

xz工具的压缩与解压
[root@localhost tmp]# xz file6
'[root@localhost tmp]# ll file6
ls: cannot access file6: No such file or directory
[root@localhost tmp]# ll file6.xz 
-rw-r--r--. 1 root root 88 Mar  7 09:44 file6.xz
[root@localhost tmp]# 
[root@localhost tmp]# unxz file6.xz 
[root@localhost tmp]# ll file6.xz
ls: cannot access file6.xz: No such file or directory
[root@localhost tmp]# ll file6
-rw-r--r--. 1 root root 29 Mar  7 09:44 file6
[root@localhost tmp]# 

3.tar(重点) 打包与压缩

-c 打包
-z 使用gzip工具压缩
-j 使用bzip2工具压缩
-J 使用xz工具压缩
-x 解压
-f 指定包名
-v 显示过程信息
-C 指定解压路径
-r 追加打包文件(压缩不可以,打包可以)
-t 查看打包或压缩包里面的内容
案例如下
打包并压缩目录和文件
[root@localhost tmp]# tar -czvf backup.tar.gz songxiaolong/ /etc/hosts
songxiaolong/
songxiaolong/fil2
songxiaolong/file1
tar: Removing leading `/' from member names             加上-P选项可以去掉该行,
/etc/hosts
-t选项可以查看压缩包里的文件列表
[root@localhost tmp]# tar -tf backup.tar.gz 
songxiaolong/
songxiaolong/fil2
songxiaolong/file1
etc/hosts
解压缩
[root@localhost tmp]# tar -xvf backup.tar.gz 
songxiaolong/
songxiaolong/fil2
songxiaolong/file1
etc/hosts
[root@localhost tmp]# 
只打包不压缩
[root@localhost tmp]# tar -cvf file.tar songxiaolong/
songxiaolong/
songxiaolong/fil2
songxiaolong/file1
查看tar包内容
[root@localhost tmp]# tar -tf file.tar 
songxiaolong/
songxiaolong/fil2
songxiaolong/file1
-r向tar包追加内容
[root@localhost tmp]# tar -rf file.tar /etc/passwd
tar: Removing leading `/' from member names
[root@localhost tmp]# tar -tf file.tar 
songxiaolong/
songxiaolong/fil2
songxiaolong/file1
etc/passwd
[root@localhost tmp]# 

二.日期相关指令

1.date命令(重点)

1.打印日期或时间
date
date +%F
date +%T
date +"%F %T"
date +%X
date +%D
date +"%Y-%m-%d %H:%M:%S"
date +%s  从1970.1.1 0:0:0到当前时间的秒数

案列如下:
[root@localhost tmp]# date
Wed Nov 11 11:53:02 CST 2020
[root@localhost tmp]# date +%F
2020-11-11
[root@localhost tmp]# date +%T
11:53:14
[root@localhost tmp]# date +"%F %T"
2020-11-11 11:53:22
[root@localhost tmp]# date +%X
11:53:50 AM
[root@localhost tmp]# date +%D
11/11/20
[root@localhost tmp]# date +"%Y-%m-%d %H:%M:%S"
2020-11-11 11:53:52
2.设置系统日期或时间
date -s 20190304  设置日期,会把时间设置为空
date -s 11:30:23  设置时间,不会对日期做出修改
date -s "20190304 15:30:34"  设置日期和时间

案例如下:
[root@localhost tmp]# date -s 20190306
Wed Mar  6 00:00:00 CST 2019
[root@localhost tmp]# date -s 17:50:30
Wed Mar  6 17:50:30 CST 2019
[root@localhost tmp]# date -s "20190306 17:52:20"
Wed Mar  6 17:52:20 CST 2019
3.系统时间与硬件时间的同步
hwclock 查看硬件时间
hwclock -s|--hctosys  以硬件时间为准同步系统时间
hwclock -w|--systohc  以系统时间为准同步硬件时间

案例如下:
[root@localhost tmp]# hwclock
Wed 11 Nov 2020 11:57:30 AM CST  -0.112570 seconds
[root@localhost tmp]# hwclock -w
[root@localhost tmp]# hwclock 
Wed 06 Mar 2019 05:52:58 PM CST  -0.547707 seconds

4.打印非当前的日期时间
date -d "+3days" +"%F %T" 3天后日期时间
date -d "-3days" +"%F %T" 3天前日期时间

说明:$()表示调用括号里命令的执行结果;  
$(date +%F)表示date +%F的输出结果20190304

案例如下:
[root@localhost tmp]# date -d "-3days" +%F
2019-03-03
[root@localhost tmp]# date -d "4days" +%F
2019-03-10

2.cal 命令

cal 或cal -1 输出当月日历

cal -3输出上月,当月,下月三个月的日历

cal 2019 显示2019年份的日历

cal 3 2019 显示2019.3月的日历

案例如下
[root@localhost tmp]# cal
     March 2019     
Su Mo Tu We Th Fr Sa
                1  2
 3  4  5  6  7  8  9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
31
[root@localhost tmp]# 
[root@localhost tmp]# cal 2018         2018年所有日历
[root@localhost tmp]# cal 3 2018       2018年3月份日历

三.文件查找查找

1.命令查找

which mkdir  显示mkdir命令的路径
whereis mkdir 显示命令路径及相关手册信息路径

案例
[root@localhost tmp]# which man
/usr/bin/man
[root@localhost tmp]# whereis mkdir
mkdir: /bin/mkdir /usr/share/man/man1/mkdir.1.gz /usr/share/man/man3p/mkdir.3p.gz /usr/share/man/man1p/mkdir.1p.gz /usr/share/man/man2/mkdir.2.gz
[root@localhost tmp]# 


2.文件查找

格式:find 路径 选项 关键字
-name  按文件名查找
-iname 忽略大小写按文件名查找
-type  按文件类型查找   b|c|d|f/-|l|p|s  文件类型
-size  按文件大小查找   +1M大于1M  -1M小于1M 1M等于1M
-user  按文件属主查找   -user inst01
-group 按文件属组查找   -group inst01
-perm  按文件权限查找   -perm  664
-atime 按文件访问时间查找
-mtime 按文件修改时间查找    +3 3天前(不包含3天前的当天) -3 3天以内 3 3天前的当天 0 当天
-ctime 按文件创建时间查找

案例
[root@localhost tmp]# find /etc/ -name '*.conf'
[root@localhost tmp]# find /etc/ -name '*.conf' -type c
[root@localhost tmp]# find /etc/ -type f -a -size +1M
/etc/selinux/targeted/modules/active/policy.kern
/etc/selinux/targeted/policy/policy.24
/etc/gconf/gconf.xml.defaults/%gconf-tree.xml
[root@localhost tmp]# 
[root@localhost tmp]# find /etc/  -mtime -1 

根据需求查找出来后执行某个动作

-exec 对匹配的文件执行该参数给出的命令
-ok   作用同-exec,但有提示用户确认是否执行
-delete 删除文件
-ls  列出文件
-print 打印出来,默认

注意
1.-exec或-ok后面写完命令后必须以( \;)结尾
2.{}表示find命令的执行结果
eg:find /etc -type f -a -size +1M -exec cp -a {} /tmp/dir/ \; 

案例
[root@localhost tmp]# find /tmp/ -name 'file*' -exec cp {}  ./inst01/ \;
[root@localhost tmp]# ll ./inst01/
total 8
-rw-r--r--. 1 root root    0 Mar  7 10:59 file4
-rw-r--r--. 1 root root 2184 Mar  7 10:59 file5
-rw-r--r--. 1 root root   29 Mar  7 10:59 file6
[root@localhost tmp]# 
[root@localhost tmp]# find /etc/  -mtime -1 -ls

猜你喜欢

转载自blog.csdn.net/sxlong_work/article/details/88782242
今日推荐