Linux下使用bash进行文件的匹配与删除

来源:互联网 发布:何茂春 知乎 编辑:程序博客网 时间:2024/06/07 07:40
#!/bin/bash# 程序实现删除存在"$dir"中但不在"$list"中的所有文件# "$dir"目录里的文件可以集中存放在"$dir"中,也可以以文件名的首字母分别建立子目录,如"$dir/a"中存放以'a'开头的文件。dir="/tmp/files"list="/tmp/list"# $dir :文件名首字母为a-z的文件所在的目录,本程序中为"/tmp/files"# $list:列表所在的目录,本程序中为"/tmp/list"ls -1lR $dir | grep '^-' | cut -d ' ' -f 10 | while read  line doif [ ! -f $list/$line ];thensubdir=$(echo $line | cut -c 1) rm $dir/$line 2> /dev/null || rm $dir/$subdir/$linefidone