我想在bash / python中编写一个脚本,它能够递归删除文件夹,里面没有'.txt'
个文件。
例如:
| Folder1/ --> keep
|--SubFolderA/ --> keep
|----file.txt
| Folder2/ --> delete
| Folder3/ --> keep
|--subFolderA/ --> keep
|----file.txt
| Folder4/ --> delete
|--SubFolderA/ --> delete
|----SubFolderB/ --> delete
|------file.py
| Folder5/ --> keep
|--SubFolderA/ --> keep, because .txt file is in SubFolderB
|----file.sh
|----SubFolderB/ --> keep
|------file.txt
| Folder6/ --> keep
|--SubFolderA/ --> keep
|----file.txt
|----SubFolderB/ --> delete
|------file.py
答案 0 :(得分:0)
绝对使用find。像这样:
find /path -type f -name "*.txt" -exec rm -rf {} \;
我不打算为你写if语句,但那应该让你开始......其实不论之处:
path=the/path/to/the/directory/
myvar=$(find $path -type f -name "*.txt" -exec rm -rf {} \;)
if [ -z $myvar ]
then
rmdir $path
else
#don't do anything...keep it
fi