Hi Guys!

I would like to create a shell script that checks how many files there are in a directory. If there are more than 20 then delete the ones that are older than 48 hours old.

so I have portions for a script, but I can't seem to make it work properly.

This will delete the files:

find /mydir/ -ctime 48 -exec rm -r {} \;

This will tell me how many files are in a directory:

find /mydir/  -type f | wc -l

But I can't make an if statement with "find /mydir/ -type f | wc -l" for some reason... please help!

Well, your code would look something like this:

#!/bin/sh

if [ "$(find /temp/ -type f | wc -l)" -gt 20 ]; then
	find /temp/ -ctime 48 -exec rm -r {} \;
fi

Worked beautifully! Thank you for the help!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.