PDA

View Full Version : Quick bit of bash help



paul.maddox
May 9th, 2006, 01:44 PM
I'm trying to write a quick bash script to delete all files in a directory under a certain filesize (say for example 1MB).

Has anyone got any good ways of getting that list of files?

I've checked the list of bash test operators and can't find anything about filesizes apart from one to check if it's bigger than 0bytes.

So far the best I can think of is looping through all files in the directory, then running ls -s on them to get the filesize, parse that and then compare to see if it's bigger/smaller than 1MB.

Is there a much nicer way of doing it? There must be!

Engnome
May 9th, 2006, 02:21 PM
I dont know any bash scripting, but I in most programming languages doing what you describe would turn out to be something like:

for(every file in folder)
{
sizeof(file)
if(size > 1mb) {
delete
}

:cool: You should google for some bash guides.

mostwanted
May 9th, 2006, 02:28 PM
There's a programming forum.

kabus
May 9th, 2006, 02:30 PM
'find' can test for size.

paul.maddox
May 9th, 2006, 02:40 PM
There's a programming forum.
Sorry, my mistake I did not see it.



'find' can test for size.
Thanks! Looking at find's manpage it looks like I can do:

find /downloads/music/ripped/*mp3 -size -1500k -delete


Cheers!