0

I am trying to delete all files that are older than 7 days. The command is working but not correctly.

 find '/files/tem_data/' -mtime +7 -exec rm -rf {} \+

It does delete files but it's not accurate.

ls -Artl | head -n 2

The find does delete files, but when I run the ls command does contain files that should be deleted. For example today is November 7th. The find should delete all files before November 1st. It does not. The command leaves files that are in October 30 and 31. How can I delete files that are older than 7 days.

If I run find command like 3 minutes later. It deletes files with the date of October 30 and a time of 3 minutes after it first ran.

user3525290
  • 1,557
  • 2
  • 20
  • 47
  • 1
    [Delete files older than X days](https://unix.stackexchange.com/q/194863/56041), [Removing files older than 7 days](https://askubuntu.com/q/589210), [delete file/folder older than x days](https://stackoverflow.com/q/31389483/608639), [How to delete all files older than 3 days when “Argument list too long”?](https://stackoverflow.com/q/14731133/608639), [How to delete files older than X hours](https://stackoverflow.com/q/249578/608639), etc. – jww Nov 07 '17 at 18:24
  • These links just suggest doing what OP is already doing, and they do not address the behavior OP is seeing – that other guy Nov 07 '17 at 18:39
  • Stack Overflow is a site for programming and development questions. This question appears to be off-topic because it is not about programming or development. See [What topics can I ask about here](http://stackoverflow.com/help/on-topic) in the Help Center. Perhaps [Super User](http://superuser.com/) or [Unix & Linux Stack Exchange](http://unix.stackexchange.com/) would be a better place to ask. – jww Nov 07 '17 at 18:45
  • @thatotherguy - yeah, looking at the answers, it looks like a question asking someone to read the man page of a command for them. – jww Nov 07 '17 at 18:48

2 Answers2

1

From man find:

-atime n

  File  was  last  accessed  n*24  hours  ago.  When find figures out how many
  24-hour periods ago the file was  last  accessed,  any  fractional  part  is
  ignored,  so  to  match -atime +1, a file has to have been accessed at least
  two days ago.

This means that your command actually deletes files that were accessed 8 or more days ago.

Since the time now is

$ date
Tue Nov  7 10:29:29 PST 2017

find will require files need to be older than:

$ date -d 'now - 8 days'
Mon Oct 30 11:29:05 PDT 2017

In other words, leaving some files from Oct 30 is expected and documented behavior.

To account for find rounding down, simply use -mtime +6 instead.

that other guy
  • 116,971
  • 11
  • 170
  • 194
  • Thanks. I am new to linux administration. The other comments where from my understanding pretty much what I was currently doing. – user3525290 Nov 07 '17 at 19:15
0

This is not the exact answer but you can try this as an sample. find /path/to/ -type f -mtime +7 -name '*.gz' -execdir rm -- '{}' \;

Or for an alternative and also faster command is using exec's + terminator instead of \;:

find /path/to/ -type f -mtime +7 -name '*.gz' -execdir rm -- '{}' + or

find /path/to/ -type f -mtime +7 -name '*.gz' -delete
  • find: the unix command for finding files/directories/links and etc. /path/to/: the directory to start your search in.
    -type f: only find files.
    -name '*.gz': list files that ends with .gz.
    -mtime +7: only consider the ones with modification time older than 7 days. -execdir ... \;: for each such result found, do the following command in rm -- '{}': remove the file; the {} part is where the find result gets substituted into from the previous part. -- means end of command parameters avoid prompting error for those files starting with hyphen

.

  • Thanks but that is pretty much what my code is doing. Like @that-other-guy stated it's expected behavior. I am new to linux admin type of stuff so I wasn't aware. – user3525290 Nov 07 '17 at 19:17