Tuesday, June 3, 2014

Unix Tip : Unix command to find the files modified 'n' number of days ago


Find is the command for searching files in a unix folder. Probably, this is one such command which you might need quite often.

And, while working on a project, with VCS ( version control ), you often need to find the files that have been modified today, in last couple days and so on. So, this is one the way find command comes handy. It finds the files recursively as well.

Examples:

# replaces n with number of days here
> find . -mtime –n 
# A minus (-) before n indicates number of days ago 
# i.e. include those file which falls between today and nth day
> find . -mtime n    
# display list of files modified exactly ON the nth day, from today
> find . -mtime +n 
# A plus (+) before n indicates file modified even prior to nth day

In case you need to find the files that have been modified few minutes ago, or some 'm' minutes ago, you can use the option mmin instead of mtime.

------
Reference: http://linux.about.com/od/commands/l/blcmdl1_find.htm

2 comments:

  1. nice post kislay :) But, i had found something which is a little confusing :P

    -mtime n will actualy list out all the files that were last modified n*24 hours ago.
    The argument to -mtime is interpreted as the number of whole days in the age of the file. -mtime +n means strictly greater than, -mtime -n means strictly less than.

    if a file is 1 day, 23 hours, 59 minutes, and 59 seconds old, find -mtime +1 ignores all that and just treats it like it's 1 day, 0 hours, 0 minutes, and 0 seconds old..
    Like man find says, "any fractional part is ignored". If you divide "1 day, 23 hours, 59 minutes, and 59 seconds" through "24 hours", you may get 1.9999, but the .9999 part is then stripped and suddenly the file is only 1 day old.


    Reference: http://unix.stackexchange.com/questions/92346/why-does-find-mtime-1-only-return-files-older-than-2-days

    ReplyDelete
  2. Interesting. Thanks for the valuable info.
    So, in such cases, when you want to be precise, probably the test with -mmin option is more suitable.

    ReplyDelete

Prototype

Prototype is another creation pattern. Intent:  - Intent is to create objects by cloning existing instance  - Specify the kinds of obj...