我有一个很大的共享(~5TB)即将满。现在我想制作一个脚本来删除 2 个指定文件夹中的数据。但这需要是最旧的文件/文件夹,并且需要在删除约 50GB 时停止,因此它不会删除所有文件夹。
编辑:这需要与我的 Synology DS-409 的 Samba 共享配合使用。该脚本需要在 Synology 的 /etc/crontab 中运行。
他们在其他地方给了我这个代码:
#!/opt/bin/bash
dir=/data/video
min_dirs=3
full=60
logfile=/var/tmp/removed.log
df=`df | grep data | awk '{print $5}' | sed s/%//g`
if [ $df -gt $full ]; then
[[ $(find "$dir" -type d | wc -l) -ge $min_dirs ]] &&
IFS= read -r -d $'\0' line < <(find "$dir" -printf '%T@ %p\0' 2>/dev/null | sort -z -n)
file="${line#* }"
ls -lLd "$file"
#rm -rf "$file"
date=`date`
if [ -f "$file" ]; then
echo "$date $file could not be removed!" >> $logfile
else
echo "$date $file removed" >> $logfile
fi
fi
答案1
这应该有效:
DIRS="a/ b/"
MAXDELBYTES="53687091200" # 50GB
DELBYTES="0"
find $DIRS -type f -printf "%T@ %s %p\n" | sort -r -n | while read time bytes filename
do
rm -fv "$filename"
DELBYTES=$((DELBYTES + bytes))
if [ $DELBYTES -ge $MAXDELBYTES ]; then break; fi
done
答案2
scai, autodelete.txt 是我在 Windows 中创建的文件并上传到 Linux 共享:) 现在由于 Windows Linux 代码问题,我在 Nano 中编写了此代码。
但现在它给出了一堆错误
root ~/.config # sh autodelete
find: unrecognized: -printf
BusyBox v1.20.2 (2012-08-09 05:49:15 CEST) multi-call binary.
Usage: find [PATH]... [OPTIONS] [ACTIONS]
Search for files and perform actions on them.
First failed action stops processing of current file.
Defaults: PATH is current directory, action is '-print'
-follow Follow symlinks
Actions:
! ACT Invert ACT's success/failure
ACT1 [-a] ACT2 If ACT1 fails, stop, else do ACT2
ACT1 -o ACT2 If ACT1 succeeds, stop, else do ACT2
Note: -a has higher priority than -o
-name PATTERN Match file name (w/o directory name) to PATTERN
-iname PATTERN Case insensitive -name
-path PATTERN Match path to PATTERN
-ipath PATTERN Case insensitive -path
-type X File type is X (one of: f,d,l,b,c,...)
-links N Number of links is greater than (+N), less than (-N),
or exactly N
If none of the following actions is specified, -print is assumed
-print Print file name
-exec CMD ARG ; Run CMD with all instances of {} replaced by
file name. Fails if CMD exits with nonzero
autodelete: line 11: bytes: not found