最近在学shell ,书上有个备份文件实例,突然想到虚拟机root目录以前做了多备份,很乱,想批量删除,就有此实例了。
- [root@bogon ~]# cat rmback.sh
- #!/bin/bash
- #rm backfile
- for file in /root/*
- do
- if [ -f ${file} ]
- then
- if [ -e ${file}.bak ]
- then
- rm -rf ${file}.bak
- if [ "$?" -eq "0" ];then
- echo "${file}.bak has been deleted"
- else
- echo "${file} can't be deleted"
- fi
- fi
- fi
- done
- [root@bogon ~]# cat rmback.pl
- #!/usr/bin/perl
- #chulianwang@qq.com
- #2012.3.2
- #Recursion delete the dircetory so backfile
- use strict;
- use warnings;
- use File::Find;
- my @DIR = "/home/"; #定义遍历的目录
- find(\&wanted, @DIR);
- sub wanted { (/\.bak/) ? unlink $_ : print "$File::Find::name\n" }; #如果匹配.bak文件,删除,否则就输出文件路径
大家谁会用shell写个遍历目录的脚本,发个瞧瞧