1

why the following command removed the tmp directory under /var and how to avoid this?

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


pwd
/var/tmp
[root@linux /var/tmp]# find /var/tmp -type d -ctime -1 -exec rm -rf {} \;
find: /var/tmp: No such file or directory
  • 1
    What exactly are you trying to achieve? Removing directories (together with their content) that have been last altered today in /var/tmp (especially) makes little sense. – Stéphane Chazelas Dec 2 '14 at 10:32
  • for example - I want to remove dirs/sub dirs that are old then 180 days so I run this find /var/tmp -depth -mindepth 1 -type d -ctime +180 -exec rm -rf {} \; , but the first dir under /var/tmp is not old then 180 but the sub dir is old then 180 day – maihabunash Dec 2 '14 at 10:46
  • What's a 180 day old directory? One that was created 180 days ago? one where no file or directory in it or any of its subdirectories have been modified, linked, unlinked or renamed within the last 180 days? Something else? Checking -ctime will not do any of that. – Stéphane Chazelas Dec 2 '14 at 10:52
  • YES sub dir that created 180 day ago ( the sub dir include another sub dir and sub can include files and links ) – maihabunash Dec 2 '14 at 11:07
  • Even if that directory is used intensively on a daily/minutely basis? Then that generally can't be done on Linux. Linux stores a birth time on some file systems, but no easy way to retrieve it yet. Could do on FreeBSD or OS/X with -Btime. – Stéphane Chazelas Dec 2 '14 at 11:11
3

The first directory find /var/tmp finds is /var/tmp. If you want to skip that one (and use Gnu find) then you can change the command to:

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

or

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

Without -depth and -prune error messages may occur because rm -rf deletes subdirectories – which may have already been seen by find. -depth causes rm -r to be called for the subdirectories first. But as it should be enough to run rm -rf once for each subtree you can alternatively stop processing the directory contents with -prune.

| improve this answer | |
  • WHY I GET: find /var/tmp -mindepth 1 -type d -ctime -1 -exec rm -rf {} \; find: /var/tmp/3: No such file or directory find: /var/tmp/2: No such file or directory find: /var/tmp/1: No such file or directory – maihabunash Dec 2 '14 at 10:17
  • under /var/tmp I have dir as 1 and 2 and 3 – maihabunash Dec 2 '14 at 10:19
  • @maihabunash You should never use -exec rm -rf for testing. Delete that. Then you see in which order the matches occur. You should use -depth in order to avoid such error messages. – Hauke Laging Dec 2 '14 at 10:21
  • is it good idea to put 2>/dev/null after the command in order to ignore errors? – maihabunash Dec 2 '14 at 10:21
  • OK the right solution is find /var/tmp -depth -mindepth 1 -type d -ctime -1 -exec rm -rf {} \; – maihabunash Dec 2 '14 at 10:26
0

Use the logical not operator ! or -not to exclude the path /var/tmp from the results. Note: -not is not POSIX compliant.

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

Or

find /var/tmp -type d -ctime -1 -not -path /var/tmp -exec rm -rf {} \;
| improve this answer | |

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.