2

The following command will delete directories and sub directories that are older then 100 days:

find /var/tmp -type d -ctime +100 -exec rm -rf {} \;

But actually I want to perform the test to verify if the command will remove all directories.

So I set -ctime +0 (in order to perform remove directories that old then 0 days)

find /var/tmp -type d -ctime +0 -exec rm -rf {} \;

But the find command did not remove the directories.

How can I change the -ctime in order to perform the test?

  • 1
    What do you mean by the age of a directory? -ctime is change time, that's for the time the status (data or metadata (permission, ownerships or other attributes)) of the file was last changed. Use -mtime for modification time (data changed which for a directory means entries added, removed or renamed (which I can't imagine it being what you want either). -ctime +0 is for at least 1 day old. Use -prune after your rm. – Stéphane Chazelas Dec 2 '14 at 9:40
0

This command remove the directories created in the last day.

find /var/tmp -type d -ctime -1 -exec rm -rf {} \;
2

The man page of find says this about -ctime:

-ctime n
       File's status was last changed n*24 hours ago.  See the comments
       for -atime to understand how rounding affects the interpretation
       of file status change times.

and this about -atime:

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

So you should expect -ctime 1 to remove files that were changed 1 day ago or more based on the rounding, and you should set it to -ctime +-1 to get the effect you want.

To prevent /var/tmp itself from being deleted itself also specify -mindepth 1 and while experimenting always first print out the directories find is going to work on:

 find /var/tmp -mindepth 1 -type d -ctime +-1  -print

before doing the destructive:

  find /var/tmp -mindepth 1 -type d -ctime +-1 -exec rm -rf {} +

( I would use + instead of \; no need to invoke rm multiple times. (Alternatively you could look at using -delete instead of using -exec rm.... but in that case find would need to remove the files under those directories as well, while leaving those directly under /var/tmp)

  • another quastion - find /var/tmp -type d -ctime +100 -exec rm -rf {} \; command deleted also the tmp dir under /var - how to avoid this , and why it remove the tmp under /var ? – maihabunash Dec 2 '14 at 9:29
  • @maihabunash you might want to include -mindepth 1 or if there are no "hidden" directories in /var/tmp use find /var/tmp/* .... The directory gets changed every time something gets added to it e.g. – Anthon Dec 2 '14 at 9:59

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.