18

I'm sure this is straight forward and answered somewhere, but I didn't manage to find what I was looking for. Basically, I'm trying to run a cron script to clear the contents of a given directory every 7 days. So far I have tried the following,

find /myDir -mtime 7 -exec rm -rf {} \;

This however also deletes the parent directory myDir, which I do not want. I also tried,

find /myDir -type f -type d -mtime 7 -delete

which appeared to do nothing. I also tried,

fnd /myDir -type d -delete

which deleted all but the parent directory just as I need. However, a warning message came up reading,

relative path potentially not safe

I'd appreciate if anyone can rectify my script so that it safely deletes all subdirectories in folder.

Many thanks. =)

UPDATE: I decided to go for the following,

find /myDir -mindepth 1 -mtime 7 -delete

Based upon what I learned from all who replied. Again, many thanks to you all.

infmz
  • 347
  • 2
  • 3
  • 10
  • As mentioned in one of the replies, your final command should probably be `find /myDir -mindepth 1 -mtime 7 -delete`. Note `+7` instead of `7`. The former deletes everything older than 7 days and the latter everything that's exactly 7 days old. – Jakub Kukul Nov 25 '16 at 13:11

3 Answers3

14

Try:

find /myDir -mindepth 1 -mtime 7 -exec rm -rf {} \;
linuts
  • 6,608
  • 4
  • 35
  • 37
  • 2
    This worked although upon execution I was given a message saying 'no such file or directory' for every file/dir deleted. Wondering if this is perfectly normal or not? Many thanks. The mindepth switch is very useful and I didn't even know it existed! – infmz May 05 '11 at 13:54
  • I changed this slightly by using -delete instead of -exec rm. I didn't receive any warnings/errors this way. Is this a good way about it you reckon? – infmz May 05 '11 at 14:14
  • @infmz: i'm not a big fan of -exec as it spawns a process for each file found, which is hideous if you're matching large numbers of files, so i'm not sure why you get the error. i'm assuming that the -delete was implemented for that reason. fwiw i would have piped the result to xargs -l2000 rm -rf. – linuts May 06 '11 at 17:36
  • 1
    message 'no such file or directory' beause find list directory before files in directory, then it delete first directory and after try to delete files in directory(just deleted) – stefcud Jul 29 '13 at 12:14
  • Note that `-delete` [doesn't work on non-empty directories](http://unix.stackexchange.com/questions/89925/how-to-delete-directories-based-on-find-output#comment288257_149574) (couldn't find it in the manpage but it's true on my machine). – mcmlxxxvi Dec 17 '15 at 17:11
  • @StefanoCudini is correct, `find` will first locate the dir, exec `rm` on it, then try to descend into it to look for more items. You can observe this behavior: `mkdir -p test/test && find . -name test` and note the order. The POSIX `-depth` option will make `find` traverse lower levels first (write it before your test expression); the downside is the search may take longer if the dir contains a lot of items. – mcmlxxxvi Dec 17 '15 at 17:23
7

What about

cd myDir/ ; find . -type d -delete

assuming that you run this from myDir parent directory.

If you can't guarantee myDir exists, then this is safer:

cd myDir/ && find . -type d -delete
linuts
  • 6,608
  • 4
  • 35
  • 37
MarcoS
  • 13,386
  • 7
  • 42
  • 63
  • @infmz: you're welcome. If you think this is a good answer for you question, then accept it as answer (that's the way stackoverflow works) – MarcoS May 05 '11 at 11:55
  • Thanks for the reminder, I know how it works. =) When I choose a best solution I will do. – infmz May 05 '11 at 13:16
  • What if myDir doesn't exist anymore? cd fails and find delete starting from the current directory.. not safe isn't it? – Marco Martinelli Jun 21 '13 at 17:26
  • @MarcoMartinelli if myDir doesn't exist anymore, than its content doesn't exists either ... so you don't need to delete anything, I guess – MarcoS Jun 23 '13 at 08:12
  • 1
    You are right, but for example, I'm using a cron job with something like `find /myDir -mindepth 1 -mtime 7 -delete` where /myDir is dinamically created by another script. Now, if /myDir doesn't exist find just give up. In this case find just assume you are in the right directory. By the way, your solution is valid, my comment was meant for newcomers in the unix world that may not think about this. – Marco Martinelli Jun 23 '13 at 20:58
6

find /myDir -mindepth 1 -mtime 7 -delete

should probably be

find /myDir -mindepth 1 -mtime +7 -delete

(or maybe mtime +6). The + means things 7 days old or older rather than exactly 7 days.