You are not logged in.

#1 2009-04-13 15:44:11

Coume
Member
From: UK
Registered: 2008-02-10
Posts: 78
Website

[Bash] Delete a folder if size < 5MB

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

#2 2009-04-13 15:58:42

Inxsible
Forum Fellow
From: Chicago
Registered: 2008-06-09
Posts: 9,183

Re: [Bash] Delete a folder if size < 5MB

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)


Forum Rules

There's no such thing as a stupid question, but there sure are a lot of inquisitive idiots !

Offline

#3 2009-04-13 15:59:06

Procyon
Member
Registered: 2008-05-07
Posts: 1,819

Re: [Bash] Delete a folder if size < 5MB

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

#4 2009-04-13 16:02:59

Procyon
Member
Registered: 2008-05-07
Posts: 1,819

Re: [Bash] Delete a folder if size < 5MB

Inxsible wrote:

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

#5 2009-04-13 16:19:16

Inxsible
Forum Fellow
From: Chicago
Registered: 2008-06-09
Posts: 9,183

Re: [Bash] Delete a folder if size < 5MB

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 '-type d' along with '-depth' with the find command to check for directories?

Last edited by Inxsible (2009-04-13 16:21:49)


Forum Rules

There's no such thing as a stupid question, but there sure are a lot of inquisitive idiots !

Offline

#6 2009-04-13 16:21:42

Coume
Member
From: UK
Registered: 2008-02-10
Posts: 78
Website

Re: [Bash] Delete a folder if size < 5MB

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 smile

Offline

#7 2009-04-13 16:23:33

Procyon
Member
Registered: 2008-05-07
Posts: 1,819

Re: [Bash] Delete a folder if size < 5MB

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.

Offline

#8 2009-04-13 16:26:00

Inxsible
Forum Fellow
From: Chicago
Registered: 2008-06-09
Posts: 9,183

Re: [Bash] Delete a folder if size < 5MB

Procyon wrote:
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 smile. Thanks for clearing that up Procyon.


Forum Rules

There's no such thing as a stupid question, but there sure are a lot of inquisitive idiots !

Offline

#9 2009-04-13 19:27:01

Coume
Member
From: UK
Registered: 2008-02-10
Posts: 78
Website

Re: [Bash] Delete a folder if size < 5MB

Coume wrote:
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 smile

In fact, I discovered a problem hmm
If I use that script in a folder with files, all the files (<5MB) in the folder gets deleted sad

Offline

#10 2009-04-13 19:36:20

Procyon
Member
Registered: 2008-05-07
Posts: 1,819

Re: [Bash] Delete a folder if size < 5MB

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

#11 2009-04-13 19:49:25

Coume
Member
From: UK
Registered: 2008-02-10
Posts: 78
Website

Re: [Bash] Delete a folder if size < 5MB

no, I haven't lose anything important as I was playing in a test folder wink

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

Board footer

Powered by FluxBB