Thursday, May 2, 2024
1
rated 0 times [  1] [ 0]  / answers: 1 / hits: 1603  / 2 Years ago, tue, april 19, 2022, 1:58:34

What command can I execute to check the directories inside a folder and if the total size of the directory is smaller than 30MB, delete it? I played around with the find command trying to move files based in the size but it didn't work properly.



I was using (for 1mb):



find . -size +102400k -exec mv DIRECTORY
find . -size -102400k -exec mv DIRECTORY

More From » command-line

 Answers
1

If we're talking about directory size, I'm not sure find is going to help. du is going to have to be involved at some point.



Here's something I have written to find near directories under 30M:



du --max-depth 1 | awk -v q='"' '$1 < 30000000 && $2 != "." {sub(/^[0-9	 ]+/, "", $0); print q $0 q}'


You can then chain that into xargs rm -rf but I would test it very seriously before doing that. You might want to move the directories (with mv) instead of straight-up deleting them.


[#39627] Thursday, April 21, 2022, 2 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
pilun

Total Points: 270
Total Questions: 100
Total Answers: 94

Location: England
Member since Sat, Feb 13, 2021
3 Years ago
;