Page 1 of 2 1 2 LastLast
Results 1 to 10 of 16

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

  1. #1
    Senior Member vamsi's Avatar
    Join Date
    Nov 2009
    Location
    Bangalore / India
    Posts
    263
    Thanks
    138
    Thanked 14 Times in 12 Posts
    Rep Power
    6

    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 !!

  2. #2
    Never say die nixcraft's Avatar
    Join Date
    Jan 2005
    Location
    BIOS
    Posts
    4,512
    Thanks
    17
    Thanked 799 Times in 511 Posts
    Rep Power
    10

    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!
    All [Solved] threads are closed by mods / admin to avoid spam issues. See Howto mark a thread as [Solved]


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

    vamsi (24th November 2009)

  4. #3
    Never say die nixcraft's Avatar
    Join Date
    Jan 2005
    Location
    BIOS
    Posts
    4,512
    Thanks
    17
    Thanked 799 Times in 511 Posts
    Rep Power
    10

    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
    All [Solved] threads are closed by mods / admin to avoid spam issues. See Howto mark a thread as [Solved]


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

    vamsi (24th November 2009)

  6. #4
    Contributors jaysunn's Avatar
    Join Date
    Apr 2009
    Location
    NYC - USA
    Posts
    1,023
    Thanks
    164
    Thanked 153 Times in 132 Posts
    Rep Power
    24

    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

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

    vamsi (24th November 2009)

  8. #5
    Never say die nixcraft's Avatar
    Join Date
    Jan 2005
    Location
    BIOS
    Posts
    4,512
    Thanks
    17
    Thanked 799 Times in 511 Posts
    Rep Power
    10

    Default

    Good point, also take a look at xargs (for speed) and print0 (files with spaces).
    All [Solved] threads are closed by mods / admin to avoid spam issues. See Howto mark a thread as [Solved]


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

    vamsi (24th November 2009)

  10. #6
    Senior Member cfajohnson's Avatar
    Join Date
    May 2009
    Posts
    190
    Thanks
    0
    Thanked 44 Times in 36 Posts
    Rep Power
    10

    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

  11. #7
    Contributors jaysunn's Avatar
    Join Date
    Apr 2009
    Location
    NYC - USA
    Posts
    1,023
    Thanks
    164
    Thanked 153 Times in 132 Posts
    Rep Power
    24

    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

  12. #8
    Senior Member monk's Avatar
    Join Date
    Jan 2005
    Location
    Tibet
    Posts
    643
    Thanks
    5
    Thanked 43 Times in 38 Posts
    Rep Power
    15

    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!

  13. #9
    Never say die nixcraft's Avatar
    Join Date
    Jan 2005
    Location
    BIOS
    Posts
    4,512
    Thanks
    17
    Thanked 799 Times in 511 Posts
    Rep Power
    10

    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:
    -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
    All [Solved] threads are closed by mods / admin to avoid spam issues. See Howto mark a thread as [Solved]


  14. #10
    Contributors jaysunn's Avatar
    Join Date
    Apr 2009
    Location
    NYC - USA
    Posts
    1,023
    Thanks
    164
    Thanked 153 Times in 132 Posts
    Rep Power
    24

    Default

    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

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Replies: 1
    Last Post: 30th May 2009, 09:31 PM
  2. shell script to zip old files and then delete
    By shankar43 in forum Shell scripting
    Replies: 2
    Last Post: 13th March 2009, 09:10 AM
  3. Replies: 1
    Last Post: 25th October 2007, 01:44 PM
  4. i need a script to delete one folder with files on my ftp
    By silver_ch in forum Shell scripting
    Replies: 1
    Last Post: 27th March 2007, 10:15 AM
  5. HTML Program cannot open in cgi-bin folder
    By sonaikar in forum Linux software
    Replies: 5
    Last Post: 4th February 2005, 04:45 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •