Results 1 to 8 of 8
  1. #1
    Join Date
    Dec 2006
    Posts
    288

    Deleting files older than 30 days

    On a Red Hat server, how can I delete files in a specific directory that are older than 30 days?

  2. #2
    Join Date
    Jan 2005
    Location
    Darwin, Australia
    Posts
    1,333
    find /path/to/files -mtime +30 -exec rm {} \;
    Graham Craig

    "IT'S NOT HOW GOOD YOU ARE, IT'S HOW BAD YOU WANT IT."

  3. #3
    Join Date
    Dec 2006
    Posts
    288
    Does that command also delete files in the /path/to/files subdirectories as well? If so, how do I just delete files older than 30 days in the /path/to/files directory only? I don't want to delete anything in /path/to/files/sub or /path/to/files/sub1, etc. Thanks!

  4. #4
    Join Date
    Jan 2008
    Posts
    42
    if your worried about what your deleting change the "exec rm" for "exec ls -l" as a test

  5. #5
    Join Date
    Dec 2006
    Posts
    288
    Thanks!

    For me that command would delete everything older than 30 days, including anything older than 30 days in any of the directories subdirectories. Any one have a command that will only delete files older than 30 days in a specific directory only, that would NOT include any subdirectories?

  6. #6
    Join Date
    Jun 2007
    Location
    UK
    Posts
    219
    You want to use the -maxdepth option. In your case "-maxdepth 1" is probably what you want (but check by using "-print" instead of the "-exec rm...").

    (Hint, the man page is your friend )
    I think the server saw what was required of it and just committed suicide instead.

  7. #7
    Join Date
    Oct 2004
    Location
    Kerala, India
    Posts
    4,750
    Quote Originally Posted by smrtalex View Post
    Any one have a command that will only delete files older than 30 days in a specific directory only, that would NOT include any subdirectories?
    Use the command below.
    Code:
    find /path/to/directory/  -type f -mtime +30
    This above list ONLY the files older than 30 days and NOT any sub directories. To remove them all at once, use the following command.
    Code:
    find /path/to/directory/  -type f -mtime +30 | xargs rm -rf
    David | www.cliffsupport.com
    Affordable Server Management Solutions sales AT cliffsupport DOT com
    CliffWebManager | Access WHM from iPhone and Android

  8. #8
    Join Date
    Feb 2005
    Location
    Australia
    Posts
    5,842
    Quote Originally Posted by david510 View Post
    Code:
    find /path/to/directory/  -type f -mtime +30
    This above list ONLY the files older than 30 days and NOT any sub directories.
    It won't list the subdirectories but it will descend into them and list the files contained unless you use -maxdepth 1 as Hound suggested. (-type f limits find to files only, but this does include files in subdirectories)
    Chris

    "Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them." - Laurence J. Peter

Posting Permissions

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