六、Linux计划任务及压缩归档

1. find:用来查找文件

格式:find 目录名 选项 参数 -a …

-name

 根据名称查找

find / -name "ww"

find / -name "ken*" | xargs rm -rf

-perm

权限(为完全匹配,-为包含匹配

find / -perm 777

ls -ld / 随便查看一个

find / -perm 444

ls-ld / 随便查看一个

-user

匹配所有者

find/ -user ken

-group

 属组

-mtime:修改时间

fond/ -mtime +2

-atime: 访问时间

-ctime: 修改时间

-nouser

匹配无用户所有者文件

find / -nouser

-nogroup

匹配无组所有者文件

 

-type

f/d/l/b/c/p 匹配文件类型

 

-size:

 + -  匹配文件大小

 

-exec:

 - nouser -exec 命令 {} \;

find /ww -name “*txt”-exec rm -rf {} \ ;

find /ww -name “*txt”| xargs rm -rf

 

2. tar 压缩归档

-c: 创建

-z: gzip 压缩

-v: 显示过程

-f: 指定包名

-x: 解压

 

zip 结尾的包:unzip

gz 结尾的包:gunzip

tar.gz 结尾的包:xzvf

 

 

常用选项组合:

压缩归档

czvf

减压

xzvf

 

 

 

案例:

压缩归档:

tar czvf files.tar.gz./*

Mkdir / test

cp files.tar gz /test    (gz : gzip )

cd /test

ls

 

解压: tar xzvf files.tar.gz

tar xzvf files.tar.gz -C /ww 指定解压到哪个目录下

unzip ; ww.zip

yum install unzip -y

mount /dev/cdrom/mnt挂载硬件

yum install unzip -y

unzip  ww.zip

cd ww

 

3.计划任务

at :一次性计划任务:at (atd):执行完退出

 

实例:

1. 下载at程序

 yum install at -y

2. 启动atd服务

 systemctl restart atd

 systemctl  enable  atd  开机自启

3. 设置at计划任务

[root@ww ~]# at 10:00                       

at> touch /tmp/test1                         #输入你要执行的命令

at> <EOT>                                    #按ctrl+d保存

job 1 at Fri Mar  1 10:00:00 2019

[root@ww ~]# at -l                          #查看计划任务1    Fri Mar  1 10:00:00 2019 a root

[root@ww ~]# atq                            #查看计划任务1    Fri Mar  1 10:00:00 2019 a root

 

(ctrl + backspace删除)

atq 查看计划任务或者 at-l

按ctrl d 保存

删除计划任务atrm

 At -l查看

atrm 名字  删除

 

5. at计划任务的特殊写法

[root@ww ~]# at 20:00 2018-10-1   在某天

[root@ww ~]# at now +10min   在 10分钟后执行

[root@ww ~]# at 17:00 tomorrow   明天下午5点执行

[root@ww ~]# at 6:00 pm +3 days   在3天以后的下午6点执行

 

 

4.周期性计划任务:crontab

 

作用:定时任务的使用

-l: list 查看计划任务

-e: edit编辑计划任务

-r: remove 删除计划任务

-u: user指定用户

 

四个符号:

 

*:表示取值范围中的每一个数字

-:做连续区间表达式的,要想表示1~7,则可以写成:1-7

/: 每隔,例如:每隔10分钟一次,则可以在分的位置写:*/10

,:表示多个取值,比如想在1点,2点6点执行,则可以在时的位置写:1,2,6

编写格式:

*

 *

 *

*

 *  

绝对路径

执行命令

 

minute

hour

 日

Day of month

month

Day of week

 

 

0~59 

0~23 

1~31 

1~12

0~7

0和7表示星期天

 

 

 

实例:

1. 查看进程是否启动

[root@ww ~]# ps aux | grep crond

root        621  0.0  0.3 126284  1600 ?        Ss   17:46   0:00 /usr/sbin/crond -n

root       1194  0.0  0.1 112704   956 pts/0    S+   18:05   0:00 grep --color=auto crond

2. 查看计划任务

[root@ww ~]# crontab -l

no crontab for root

3. 编写计划任务

支持#号注释,不执行。

[root@ww ~]# crontab -e                   

 #编写计划任务* * * * * echo "123" >> /root/ken.txt       

[root@ww ~]# tail -f ken.txt             

  #查看计划任务执行结果123123

 

4. 删除计划任务

[root@ww ~]# crontab -l* * * * * echo "123" >> /root/ken.txt

[root@ww ~]# crontab -r

[root@ww ~]# crontab -l

no crontab for root

 

0 1 * * * /usr/bin/tar czf ` touch $ (date “+\%F””)`  /ww

 

 

posted @ 2019-06-27 18:32  wete_weiwei  阅读(223)  评论(0编辑  收藏  举报