Page 1 of 1

Recursively delete small files PHP script

Posted: Fri Nov 11, 2011 10:35 am
by jole1234
How to recursively delete small files, smaller than 10 kilobytes regardless on their creation time, format or name?

PHP script needs to go through cache folder and all sub folders and delete files smaller than 10 KB, but to leave all larger files. It would be nice to show list of deleted files, but it is not must. Also it would be nice if script can be set via cron job, but also it is not must, it can do manually too.

P.S. I am not programmer, so I need already made script or at least pretty much made :-)

Best regards.

Re: Recursively delete small files PHP script

Posted: Fri Nov 11, 2011 10:49 am
by Celauran
If you're going to run it from cron, it seems silly to create this script in PHP.

Code: Select all

#!/bin/bash

find $1 -type f -size -10k -delete

exit 0

Re: Recursively delete small files PHP script

Posted: Fri Nov 11, 2011 11:05 am
by jole1234
Hello.

Thank you for your response. Like I said I am not programmer so please tell me how to use it how to set from which starting folder to start looking and deleting files. Now when I see it maybe I will need first regular script file to test it first. If you know how to help please write it for me :-).

Best regards.

Re: Recursively delete small files PHP script

Posted: Fri Nov 11, 2011 11:10 am
by Celauran
Copy the code to a text file, chmod +x the file, then run it by calling the file name. The script takes one argument: the directory to search. If none is given, it uses the current directory.

Re: Recursively delete small files PHP script

Posted: Fri Nov 11, 2011 11:30 am
by jole1234
Thank you for reply.
1.
So I need to copy and paste that code in text file, for example delete_small_files.txt and put it in folder for example public_html/cache/ then to open cron jobs under cPanel and set path to that file and it will delete only files smaller than 10kb but it will leave all folder and sub folder structure and of course all files bigger than 10kb?

2.
How to write that argument, the directory to search, for example if file is in public_html/cache/ how to tell to start search from public_html/cache/cache-files/ folder?

3.
Also will it be demanding operations if there are for example in that first/start folder 35 sub folders and each sub folder have 35 sub folders and files are in those sub folders, up to several thousands files per folder and some % of them are smaller than 10kb?

Re: Recursively delete small files PHP script

Posted: Fri Nov 11, 2011 11:38 am
by Celauran
jole1234 wrote: 1.
So I need to copy and paste that code in text file, for example delete_small_files.txt and put it in folder for example public_html/cache/ then to open cron jobs under cPanel and set path to that file and it will delete only files smaller than 10kb but it will leave all folder and sub folder structure and of course all files bigger than 10kb?

2.
How to write that argument, the directory to search, for example if file is in public_html/cache/ how to tell to start search from public_html/cache/cache-files/ folder?

3.
Also will it be demanding operations if there are for example in that first/start folder 35 sub folders and each sub folder have 35 sub folders and files are in those sub folders, up to several thousands files per folder and some % of them are smaller than 10kb?
1. Yes, though you must remember to set execute permission on the file. Also, as a matter of convention, I'd give it the .sh extension, or no extension at all.
2. It's probably best to specify an absolute path, like /home/your_username/public_html/cache
3. Yes, that might take a while. Are you really looking to delete tens of thousands of files?

Re: Recursively delete small files PHP script

Posted: Fri Nov 11, 2011 3:13 pm
by jole1234
1.
Ok, I will save that file as .sh and I will set execute permission on the file.

2.
" It's probably best to specify an absolute path, like /home/your_username/public_html/cache"

How and where to set it, somewhere in this code:

Code: Select all

#!/bin/bash

find $1 -type f -size -10k -delete

exit 0
or somewhere else?

3.
Site has about 33000 pages and they are cached in cache folder to speed up next time show, and because most of pages do not change content at all, they can be cached for very long time (for months). But caching script makes for every new or updated cached version of page some very small file, which is practically junk and can be deleted, and also "page not found" pages are cached too, for those files I need this. So, when I start using caching script I will start to use this one too. On first run this script will have to delete lots of those files, next time less, and less, and after some time most pages are cached and like I said they are cached for long time, so there will be no lot of new cached pages so there will be no to much new junk files to delete.

But still this script will need to go trough all folders and files, eventually trough up to 35000 pages in up to about 35 folders every time, right, maybe that will take time, even small percent will be deleted?

For now it is planed for about 33000 pages but in future it might be more. But practically script will never need to delete 33000 pages or something like that, most likely it will need to delete up to few thousand files (first days) and after few days probably just few hundreds, at least those are my best expectations. At the beginning I guess it will be enough to run script one time per day in first week, after that one time in few days. Site is on virtual private server 4 x 2.8 CPU core, so I hope it will run easily?

Re: Recursively delete small files PHP script

Posted: Fri Nov 11, 2011 3:20 pm
by Celauran
Don't modify the code. Pass the directory as a command line argument.

Code: Select all

you@shell$ ./filename.sh /path/to/check
I don't have 35000 small files to sacrifice, so I can't test it exactly as written. I changed -delete to -print and ran it against / on my server and it did 85000+ files in 2 seconds. Could be considerably slower to delete files, though.

Re: Recursively delete small files PHP script

Posted: Sat Nov 12, 2011 8:23 am
by jole1234
I am not sure what is "Don't modify the code. Pass the directory as a command line argument." and where to put

Code: Select all

you@shell$ ./filename.sh /path/to/check
.
Like I said I am not programmer and I never did anything via cron job too :-).

Re: Recursively delete small files PHP script

Posted: Sat Nov 12, 2011 9:11 am
by Celauran
What have you named your script? Have you found out the full path name?

Re: Recursively delete small files PHP script

Posted: Sat Nov 12, 2011 10:43 am
by jole1234
"delete_small_files.sh" file located in /home/highreso/public_html/cache
and it should start looking for files from it's sub folder (/home/highreso/public_html/cache/cache-files/)
I hope this will tell you what you need.

Re: Recursively delete small files PHP script

Posted: Sat Nov 12, 2011 4:26 pm
by Celauran
So add this line to your crontab:

Code: Select all

0 1 * * * /home/highreso/public_html/cache/delete_small_files.sh /home/highreso/public_html/cache/cache-files/
This will run the script every night at 1:00 am

Re: Recursively delete small files PHP script

Posted: Sun Nov 13, 2011 4:17 am
by jole1234
Thank you very much.
I have been looking for that for weeks. I posted same question on several php forums, and nobody couldn't figure out how to do it. You were so kind to me. Next time If I need help I will come direct to you, for sure, and I will recommend you.
Thank you again.