3

I have a directory containing lots of files. The output of ls command is

-rw-r--r-- 1 nobody nogroup    427494 May 26 13:59 14_20150526065238928590_102.txt
-rw-r--r-- 1 nobody nogroup   2113592 May 26 14:00 14_20150526065238928590_105.txt
-rw-r--r-- 1 nobody nogroup    429947 May 26 14:00 14_20150526065238928590_110.txt
-rw-r--r-- 1 nobody nogroup    453831 May 27 14:00 14_20150526065238928590_112.txt
-rw-r--r-- 1 nobody nogroup    551023 May 27 14:01 14_20150526065238928590_118.txt
-rw-r--r-- 1 nobody nogroup     60083 May 27 14:01 14_20150526065238928590_119.txt
-rw-r--r-- 1 nobody nogroup    632324 May 28 03:38 14_20150526065238928590_160.txt
-rw-r--r-- 1 nobody nogroup    735624 May 28 03:38 14_20150526065238928590_161.txt
-rw-r--r-- 1 nobody nogroup   2707507 May 28 03:40 14_20150526065238928590_162.txt

From the above list I want to retrieve file names those are created before input date:

For eg: My input date is 20150528, I want the result: (created before 28th of May 2015)

14_20150526065238928590_102.txt
14_20150526065238928590_105.txt
14_20150526065238928590_110.txt
14_20150526065238928590_112.txt
14_20150526065238928590_118.txt
14_20150526065238928590_119.txt

How can I achieve this?

5

To find all files in the current directory and its subdirectories whose last modification time is earlier than 2015-05-28:

find . ! -newermt 20150527

If you only want files from the current directory and not its subdirectories, use:

find . -maxdepth 1 ! -newermt 20150527

How it works

  • find

    This is one of unix's most useful commands when searching for files.

  • .

    This tells find to start looking in the current directory. You can replace this with any directory that you like.

  • !

    This is logical-not: it inverts the test which follows.

  • -newermt 20150527

    This is test for files whose modification time is newer than 2015-05-27. Because of the preceding !, this test is inverted and it looks for files not newer than 2015-05-27.

    Note that "not newer than 2015-05-27" means the same as "created before 28th of May 2015".

3
  • Thank you very much. I got the result. The output contain ./ append before the filename. like ./14_20150526065238928590_105.txt. Can we remove the path from the result?
    – fortune
    May 28 '15 at 7:15
  • 1
    @fortune To remove the leading ./, use: find . ! -newermt 20150527 | sed 's|^./||'
    – John1024
    May 28 '15 at 7:40
  • That is it. Great
    – fortune
    May 28 '15 at 7:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.