Shell – Get all filenames in a directory created before input date

filesshelltimestamps

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?

Best Answer

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".

Related Question