Operating System - Linux
1830161 Members
3021 Online
109999 Solutions
New Discussion

delete based in timestamp

 
Tonatiuh
Super Advisor

delete based in timestamp

Red Hat Enterprise Linux

How can I delete files based in its timestamp.

Example:

Delete all files older than October 23, 16:35 Hrs.
5 REPLIES 5
Ivan Ferreira
Honored Contributor

Re: delete based in timestamp

Create a reference file with the touch -t command and set the time that you want. Then use the find command:

touch -t YYYYMMDDhhmm.ss /tmp/referencefile
find /path -type f ! -newer /tmp/referencefile -exec rm {} \;
Por que hacerlo dificil si es posible hacerlo facil? - Why do it the hard way, when you can do it the easy way?
Peter Godron
Honored Contributor

Re: delete based in timestamp

Hi,
please be aware that find will also search through sub-directories !
Andrea Rossi
Frequent Advisor

Re: delete based in timestamp

your delete operation is based on creation or modification date?
Tonatiuh
Super Advisor

Re: delete based in timestamp

Hi Andrea,

Is any difference between creation and modification timestamp?

How can I see this two dates of same file?
James R. Ferguson
Acclaimed Contributor

Re: delete based in timestamp

Hi:

You asked, "Is any difference between creation and modification timestamp?"

There is *NO* creation timestamp for a file in Unix.

You have three timestamps associated with a file. The 'mtime' is the last modification time. When a file is first created, its 'mtime' would represent a creation time, but any subsequent write to the file updates the 'mtime'.

The second timestamp is the last-access time ('atime') which is as the name suggests, the last time a file was read, written or executed.

The last timestamp is the 'ctime' or change-time. Altering a file's permissions, ownership or changing its name updates this timestamp.

All three timestamps are in Epoch seconds --- the number of seconds elapsed since January 1, 1970.

Ivan has already shown hou you can delete files older than a specific date. I would suggest, however, that you optimize:

# find /path -type f ! -newer /tmp/referencefile -exec rm {} \;

...to:

# find /path -xdev -type f ! -newer /tmp/referencefile -exec rm {} +

...if your Unix version supports it, or:

# find /path -xdev -type f ! -newer /tmp/referencefile | xargs rm

The last two cases assemble many filenames into a list that is then passed to the 'rm' process. The first case means that a new 'rm' process will be spawned for *every* file found. Needless to say, creating many processes is far more resource intensive then creating merely a few. With large numbers of files to remove you could easily measure the time in minutes versus seconds!

Regards!

...JRF...