1

I want to delete directories that older than 180 days

for example directories that older than 180 days:

drwxr-xr-x 2 root root 4096 Oct  1  2009 nis
drwxr-xr-x 3 root root 4096 Nov  4  2012 pkgs

I use this command:

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

After I run the find command , I see that the older directories are still exist

Please advice what wrong with my find command?

[root@vm1 /var/tmp]# ls -ltr
total 20
drwxr-xr-x 2 root root 4096 Oct  1  2009 nis
drwxr-xr-x 3 root root 4096 Nov  4  2012 pkgs
drwxr-x--- 2 root root 4096 Dec  3 08:24 1
drwxr-x--- 2 root root 4096 Dec  3 08:41 2
drwxr-x--- 2 root root 4096 Dec  3 08:41 3

[root@vm1 /var/tmp]# find /var/tmp  -depth -mindepth 1 -type d -ctime +180  -exec rm -rf {} \;

[root@vm1 /var/tmp]# ls -ltr
total 20
drwxr-xr-x 2 root root 4096 Oct  1  2009 nis
drwxr-xr-x 3 root root 4096 Nov  4  2012 pkgs
drwxr-x--- 2 root root 4096 Dec  3 08:24 1
drwxr-x--- 2 root root 4096 Dec  3 08:41 2
drwxr-x--- 2 root root 4096 Dec  3 08:41 3

I also try this ( but not remove the old dir ) the -mtime only change the date of the old dir to the current date

   find /var/tmp  -depth -mindepth 1 -type d -mtime +180  -exec rm -rf {} \;
1

ctime, mtime, and atime behave differently (see, e.g. here)

ctime checks for the inode modification time (this gets changed for instance if you copy an older file to a new directory or disk (which does not change mtime))

mtime checks for the file modification time (which is for instance not updated when copying a file to another location or when unpacking a file from an archive etc)

atime checks for access time

I would assume that ctime and mtime differ for your directory structure. (And ls -ltr shows the mtime, not the ctime.)

Your second attempt (with mtime) should not have changed the mtime of the dir (this behavior would be expected for atime) and, in fact, for me this command works for the task you describe. Not even in the case that you were missing write permissions for any of the subdirectories should this fail (since you have write permissions for the top level); rm would, of course fail if the disk was mounted readonly but in that case it should be impossible for this find command to change the time.

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.