I would like to ask for help in Linux. I have a main folder where are three subfolders with files.
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.
I did this, but I found it completely wrong.
find ./main/* -type 'f' -mmin 5 -size -5k -delete
Structure: Structure
Folder: Main -> Subfolders: first(2 files), second(2 files), third(2 files) see picture
And I would like to make an exception for the subfolder - "second". I mean these rules will not work on the "second" subfolder.
So I did this.
find ./main/* -type 'd' -size -5k -not -path "./main/second"
But I don't know how to implement the part where I use the condition for files(older than 5 minutes) and then these files will be deleted.
Thank you for any help.
I tried this code. But it's always else
#! /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
and it is always else... :/
summarize: I need to create a bash script that checks all the subfolders in the main folder. And if it finds that a subfolder is less than 5kb in size. So the script checks the files in that subfolder to see if the files are older than 5 minutes and if all these conditions are met. This will delete all files from that subfolder.
CodePudding user response:
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
will not work on folders (at least not in the way you expect it to). You will have to use something like du
for this.
Here is a script that should do what you want to achieve:
#!/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
Previous answer before OP provided more details - leaving this as is for future reference:
Regarding your find
command:
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.
This would be:
find ./main -type f -size -5k -mmin 5 -not -path "./main/second/*" -delete
Explanation:
- You want to delete files, thus you need to find files using
-type f
-path
needs a wildcard to function properly
Regarding your Bash script code:
[ "$d" == TRUE ]
does a string comparison. You setd="./main/*"
, so you test for[ "./main/*" == "TRUE" ]
, which is never true as those are different strings (same for$f
)- If you want to actually run the find command, you need to do it like this:
$z=$("find $d -type 'd' -size -5k $f -type 'f' -mmin 5 -delete")
, but$z
will be empty after this, as find does not print anything when-delete
is used
If you want further advice, you need to provide more information about what your intentions are with the script - currently, this is not very clear (at least to me).