# Thread: Shell script to delete all files with .html extension in a folder

1. ## Shell script to delete all files with .html extension in a folder

Hi all..I am very much new to shell scripting and I am highly interested to learn this as I can use this to automate many things
can any one from here please create a shell script with the following requirements ?

Desired
a script that will automatically delete all files with .html extension in a folder/directory ( in apache directories also) in prescribed time intervals ( craon job ?)
platform - CentOS 5.0

if possible please put some comments in the script , so that I can learn also

Thanks !!

2. No need to write a shell script, simply use find command. In this example, list all *.html files stored in /path/to/dir:
Code:
find /path/to/dir -iname "*.html"
Code:
find /path/to/dir -iname "*.html" -delete
Set a cron job and clean out required files. If you want to delete them daily then create a file called /etc/cron.daily/cleanhtmlfiles:

Code:
#!/bin/bash
find /dir1 -iname "*.html" -delete
find /dir2 -iname "*.html" -delete
Set executable permission and you are done!

3. ## The Following User Says Thank You to nixcraft For This Useful Post:

vamsi (24th November 2009)

4. See our cron job FAQ:

1. How do I add jobs to cron under Linux or UNIX oses?
2. I'm sure you know where to look for scripting tutorial

5. ## The Following User Says Thank You to nixcraft For This Useful Post:

vamsi (24th November 2009)

6. If your version of find does not support the delete option you can use the following when deleting files.

Code:
find /path/to/dir -iname "*.html" -exec rm -f {} \; -print

HTH,
jaysunn

7. ## The Following User Says Thank You to jaysunn For This Useful Post:

vamsi (24th November 2009)

8. Good point, also take a look at xargs (for speed) and print0 (files with spaces).

9. ## The Following User Says Thank You to nixcraft For This Useful Post:

vamsi (24th November 2009)

10. Originally Posted by nixcraft
No need to write a shell script, simply use find command. In this example, list all *.html files stored in /path/to/dir:
Code:
find /path/to/dir -iname "*.html"
Code:
find /path/to/dir -iname "*.html" -delete

Neither -iname nor -delete is in the POSIX spec for find. Most implementations will not include them.

Use -name and -exec rm.

To remove all the .html files in the current folder all that's needed is:

Code:
rm *.html

11. Hello

To avoid pressing y 1000 times after running the rm command without the -f option. Use this if you are in the directory that contain the html files.

Code:
rm -f *.html

Jaysunn

12. Originally Posted by cfajohnson

Neither -iname nor -delete is in the POSIX spec for find. Most implementations will not include them.

To remove all the .html files in the current folder all that's needed is:
OP clear stated CentOS 5 as operating system and the find command does support those options on CentOS 5. But, I agree POSIX compatible script will run on other UNIX variant.
Originally Posted by cfajohnson
Code:
rm *.html
Only if you want to clear all *.html files from the current directory. If you need to process 10-20 subdirs, find is handy tool, IMO.

On a related not what do you think of using find ... | xargs rm syntax instead of find .... -exec rm syntax?

13. Originally Posted by jaysunn
Hello

To avoid pressing y 1000 times after running the rm command without the -f option. Use this if you are in the directory that contain the html files.

Code:
rm -f *.html
Jaysunn
from the rm man page:
-f, --force ignore nonexistent files, never prompt
I think y prompt is becuase of RHEL / CentOS default rm aliase (rm is rm -i). So to get around:
Code:
\rm *.html
/bin/rm *.html

14. Yes due to the O/P's O/S. I explained that the rm without the -f option will be unbearable to run.

platform - CentOS 5.0
Here is the test I just ran to demonstrate the horror. LOL

Code:
[root@radio5 testing]# rm *.html
rm: remove regular empty file a.html'? y
rm: remove regular empty file b.html'? y
rm: remove regular empty file c.html'? y
rm: remove regular empty file d.html'? y
rm: remove regular empty file e.html'? y
rm: remove regular empty file google45648f09a80004b1.html'? y
rm: remove regular empty file googlec628fdecd594064c.html'? y
rm: remove regular file index.html'? y
rm: remove regular file jason.html'? y
[root@radio5 testing]#`

Jaysunn