nixCraft Linux Forum

nixCraft

Linux / UNIX Tech Support Forum

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

This is a discussion on Shell script to delete all files with .html extension in a folder within the Shell scripting forums, part of the Development/Scripting category; Hi all..I am very much new to shell scripting and I am highly interested to learn this as I can ...


Go Back   nixCraft Linux Forum > Development/Scripting > Shell scripting

Linux answers from nixCraft.


Shell scripting You can discuss the shell scripting, request shell scripts and scripting techniques

Reply

 

LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 24-11-2009, 02:45 PM
vamsi's Avatar
Senior Member
User
 
Join Date: Nov 2009
Location: Bangalore / India
OS: Ubuntu , Debian Lenny , CentOS 5.x
Posts: 109
Thanks: 70
Thanked 7 Times in 5 Posts
Rep Power: 1
vamsi will become famous soon enough
Thumbs up 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 !!
Reply With Quote
  #2 (permalink)  
Old 24-11-2009, 05:39 PM
nixcraft's Avatar
Never say die
User
 
Join Date: Jan 2005
Location: BIOS
OS: RHEL
Scripting language: Bash and Python
Posts: 2,710
Thanks: 11
Thanked 245 Times in 184 Posts
Rep Power: 10
nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute
Default

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"
To delete simply add -delete
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
# Task: Clean html files
# add your find command here
find /dir1 -iname "*.html" -delete
find /dir2 -iname "*.html" -delete
Set executable permission and you are done!
__________________
Vivek Gite
Linux Evangelist
Be proud RHEL user, and let the world know about your enterprise choices! Join RedHat user group.
Always use CODE tags for posting system output and commands!
Do you run a Linux? Let's face it, you need help
Reply With Quote
The Following User Says Thank You to nixcraft For This Useful Post:
vamsi (24-11-2009)
  #3 (permalink)  
Old 24-11-2009, 05:41 PM
nixcraft's Avatar
Never say die
User
 
Join Date: Jan 2005
Location: BIOS
OS: RHEL
Scripting language: Bash and Python
Posts: 2,710
Thanks: 11
Thanked 245 Times in 184 Posts
Rep Power: 10
nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute
Default

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
__________________
Vivek Gite
Linux Evangelist
Be proud RHEL user, and let the world know about your enterprise choices! Join RedHat user group.
Always use CODE tags for posting system output and commands!
Do you run a Linux? Let's face it, you need help
Reply With Quote
The Following User Says Thank You to nixcraft For This Useful Post:
vamsi (24-11-2009)
  #4 (permalink)  
Old 24-11-2009, 07:24 PM
jaysunn's Avatar
Powered By Linux
User
 
Join Date: Apr 2009
Location: 41.332032,-73.089775
OS: RHEL - OSX
Scripting language: BASH - Learning Ruby
Posts: 604
Thanks: 61
Thanked 80 Times in 72 Posts
Rep Power: 10
jaysunn is a splendid one to behold jaysunn is a splendid one to behold jaysunn is a splendid one to behold jaysunn is a splendid one to behold jaysunn is a splendid one to behold jaysunn is a splendid one to behold jaysunn is a splendid one to behold jaysunn is a splendid one to behold
Default

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
__________________
Have a look at what I have been working on
http://www.shellasaurus.com
Reply With Quote
The Following User Says Thank You to jaysunn For This Useful Post:
vamsi (24-11-2009)
  #5 (permalink)  
Old 24-11-2009, 08:27 PM
nixcraft's Avatar
Never say die
User
 
Join Date: Jan 2005
Location: BIOS
OS: RHEL
Scripting language: Bash and Python
Posts: 2,710
Thanks: 11
Thanked 245 Times in 184 Posts
Rep Power: 10
nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute
Default

Good point, also take a look at xargs (for speed) and print0 (files with spaces).
__________________
Vivek Gite
Linux Evangelist
Be proud RHEL user, and let the world know about your enterprise choices! Join RedHat user group.
Always use CODE tags for posting system output and commands!
Do you run a Linux? Let's face it, you need help
Reply With Quote
The Following User Says Thank You to nixcraft For This Useful Post:
vamsi (24-11-2009)
  #6 (permalink)  
Old 06-12-2009, 04:04 AM
Member
User
 
Join Date: May 2009
OS: Mandriva
Posts: 82
Thanks: 0
Thanked 16 Times in 16 Posts
Rep Power: 3
cfajohnson has a spectacular aura about cfajohnson has a spectacular aura about cfajohnson has a spectacular aura about
Default

Quote:
Originally Posted by nixcraft View Post
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"
To delete simply add -delete
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
Reply With Quote
  #7 (permalink)  
Old 06-12-2009, 07:50 PM
jaysunn's Avatar
Powered By Linux
User
 
Join Date: Apr 2009
Location: 41.332032,-73.089775
OS: RHEL - OSX
Scripting language: BASH - Learning Ruby
Posts: 604
Thanks: 61
Thanked 80 Times in 72 Posts
Rep Power: 10
jaysunn is a splendid one to behold jaysunn is a splendid one to behold jaysunn is a splendid one to behold jaysunn is a splendid one to behold jaysunn is a splendid one to behold jaysunn is a splendid one to behold jaysunn is a splendid one to behold jaysunn is a splendid one to behold
Default

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
__________________
Have a look at what I have been working on
http://www.shellasaurus.com
Reply With Quote
  #8 (permalink)  
Old 06-12-2009, 09:03 PM
monk's Avatar
Senior Member
User
 
Join Date: Jan 2005
Location: Tibet
OS: Debian GNU/Linux
Posts: 506
Thanks: 0
Thanked 8 Times in 6 Posts
Rep Power: 7
monk has a spectacular aura about monk has a spectacular aura about
Default

Quote:
Originally Posted by cfajohnson View Post

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.
Quote:
Originally Posted by cfajohnson View Post
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?
__________________
May the force with you!
Reply With Quote
  #9 (permalink)  
Old 06-12-2009, 09:23 PM
nixcraft's Avatar
Never say die
User
 
Join Date: Jan 2005
Location: BIOS
OS: RHEL
Scripting language: Bash and Python
Posts: 2,710
Thanks: 11
Thanked 245 Times in 184 Posts
Rep Power: 10
nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute
Default

Quote:
Originally Posted by jaysunn View Post
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:
Quote:
-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
__________________
Vivek Gite
Linux Evangelist
Be proud RHEL user, and let the world know about your enterprise choices! Join RedHat user group.
Always use CODE tags for posting system output and commands!
Do you run a Linux? Let's face it, you need help
Reply With Quote
  #10 (permalink)  
Old 06-12-2009, 09:34 PM
jaysunn's Avatar
Powered By Linux
User
 
Join Date: Apr 2009
Location: 41.332032,-73.089775
OS: RHEL - OSX
Scripting language: BASH - Learning Ruby
Posts: 604
Thanks: 61
Thanked 80 Times in 72 Posts
Rep Power: 10
jaysunn is a splendid one to behold jaysunn is a splendid one to behold jaysunn is a splendid one to behold jaysunn is a splendid one to behold jaysunn is a splendid one to behold jaysunn is a splendid one to behold jaysunn is a splendid one to behold jaysunn is a splendid one to behold
Default

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

Quote:
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
__________________
Have a look at what I have been working on
http://www.shellasaurus.com
Reply With Quote
Reply

Tags
/etc/cron.daily , centos find file , find , find -delete , find and delete html files , find cron job , find file linux


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads

Thread Thread Starter Forum Replies Last Post
Shell script to Give free space on hard disk & propose a list of files to be delete manike Shell scripting 1 30-05-2009 09:31 PM
shell script to zip old files and then delete shankar43 Shell scripting 2 13-03-2009 09:10 AM
A script for Removing all the files inside a folder and its sub folder vivekv Shell scripting 1 25-10-2007 01:44 PM
i need a script to delete one folder with files on my ftp silver_ch Shell scripting 1 27-03-2007 10:15 AM
HTML Program cannot open in cgi-bin folder sonaikar Linux software 5 04-02-2005 04:45 PM


All times are GMT +5.5. The time now is 06:12 AM.


Powered by vBulletin® Version 3.8.5 - Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2
©2005-2010 nixCraft. All rights reserved

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38