You are not logged in.
Hello,
I have small bash script that I use to check the size of all the folders in a given directory:
$ cat /home/lcoumetou/Scripts/ds
#!/bin/bash# display the directories size
for i in *;
do if [ "$i" != "mnt" ]
then
du -sh "$i"
fi
done[~/Music/.To_sort]Arch64$ ds
127M 0 Autumn ChillOut I ll be around
1.8M CD.Lily.Allen-Alright.Still-2006-GBM
120M Cafe Del Mar - Chill out - House Vol II CD 2(Full Album) Chemical Brothers & Moby Vs Prodigy and Fat Boy Slim.mp3
158M Cafe Mambo Ibiza 08 Mixed by Andy Cato
202M Cream_Ibiza__Mixed_By_Paul_Van_Dyk
220K Gotan Project - La Revancha Del Tango - EAC Lame192 - Guxfer
319M Ibiza 2008
95M Ibiza Electrohouse 2008
113M Ibiza Megamix 2008
192M Ibiza Most Wanted (2007)
198M Las Tardes En Ibiza 2008
171M On The Beach 4 Las Salinas Ibiza (2008)
233M The Nu Sound Of Ibiza (Kontor Records)
219M Tunnel_Goes_Ibiza Vol_7
328M VA_-_Hed_Kandi_The_Mix_2009-3CD-2008-LiR
Do you think it could be possible to tweak it in order to remove every folder with a size below 5MB?
Is there a way to do a test by parsing the size a the folder?
Thanks in advance.
Ludo
Offline
Look into the find command. You will need to use the -size option
find -size n[cwbkMG]
you can then pass the 'exec rm -rf' to it and remove all folders less than 5mb
Last edited by Inxsible (2009-04-13 16:00:51)
There's no such thing as a stupid question, but there sure are a lot of inquisitive idiots !
Offline
Don't use du -h
du -s * | while read size filename; do
if [ $size -lt 5000 ]; then
echo "$filename" is under 5MB
fi
done
Offline
Look into the find command. You will need to use the -size option
find -size n[cwbkMG]
you can then pass the 'exec rm -rf' to it and remove all folders less than 5mb
A directory itself is only 4KB, so that would only work for deleting individual files under 5MB. (with -type f)
Last edited by Procyon (2009-04-13 16:03:46)
Offline
A directory itself is only 4KB, so that would only work for deleting individual files under 5MB. (with -type f)
I haven't tried it out yet, but couldn't we use '-type d' along with '-depth' with the find command to check for directories?
Last edited by Inxsible (2009-04-13 16:21:49)
There's no such thing as a stupid question, but there sure are a lot of inquisitive idiots !
Offline
Don't use du -h
du -s * | while read size filename; do if [ $size -lt 5000 ]; then echo "$filename" is under 5MB fi done
Thanks Procyon, working great
Offline
Procyon wrote:A directory itself is only 4KB, so that would only work for deleting individual files under 5MB. (with -type f)
I haven't tried it out yet, but couldn't we use -d with the find command to check for directories?
-type d? You do not want to do that. Because a directory is 4KB regardless of what is inside (sometimes a little bigger) what happens is EVERYTHING will be deleted.
As I said the only thing you can use find on for checking size is individual files.
Offline
Inxsible wrote:Procyon wrote:A directory itself is only 4KB, so that would only work for deleting individual files under 5MB. (with -type f)
I haven't tried it out yet, but couldn't we use -d with the find command to check for directories?
-type d? You do not want to do that. Because a directory is 4KB regardless of what is inside (sometimes a little bigger) what happens is EVERYTHING will be deleted.
As I said the only thing you can use find on for checking size is individual files.
I stand corrected then . Thanks for clearing that up Procyon.
There's no such thing as a stupid question, but there sure are a lot of inquisitive idiots !
Offline
Procyon wrote:Don't use du -h
du -s * | while read size filename; do if [ $size -lt 5000 ]; then echo "$filename" is under 5MB fi done
Thanks Procyon, working great
In fact, I discovered a problem
If I use that script in a folder with files, all the files (<5MB) in the folder gets deleted
Offline
That sounds terrible, I hope you didn't lose anything important. It is a risky script no matter how you look at it.
There are checks you can perform, like
[ ! -d "$filename" ] && continue
before the rm statement.
Last edited by Procyon (2009-04-13 19:37:10)
Offline
no, I haven't lose anything important as I was playing in a test folder
I just wanted to report this little *unexpected* behaviour should someone do a basic copy/paste to prevent them erasing lots of important files.
Offline