shell:写一个删除指定文件或者目录的脚本

本文介绍了一个bash脚本,用于删除指定名称的模板文件。该脚本接收一个参数作为要删除的文件名,并在两个指定目录中查找并删除匹配的文件夹。

#!/bin/bash                                                                                                                                                                                                                                  
# This script deletes the specified template file                                                                                                                                                                                            
                                                                                                                                                                                                                                             
#Parameters passed in                                                                                                                                                                                                                        
temp=$1                                                                                                                                                                                                                                      
                                                                                                                                                                                                                                             
#The purpose of this variable is concatenation                                                                                                                                                                                               
vendor=Grandstream_                                                                                                                                                                                                                          
                                                                                                                                                                                                                                             
#to dDir2                                                                                                                                                                                                                                    
vendor_temp="$vendor$temp"                                                                                                                                                                                                                   
                                                                                                                                                                                                                                             
dDir1=/data/zeroconfig                                                                                                                                                                                                                       
dDir2=/data/zeroconfig/model_template/models                                                                                                                                                                                                 
                                                                                                                                                                                                                                             
#delete the specified file                                                                                                                                                                                                                   
for i in $dDir1/*                                                                                                                                                                                                                            
do                                                                                                                                                                                                                                           
                                                                                                                                                                                                                                             
if [ -d $i ]                                                                                                                                                                                                                                 
then                                                                                                                                                                                                                                         
        if [ $i = $dDir1/$temp ]                                                                                                                                                                                                             
        then                                                                                                                                                                                                                                 
                rm -rf $i                                                                                                                                                                                                                    
                break                                                                                                                                                                                                                        
        fi                                                                                                                                                                                                                                   
fi                                                                                                                                                                                                                                           
done                                                                                                                                                                                                                                         
                                                                                                                                                                                                                                             
#delete the specified file                                                                                                                                                                                                                   
for i in $dDir2/*                                                                                                                                                                                                                            
do                                                                                                                                                                                                                                           
if [ -d $i ]                                                                                                                                                                                                                                 
then                                                                                                                                                                                                                                         
        if [ $i = $dDir2/$vendor_temp ]                                                                                                                                                                                                      
        then                                                                                                                                                                                                                                 
                rm -rf $i                                                                                                                                                                                                                    
                break                                                                                                                                                                                                                        
        fi                                                                                                                                                                                                                                   
fi                                                                                                                                                                                                                                           
done

说明:

dDir1、dDir2是路径,视具体情况而定,我就要删两个路径下的内容,所以使用了两个路径

在命令行输入 xxx.sh xx

xx:你要删除的文件名称

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

AnJunDeng

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值