Linux 磁盘已满时删除文件的Shell脚本

Linux 磁盘已满时删除文件的Shell脚本,linux,bash,shell,Linux,Bash,Shell,我正在编写一个小脚本,以便在缓存目录变得太大时,每天通过CRON清除linux上的空间。 因为我对bash脚本非常熟悉,所以我需要一些来自linux大师的帮助 下面是基本的逻辑(伪代码) 这是由于linux系统固有的无法“rm”一个目录,如果它包含太多的文件,正如我在过去了解到的那样。要检测文件系统的占用情况,我使用以下方法: df -k $FILESYSTEM | tail -1 | awk '{print $5}' 这给了我文件系统的占用百分比,这样我就不需要计算:) 如果您使用bash,

我正在编写一个小脚本,以便在缓存目录变得太大时,每天通过CRON清除linux上的空间。 因为我对bash脚本非常熟悉,所以我需要一些来自linux大师的帮助

下面是基本的逻辑(伪代码)


这是由于linux系统固有的无法“rm”一个目录,如果它包含太多的文件,正如我在过去了解到的那样。

要检测文件系统的占用情况,我使用以下方法:

df -k $FILESYSTEM | tail -1 | awk '{print $5}'
这给了我文件系统的占用百分比,这样我就不需要计算:)

如果您使用bash,那么可以使用pushd/popd操作来更改目录并确保处于

pushd '/home/user/lotsa_cache_files/'
do the stuff
popd

我会这样做:

# get the available space left on the device
size=$(df -k /dev/sda5 | tail -1 | awk '{print $4}')

# check if the available space is smaller than 5GB (5000000kB)
if (($size<5000000)); then
  # find all files under /home/user/lotsa_cache_files and delete them
  find /home/user/lotsa_cache_files -name "*" -delete
fi
#获取设备上剩余的可用空间
大小=$(df-k/dev/sda5 | tail-1 | awk'{print$4}')
#检查可用空间是否小于5GB(5000000kB)
如果($size只是另一个提案(代码中的注释):


从crontab调用脚本以执行计划的清理

以下是我用来删除目录中的旧文件以释放空间的脚本

#!/bin/bash
#
#  prune_dir - prune directory by deleting files if we are low on space
#
DIR=$1
CAPACITY_LIMIT=$2

if [ "$DIR" == "" ]
then
    echo "ERROR: directory not specified"
    exit 1
fi

if ! cd $DIR
then
    echo "ERROR: unable to chdir to directory '$DIR'"
    exit 2
fi

if [ "$CAPACITY_LIMIT" == "" ]
then
    CAPACITY_LIMIT=95   # default limit
fi

CAPACITY=$(df -k . | awk '{gsub("%",""); capacity=$5}; END {print capacity}')

if [ $CAPACITY -gt $CAPACITY_LIMIT ]
then
    #
    # Get list of files, oldest first.
    # Delete the oldest files until
    # we are below the limit. Just
    # delete regular files, ignore directories.
    #
    ls -rt | while read FILE
    do
        if [ -f $FILE ]
        then
            if rm -f $FILE
            then
                echo "Deleted $FILE"

                CAPACITY=$(df -k . | awk '{gsub("%",""); capacity=$5}; END {print capacity}')

                if [ $CAPACITY -le $CAPACITY_LIMIT ]
                then
                    # we're below the limit, so stop deleting
                    exit
                fi
            fi
        fi
    done
fi
我是这样做的:


读取f;do rm-rf${f};done

“如果目录包含太多文件,linux系统固有的无法“rm”目录”是否也意味着“rm-rf”?这就是我的建议:在
find
命令中对目录进行硬编码,不要使用xargs,因为这可能会很危险。请再次检查代码!它似乎会删除当前文件夹中的文件。@Max它会
cd$DIR
所以不会。我建议使用
-delete
标志而不是
-execRM- F{};。还考虑添加<代码> -XDEV < /Cord>作为第一个标志,以保持在同一文件系统中。
pushd '/home/user/lotsa_cache_files/'
do the stuff
popd
# get the available space left on the device
size=$(df -k /dev/sda5 | tail -1 | awk '{print $4}')

# check if the available space is smaller than 5GB (5000000kB)
if (($size<5000000)); then
  # find all files under /home/user/lotsa_cache_files and delete them
  find /home/user/lotsa_cache_files -name "*" -delete
fi
FILESYSTEM=/dev/sda1 # or whatever filesystem to monitor
CAPACITY=95 # delete if FS is over 95% of usage 
CACHEDIR=/home/user/lotsa_cache_files/

# Proceed if filesystem capacity is over than the value of CAPACITY (using df POSIX syntax)
# using [ instead of [[ for better error handling.
if [ $(df -P $FILESYSTEM | awk '{ gsub("%",""); capacity = $5 }; END { print capacity }') -gt $CAPACITY ]
then
    # lets do some secure removal (if $CACHEDIR is empty or is not a directory find will exit
    # with error which is quite safe for missruns.):
    find "$CACHEDIR" --maxdepth 1 --type f -exec rm -f {} \;
    # remove "maxdepth and type" if you want to do a recursive removal of files and dirs
    find "$CACHEDIR" -exec rm -f {} \;
fi 
#!/bin/bash
#
#  prune_dir - prune directory by deleting files if we are low on space
#
DIR=$1
CAPACITY_LIMIT=$2

if [ "$DIR" == "" ]
then
    echo "ERROR: directory not specified"
    exit 1
fi

if ! cd $DIR
then
    echo "ERROR: unable to chdir to directory '$DIR'"
    exit 2
fi

if [ "$CAPACITY_LIMIT" == "" ]
then
    CAPACITY_LIMIT=95   # default limit
fi

CAPACITY=$(df -k . | awk '{gsub("%",""); capacity=$5}; END {print capacity}')

if [ $CAPACITY -gt $CAPACITY_LIMIT ]
then
    #
    # Get list of files, oldest first.
    # Delete the oldest files until
    # we are below the limit. Just
    # delete regular files, ignore directories.
    #
    ls -rt | while read FILE
    do
        if [ -f $FILE ]
        then
            if rm -f $FILE
            then
                echo "Deleted $FILE"

                CAPACITY=$(df -k . | awk '{gsub("%",""); capacity=$5}; END {print capacity}')

                if [ $CAPACITY -le $CAPACITY_LIMIT ]
                then
                    # we're below the limit, so stop deleting
                    exit
                fi
            fi
        fi
    done
fi