2011-05-06 154 views
9

如果缓存目录变得太大,我正在写一个小小的脚本来清除我的Linux上每天通过CRON的空间。 因为我在bash脚本编程中真的很绿,所以我需要一点点的帮助。shell脚本在磁盘已满时删除文件

这里是基本逻辑(伪代码)

if (Drive Space Left < 5GB) 
    { 
     change directory to '/home/user/lotsa_cache_files/' 

     if (current working directory = '/home/user/lotsa_cache_files/') 
     { 
      delete files in /home/user/lotsa_cache_files/ 
     } 
    } 

获取驱动器的剩余空间

我打算获得从“的/ dev/SDA5”命令留下的驱动器空间。 如果下面的值返回给我您的信息:

Filesystem   1K-blocks  Used Available Use% Mounted on<br> 
/dev/sda5   225981844 202987200 11330252 95%/

那么一点点正则表达式可能需要得到“11330252”出返回值

有点偏执

的“如果(当前工作目录=/home/user/lotsa_cache_files /)'部分仅仅是我内心偏执狂的防御机制。在我继续使用删除命令之前,我想确保我确实在'/ home/user/lotsa_cache_files /'中,如果当前工作目录由于某种原因而不存在,这可能具有破坏性。

删除文件

文件的删除将与命令下面而不是通常的RM -f来完成:

find . -name "*" -print | xargs rm 

这是由于Linux系统的固有的不能为“RM”一个目录,如果它包含太多的文件,就像我以前学过的那样。

+3

“如果Linux系统包含的文件过多,那么Linux系统固有的不可能'rm'目录”这是否也意味着“rm -rf”? – rzetterberg 2011-05-06 08:45:20

回答

10

又一个提议(在代码中的注释):

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 

呼叫从crontab中的脚本来完成预定清洗

+0

我建议使用'-delete'标志而不是'-exec rm -f {} \;'。另外考虑添加'-xdev'作为保持在同一文件系统中的第一个标志。 – 2017-11-28 08:07:37

3

为了检测文件系统的职业,我用这个:

df -k $FILESYSTEM | tail -1 | awk '{print $5}' 

这给我的文件系统,这样的占有百分比,我不需要计算它:)

如果你使用bash,您可以使用pushd/popd的操作来更改目录,并一定要在

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

我会做这种方式:

# 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 
+0

这就是我要推荐的:在'find'命令中对目录进行硬编码,不要使用xargs,因为它可能很危险。 – 2011-05-06 18:36:04

1

下面是我用一个目录中删除旧文件,以腾出空间脚本...

#!/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 
+0

请再次检查代码!它似乎会删除当前文件夹中的文件。 – Max 2017-06-03 20:56:00

+0

@Max它'cd $ DIR',所以它不会。 – dgpro 2017-08-15 11:18:53