如何按时间和大小删除子文件夹中的所有文件
How to delete all files in subfolder by time and size
我想在Linux寻求帮助。我有一个主文件夹,其中包含三个包含文件的子文件夹。
我需要删除子文件夹中满足条件的所有文件...子文件夹大小小于5kb且该子文件夹中的文件早于5分钟
我这样做了,但我发现完全错了。
find ./main/* -type 'f' -mmin +5 -size -5k -delete
结构:
Structure
文件夹:主文件夹 -> 子文件夹:第一个(2 个文件),第二个(2 个文件),第三个(2 个文件)见图
我想为子文件夹 - “second”破例。我的意思是这些规则不适用于“第二个”子文件夹。
所以我这样做了。
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 分钟以及是否满足所有这些条件。这将删除该子文件夹中的所有文件。
I need to delete all files in the subfolders where the subfolder meet the conditions... the subfolder size is smaller than 5kb and the files in this subfolder are older than 5 minutes.
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 }')
# 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
命令:
I need to delete all files in the subfolders where the subfolder meet the conditions... the subfolder size is smaller than 5kb and the files in this subfolder are older than 5 minutes.
这将是:
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
时的任何内容
如果您需要进一步的建议,您需要提供更多关于您对脚本的意图的信息 - 目前,这不是很清楚(至少对我而言)。
我想在Linux寻求帮助。我有一个主文件夹,其中包含三个包含文件的子文件夹。
我需要删除子文件夹中满足条件的所有文件...子文件夹大小小于5kb且该子文件夹中的文件早于5分钟
我这样做了,但我发现完全错了。
find ./main/* -type 'f' -mmin +5 -size -5k -delete
结构: Structure
文件夹:主文件夹 -> 子文件夹:第一个(2 个文件),第二个(2 个文件),第三个(2 个文件)见图
我想为子文件夹 - “second”破例。我的意思是这些规则不适用于“第二个”子文件夹。
所以我这样做了。
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 分钟以及是否满足所有这些条件。这将删除该子文件夹中的所有文件。
I need to delete all files in the subfolders where the subfolder meet the conditions... the subfolder size is smaller than 5kb and the files in this subfolder are older than 5 minutes.
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 }')
# 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
命令:
I need to delete all files in the subfolders where the subfolder meet the conditions... the subfolder size is smaller than 5kb and the files in this subfolder are older than 5 minutes.
这将是:
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
时的任何内容
如果您需要进一步的建议,您需要提供更多关于您对脚本的意图的信息 - 目前,这不是很清楚(至少对我而言)。