一个安全删除文件的shell命令

本文介绍了一种安全删除文件的方法,通过将文件移动到备份目录并加上时间戳标记来实现。此外,还提供了一个用于清理备份目录中过期文件的脚本,确保备份目录大小不超过设定限制。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

以下脚本中的函数可以安全用来删除

delete xxxx

该命令会将xxx文件(或目录(不要再目录后加/))移动的~/backup下面,并同时打上时间标签,通过运行

delete_old

会将~/backup目录下的超过3000M的最旧的文件或目录删除掉,希望数据保护还是有一定的用处


# Description:  This function used to move some useless files in current folder to ~/backup/
#      Author:  fulinux
#     Version:  1.0.0 (Release by fulinux on 8th April. 2013)


function delete ()
{
   if [ -z $1 ]; then
      echo "delete it to backuo directory"
      echo "Usage: $FUNCNAME [express]"
      echo "Example: $FUNCNAME fulinux/"
      return;
   fi


   express=$1
   timestamp=$(date +%Y%m%d%H%M%S)


   rechristen=$1.$timestamp


   mv "$express" ~/backup/"$rechristen"
}


# Description:  This function used to search dir to fetch the oldest file
#      Author:  fulinux
#     Version:  1.0.0 (Release by fulinux on 8th April. 2013)


searchdir()
{
    oldestfile=`ls -rt | head -n 1 |awk '{print $1}'`
}


# Description:  This function used to remove oldest useless files in ~/backup folder if its size is more than 4000M
#      Author:  fulinux
#     Version:  1.0.0 (Release by fulinux on 8th April. 2013)


#This is the directory where backup files are kept
backup=~/backup
#maximum useless files
alarmrate=3000


function delete_old()
{
    cd $backup
    while true;
    do
        backup_size=`du -ms $backup | awk '{print $1}'`
        if [ $backup_size -gt $alarmrate ];
        then
            searchdir
            delete $oldestfile
        else
            break;
        fi
    done
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

fulinux

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

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

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

打赏作者

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

抵扣说明:

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

余额充值