maihabunash
maihabunash

Reputation: 1702

find command + remove older directories according to directory time stamp

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 {} \;

Upvotes: 0

Views: 458

Answers (1)

toto21
toto21

Reputation: 612

-t sort by modification time

try

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

Update : delete options depth and mindepth

Upvotes: 1

Related Questions