How to delete everything except 2 files in directories?

Discussion in 'Site & Server Administration' started by sample4u, Jun 27, 2012.

  1. #1
    Hi

    I have 1 directory in my server i will delete everything except 2 files (index.html and .htaccess) with cronjob how can do it?
     
    sample4u, Jun 27, 2012 IP
  2. sahabcse

    sahabcse Well-Known Member

    Messages:
    272
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    140
    #2
    rm -R *.* !(index.html*) !(.htaccess*)
     
    sahabcse, Jun 27, 2012 IP
  3. MrOSX

    MrOSX Active Member

    Messages:
    29
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    58
    #3
    From SSH:
    rm -R *.* !(index.html*) !(.htaccess*)
    Code (markup):
    From FTP:
    Just select and delete the files
    Code (markup):
     
    MrOSX, Jun 27, 2012 IP
  4. Harijith

    Harijith Peon

    Messages:
    20
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Good question :) I was also wondering how it is done ;)
     
    Harijith, Jul 2, 2012 IP
  5. aqualabs

    aqualabs Peon

    Messages:
    24
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    [root@test~]# mkdir z ; cd z ; touch 1 2 3 4 5 6
    [root@test z]# ls
    1  2  3  4  5  6
    [root@test~]# find z/ -type f  \( ! -iname '2' -and ! -iname '3' \) -print
    z/1
    z/4
    z/5
    z/6
    [root@test~]#
    Code (markup):
    You could use find with -delete instead of -print to remove the files.
     
    aqualabs, Jul 3, 2012 IP
  6. Hostwinds_Dan

    Hostwinds_Dan Active Member

    Messages:
    149
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    53
    #6
    This is actually what I was about to suggest; it's a much more efficient way of doing things.
     
    Hostwinds_Dan, Jul 3, 2012 IP
  7. atxsurf

    atxsurf Peon

    Messages:
    2,394
    Likes Received:
    21
    Best Answers:
    1
    Trophy Points:
    0
    #7
    i have a different angle on it: how about not keeping your auto generated files (logs, cache, tmp) in the same directory as your static files?
     
    atxsurf, Jul 3, 2012 IP
  8. samirj09

    samirj09 Well-Known Member

    Messages:
    335
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    125
    #8
    The below should work
    find <directory here> ! -name index.html ! -name .htaccess -exec rm -frv {} \;
     
    samirj09, Jul 7, 2012 IP