我有一个系统在运行。它为每天创建一个文件夹,并按日期命名。在每个文件夹中,它存储来自安全摄像头的视频,按时间命名文件。你会有这样的东西:

这些文件夹被上传到云端,一个文件一个文件地创建。但是,我的本地存储限制是16 is。
通过这种方式,我制作了一个脚本(在bash中),以便在存储达到一定百分比时继续删除旧文件。这就是脚本应该做的事情:
- If only one folder:
- Go to the only folder and delete the 10 oldest files.这是脚本本身(https://github.com/andreluisos/motioneyeos/blob/master/storage_清洁剂_脚本/存储_清洁剂_script.sh):
#! /bin/sh
##### VARIABLES #####
RootFolder="/data/output/Camera1" #Must change to your default output folder. Originally, it's "/data/output/Camera1".
cd $RootFolder
FileCounter=$(find . -name "*.mkv" -o -name "*.avi" -o -name "*.swf" -o -name "*.flv" -o -name "*.mov" -o -name "*.mp4" | wc -l | xargs) #Must change, if the output format if different than the basic ones available originally on MotionEyeOS.
FolderCounter=$(find . -mindepth 1 -maxdepth 1 -type d | wc -l | xargs)
CurrentStorage=`df -h | grep -vE "^Filesystem|tmpfs|/tmp|/boot|/home|/dev/root" | awk '{print $5}' | cut -d'%' -f1`
StorageThreshold=75 #Define the storage % to trigger the script.
##### FUNCTIONS #####
function multiplefolders () {
echo "More than one folder detected."
cd `ls -Ft | grep '/问题是,脚本没有(显然)计算最老文件夹中的文件数量(当检测到多个文件夹时)。它不再返回根文件夹并删除最老的空文件夹,而是从最新的文件夹中删除文件(显然)。换句话说,当您有两个文件夹(最老的文件夹是空的,最新的文件夹中有视频),它应该删除最老的文件夹和空的文件夹,但是我一直得到:More than one folder detected.
Entering the oldest folder...
Deleting the oldest files... | tail -1`
echo "Entering the oldest folder..."
if [ "$FileCounter" -eq 0 ];
then
echo "No Files detected."
cd $RootFolder
echo "Going back to the output's root folder..."
rm -rf `ls -Ft | grep '/问题是,脚本没有(显然)计算最老文件夹中的文件数量(当检测到多个文件夹时)。它不再返回根文件夹并删除最老的空文件夹,而是从最新的文件夹中删除文件(显然)。换句话说,当您有两个文件夹(最老的文件夹是空的,最新的文件夹中有视频),它应该删除最老的文件夹和空的文件夹,但是我一直得到:A32 | tail -1`
echo "Detecting and removing the oldest empty folder...";
else
ls -t | tail -10 | xargs rm
echo "Deleting the oldest files...";
fi
}
function singlefolder () {
echo "Only one folder detected."
echo "Entering the only folder..."
cd `ls -Ft | grep '/问题是,脚本没有(显然)计算最老文件夹中的文件数量(当检测到多个文件夹时)。它不再返回根文件夹并删除最老的空文件夹,而是从最新的文件夹中删除文件(显然)。换句话说,当您有两个文件夹(最老的文件夹是空的,最新的文件夹中有视频),它应该删除最老的文件夹和空的文件夹,但是我一直得到:A32 | head -1`
ls -t | tail -10 | xargs rm
echo "Deleting the oldest files."
}
function checkfolders () {
if [ "$FolderCounter" -gt 1 ];
then
multiplefolders
fi
if [ "$FolderCounter" -eq 1 ];
then
singlefolder
fi
if [ "$FolderCounter" -eq 0 ];
then
echo "No folders detected. Please check if the output folder's path is correct."
fi
}
##### SCRIPT STARTER #####
if [ $CurrentStorage -ge $StorageThreshold ];
then
checkfolders
else
echo "Storage threshold not yet reached."
fi问题是,脚本没有(显然)计算最老文件夹中的文件数量(当检测到多个文件夹时)。
它不再返回根文件夹并删除最老的空文件夹,而是从最新的文件夹中删除文件(显然)。
换句话说,当您有两个文件夹(最老的文件夹是空的,最新的文件夹中有视频),它应该删除最老的文件夹和空的文件夹,但是我一直得到:
A32
发布于 2018-09-04 19:45:26
我希望我们不需要担心我们是有一个文件夹还是多个文件夹。如果我们删除文件w.r.t修改的时间。
只需检查存储并删除文件夹中最老的10个文件
if [ $CurrentStorage -ge $StorageThreshold ];
then
find $RootFolder -type f -printf '%T+ %p\n' | sort | head -n 10 | awk '{print $NF}' | xargs rm -f
else
echo "Storage threshold not yet reached."
fi-type f -printf '%T+ %p\n'打印带有上次修改的时间戳的文件。sort,以获取顶部最古老的文件。head -n 10来获取10个最老的文件。awk '{print $NF}'提取文件路径。xargs rm -f删除提取的文件。For MAC:
find $RootFolder -type f -print0 | xargs -0 ls -ltr | head -n 10 | awk '{print $NF}' | xargs rm -f一个空文件夹很难占据4Kb的空间。如果要删除除最新文件夹之外的所有空文件夹,请包括下面的代码。
find $RootFolder -type d -empty -printf '%T+ %p\n' | sort | head -n -1 | xargs rm -rf或
ls -lt $RootFolder/* | awk -F ":" '/total 0/{print last}{last=$1}' | tail -n +2 | xargs rm -rfhttps://unix.stackexchange.com/questions/466849
复制相似问题