我想在 Linux 中尋求幫助。我有一個主檔案夾,其中包含三個包含檔案的子檔案夾。
我需要洗掉子檔案夾中滿足條件的子檔案夾中的所有檔案......子檔案夾大小小于5kb,并且該子檔案夾中的檔案早于5分鐘。
我這樣做了,但我發現它完全錯誤。
find ./main/* -type 'f' -mmin 5 -size -5k -delete
結構: 結構
檔案夾:主檔案夾 -> 子檔案夾:第一個(2 個檔案),第二個(2 個檔案),第三個(2 個檔案)見圖
我想為子檔案夾-“第二個”做一個例外。我的意思是這些規則不適用于“第二個”子檔案夾。
所以我做了這個。
find ./main/* -type 'd' -size -5k -not -path "./main/second"
但是我不知道如何實作我對檔案使用條件的部分(超過5分鐘),然后這些檔案將被洗掉。
感謝您的任何幫助。
我試過這個代碼。但它總是其他的
#! /usr/bin/bash
d="./main/*"
f="./main/*"
z=0
if [ "$d" == TRUE ] && ["$f" == TRUE ]
then
$z="find $d -type 'd' -size -5k $f -type 'f' -mmin 5 -delete"
echo "$z"
else
echo "nothing"
fi
它總是其他......:/
總結:我需要創建一個 bash 腳本來檢查主檔案夾中的所有子檔案夾。如果發現子檔案夾的大小小于 5kb。因此,腳本會檢查該子檔案夾中的檔案以查看檔案是否超過 5 分鐘以及是否滿足所有這些條件。這將洗掉該子檔案夾中的所有檔案。
uj5u.com熱心網友回復:
我需要洗掉子檔案夾中滿足條件的子檔案夾中的所有檔案......子檔案夾大小小于5kb,并且該子檔案夾中的檔案早于5分鐘。
find -size不適用于檔案夾(至少不是您期望的方式)。你將不得不使用類似的東西du。
這是一個應該執行您想要實作的目標的腳本:
#!/usr/bin/env bash
# The main directory
maindir="./main"
# Get list of subdirs in maindir
readarray -t subdirs < <(find "${maindir}" -mindepth 1 -maxdepth 1 -type d -not -path "${maindir}/second" | sort)
# Process list of subdirs
for subdir in "${subdirs[@]}"; do
# Determine size of subdir
size=$(du -bs "${subdir}" | awk '{ print $1 }')
# Skip subdirs that exceed 5k
if (( ${size} > 5 * 1024 )); then
echo "Skipping subdir '${subdir}' (size: ${size} bytes)"
continue
fi
# Find and delete files in subdir older than 5 min
echo "Deleting files in subdir '${subdir}'..."
find "${subdir}" -type f -mmin 5 -delete
done
OP 之前的先前答案提供了更多詳細資訊 - 保留原樣以供將來參考:
關于你的find命令:
我需要洗掉子檔案夾中滿足條件的子檔案夾中的所有檔案......子檔案夾大小小于5kb,并且該子檔案夾中的檔案早于5分鐘。
這將是:
find ./main -type f -size -5k -mmin 5 -not -path "./main/second/*" -delete
解釋:
- 您想洗掉檔案,因此您需要使用
-type f -path需要通配符才能正常運行
關于您的腳本代碼:
[ "$d" == TRUE ]進行字串比較。您設定d="./main/*",因此您測驗[ "./main/*" == "TRUE" ],這永遠不會正確,因為它們是不同的字串(與 相同$f)- 如果你想真正運行 find 命令,你需要這樣做:
$z=$("find $d -type 'd' -size -5k $f -type 'f' -mmin 5 -delete"),但$z在此之后將為空,因為 find 在-delete使用時不會列印任何內容
如果你需要進一步的建議,你需要提供更多關于你對腳本的意圖的資訊——目前,這不是很清楚(至少對我來說)。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/321758.html
