Monday, December 7, 2015

How to change time stamps for a file | How to find a file newer than or older than a reference file | how to change timestamp to match the timestamp of reference file

We come across situations where we need to find the list of files which are older or newer than a particular time. Well in that cases find command comes to our rescue. I will cover find command and its uses which demonstrate how powerful find can be.

Just to give idea on powerfulness of find, when find command executed with particular options and with few instances, can bring even the most powerful UNIX systems down on their knees.

Lets focus on find. More can be found in man pages of find, depending on the version of UNIX you can do man find or man 1 find or man 1m find or whatever.

Similarly do man touch.


I have a file named as test as below

$ ls -la test
-rw-r--r--    1 sujit    Administ       46 May  2 18:36 test

I will create a null file test1 using touch again

$touch test1

$ ls -la test1
-rw-r--r--    1 sujit    Administ       0 Jul 04 19:02 test

The file test1 0 bytes is created with the current timestamp. I will give the file test1 the same timestamp as file test1 above

$ touch -r test test1

This makes the file test1 get the time stamp of file test as can be seen. Here -r option says that take file test as reference  and put the time stamp of test to the file test1.

$ ls -la test test1

-rw-r--r--    1 sujit    Administ       46 May  2 18:36 test
-rw-r--r--    1 sujit    Administ       0 May  2 18:36 test1

Now just for example I can use the file test1 as a reference to find the list of files newer than or older than the file test1

$ find . -type f -newer test1 - exec ls -ltr {} \;

This will list the file which are newer than the file test1.


Now what if you need to find the files older than the files test1 : try below

$ find . -type f  ! - newer test1 -exec ls -ltr {} \;




No comments:

Post a Comment