Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I need to have a Cron Job, that when it runs automatically deletes all of the files inside of a folder that are exactly 0 bytes.

The script I am using allows users to enter their username so it downloads their file. However, I want the script to be basic so there is nothing that checks to see if the file exists. If it doesn't exists, it uploads nothing, but it still uploads a blank file that contains nothing, and is exactly 0 bytes.

I could not find anything on this, but I could have been searching for the wrong things as Im not exactly sure what this would be called.

share|improve this question

1 Answer 1

To find all 0 byte files in directory, you can use this command:

find directory -maxdepth 1 -size 0

Remove the -maxdepth 1 switch if you want to search in sub-directories as well.

To delete those files, just add a -delete switch at the end:

find directory -maxdepth 1 -size 0 -delete
share|improve this answer
    
Testing it now, Ill let you know results. –  Kyle Apr 12 '13 at 20:41
    
Doesn't appear to be working for me, all I have to do is replace directory with the exact location right? I do know that the directory is correct, what else could be going wrong? –  Kyle Apr 12 '13 at 20:54
    
Did you try directly from the command line or as a cron job? For a crontab entry, you might have to specify the full path of find. To debug a cron job, add 2>> pathtologfile and check the log file for error messages. –  Dennis Apr 12 '13 at 21:19
    
(1) Some versions of find support a -empty predicate (a.k.a. “primary”) that means -size 0. (2) Not all versions of find support -delete. -exec rm is more portable. –  Scott Apr 13 '13 at 0:28
    
When will this cron job run? If it will run in the middle of the night, when users are absolutely guaranteed not to be active, ignore this next part. But if it will run at a time when users might be active, I suggest adding -mtime +1, so the command will handle only files that are at least a day old. This precaution is to protect against the race condition where a user starts to upload a file, and then the cron job runs and deletes it, and then user’s script finishes uploading the file –– but, too late; it’s already been deleted. –  Scott Apr 13 '13 at 0:30

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.