Sign up ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

I need a bash script (not sure how to write the actual .sh file) which I could set to be run by cron every minute and that would delete files with the name index.html that are in a specific directory and its subdirectories.

I believe that the following command will do that. However, I need to write it as a script file which I could then have executed via cron.

find /path/to/directory -name "index.html" -print0 | xargs -0 rm -rf

The /path/to/directory would be relative to the server root.

My two questions are, do I need a trailing / at the end of the path and how do I write the bash script file for example in a file called

I am assuming that I would need to set the file as an executable using

chmod a+x

As regards setting the cron command, that is not a problem for me.

share|improve this question
Which one do you want to delete by the way - index.html or .index.html? – peterph Jan 17 '14 at 9:45
that was a typo, should have corrected it. It is index.html and not .index.html. Corrected now. – Ali Samii Jan 17 '14 at 12:04

3 Answers 3

up vote 9 down vote accepted

I wouldn't even write the script -- you should be able to put the find command in directly. You can also call the delete command directly from find using the -delete action flag.

Step 1: edit crontab

crontab -e

Step 2: add in the following line (this will run it daily at 4:30am, change to your liking):

30 4 * * * find /path/to/directory -name "index.html" -delete

Step 3: Save and exit.

share|improve this answer
+1 but you do not need the quotes in there – Zelda Jan 17 '14 at 9:49
@paulburkeland @zelda So the syntax would be * * * * * find /path/to/directory -name index.html -delete? Would that be recursive? – Ali Samii Jan 17 '14 at 12:06
@alisamii yes, find is always recursive. – terdon Jan 17 '14 at 12:17
@terdon what is the difference between * * * * * find /path/to/directory -name index.html -delete and * * * * * find /path/to/directory -name index.html -exec rm -f {} + – Ali Samii Jan 17 '14 at 12:20
@Zelda I wonder if that's true. Since the OP is using -exec command + (note the +), that will cause find to combine the file names into as few commands as possible while -delete will call unlinkat() once for each file. I'm not at all sure which will be faster. In any case, aslisamii, these are details and you really don't care. Use whichever you prefer, just remember that -delete is not available on all find implementations. – terdon Jan 17 '14 at 12:48

As you mentioned relative paths imagine that for example you want to clean up tree located in /path/to/directory. Then the following snippets will all achieve the same. The first one is using absolute path argument for find, the remaining three a relative one (relative to the one changed to with the preceding cd)

find /path/to/directory -name "index.html" -print0 | xargs -0 rm -rf  
cd /path || exit
find to/directory -name "index.html" -print0 | xargs -0 rm -rf  
cd /path/to || exit
find directory -name "index.html" -print0 | xargs -0 rm -rf  
cd /path/to/directory || exit
find . -name "index.html" -print0 | xargs -0 rm -rf  

Et voilà?

The trailing / is not important for the find command unless /path/to/directory is a symbolic link or you want to get an error if /path/to/directory is not a directory.

You will need to set the file as executable.

You just answered your own question :D

share|improve this answer
So, all that is needed to write a script file is to start the file with #!/bin/bash on its own line? That was simple. – Ali Samii Jan 17 '14 at 9:14
@alisamii it can be bash or sh or ksh it depends on wich you like the most but for a simple script like this bash is ok – Kiwy Jan 17 '14 at 9:16
as regards the first cd command, please explain one thing. If I am looking to delete index.html in the following path /home/user/site/client/conf/repo/section and in all subdirectories such as /home/user/site/client/conf/repo/section/dir1 and /home/user/site/client/conf/repo/section/dir2 what would I put in as the path for the cd command and what would I put as the path for the find command. I want to ensure that it only deletes the files called index.html and only those that are at the root of /home/user/site/client/conf/repo/section and its subdirectories. – Ali Samii Jan 17 '14 at 9:22
Also, would it be better to set the path relative to the user's home/root, i.e. ~/site/client/conf/repo/section or relative to the server's root, that is /home/user/site/client/conf/repo/section? – Ali Samii Jan 17 '14 at 9:25
~/site/client/conf/repo/section is an absolute path – Kiwy Jan 17 '14 at 9:33

A simple, fast and portable way is to use:

find /path/to/directory -name index.html -exec rm -f {} +

-print0, -delete and xargs -0 are GNU extensions.

share|improve this answer
as you have rm -f I assume that is not a recursive delete. I assume I would need to modify it to rm -rf to make it recursive, right? – Ali Samii Jan 17 '14 at 12:12
The find command is recursive. Unless you have directories named index.html, -r would be useless. – jlliagre Jan 17 '14 at 12:14
So, if I want to add it to the crontab such that it runs every minute, using crontab -e, the line I would add would be * * * * * find /path/to/directory -name index.html -exec rm -f {} + – Ali Samii Jan 17 '14 at 12:18
That would be enough indeed. – jlliagre Jan 17 '14 at 12:21

Your Answer


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.