Find the answer to your Linux question:
Results 1 to 6 of 6
Hi all, I'm here is my problem : How delete all files from a directory,which are created before a specific date? For example:[user provides the no.of days = 5] Delete ...
  1. #1
    Linux Guru Lakshmipathi's Avatar
    Join Date
    Sep 2006
    Location
    3rd rock from sun - Often seen near moon
    Posts
    1,568

    Post Shell script:How to delete based on created date ?

    Hi all,
    I'm here is my problem :
    How delete all files from a directory,which are created before a specific date?
    For example:[user provides the no.of days = 5]
    Delete all files from /home/oss directory except,
    those files created which are in the last 5 days.

    Any thoughts?
    - Lakshmipathi.G
    -------------------
    FOSS India Award winning ext3fs Undelete tool and tutorials www.giis.co.in
    First they criticize you,Then they laugh at you,Then they fight with you,Then you win. - M.K.Gandhi
    -------------------

  2. #2
    Linux Enthusiast
    Join Date
    Aug 2006
    Posts
    631
    Try the find command with the -mtime option:

    Code:
    find /home/oss -mtime +5 -exec rm -f {} \;
    Try this first to see if you get the correct files:

    Code:
    find /home/oss -mtime +5 -exec ls -l {} \;
    Regards

  3. #3
    Linux Guru Lakshmipathi's Avatar
    Join Date
    Sep 2006
    Location
    3rd rock from sun - Often seen near moon
    Posts
    1,568

    Exclamation

    Thanks for your help.
    Code:
    find /home/oss -mtime 5 -exec ls -l {} \;
    provides me files which are modifed 5 days ago.

    But,Let me explain my problem bit more clearly:

    I have 7 files created between Feb 7 to 13:

    Code:
    -rw-r--r--  1 oss oss 6770 Feb  13 11:25 access_log.someext
    -rw-r--r--  1 oss oss 6770 Feb  12 11:25 access_log.something
    -rw-r--r--  1 oss oss 6770 Feb  11 11:25  access_log.cf
    -rw-r--r--  1 oss oss 6770 Feb  10 11:25  access_log.ad
    -rw-r--r--  1 oss oss 6770 Feb  9 11:25  access_log.aw
    -rw-r--r--  1 oss oss 6770 Feb  8 11:25  access_log.zsd
    -rw-r--r--  1 oss oss 6770 Feb  7 11:25  access_log.xzs
    How to delete files which are created before 3 days.
    ie.delete all files which are created before feb-11.


    Delete following files :
    Code:
    -rw-r--r--  1 oss oss 6770 Feb  10 11:25  access_log.ad
    -rw-r--r--  1 oss oss 6770 Feb  9 11:25  access_log.aw
    -rw-r--r--  1 oss oss 6770 Feb  8 11:25  access_log.zsd
    -rw-r--r--  1 oss oss 6770 Feb  7 11:25  access_log.xzs
    Finally the directory has following files:
    These files are created between Feb 11 to 13.
    Code:
    -rw-r--r--  1 oss oss 6770 Feb  13 11:25 access_log.someext
    -rw-r--r--  1 oss oss 6770 Feb  12 11:25 access_log.something
    -rw-r--r--  1 oss oss 6770 Feb  11 11:25  access_log.cf

    Thanks.
    - Lakshmipathi.G
    -------------------
    FOSS India Award winning ext3fs Undelete tool and tutorials www.giis.co.in
    First they criticize you,Then they laugh at you,Then they fight with you,Then you win. - M.K.Gandhi
    -------------------

  4. #4
    Linux Guru Lakshmipathi's Avatar
    Join Date
    Sep 2006
    Location
    3rd rock from sun - Often seen near moon
    Posts
    1,568

    Smile

    Sorry i missed out '+'

    Code:
    find . -mtime +5 -exec ls -l {} \;
    It's working . Thanks franklin
    - Lakshmipathi.G
    -------------------
    FOSS India Award winning ext3fs Undelete tool and tutorials www.giis.co.in
    First they criticize you,Then they laugh at you,Then they fight with you,Then you win. - M.K.Gandhi
    -------------------

  5. #5
    Linux Enthusiast
    Join Date
    Aug 2006
    Posts
    631
    You forgot the "+" sign. From the manpage of find:

    -atime n
    File was last accessed n*24 hours ago. When find figures out
    how many 24-hour periods ago the file was last accessed, any
    fractional part is ignored, so to match -atime +1, a file has to
    have been accessed at least two days ago.

    -mtime n
    File's data was last modified n*24 hours ago. See the comments
    for -atime to understand how rounding affects the interpretation
    of file modification times.

    n : exact n*24 hours ago.
    -n : under n*24 hours.
    +n : over n*24 hours.

    Regards

  6. #6
    Linux Guru Lakshmipathi's Avatar
    Join Date
    Sep 2006
    Location
    3rd rock from sun - Often seen near moon
    Posts
    1,568

    Smile

    yes.I agree
    Thanks for your help.
    - Lakshmipathi.G
    -------------------
    FOSS India Award winning ext3fs Undelete tool and tutorials www.giis.co.in
    First they criticize you,Then they laugh at you,Then they fight with you,Then you win. - M.K.Gandhi
    -------------------

Posting Permissions

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