1

Can anyone tell me what is wrong in this script?

It is not working in cron, but works fine when I execute it normally.

Warning: this command is dangerous and may delete lots of files

#!/bin/bash

/bin/find  .  -maxdepth 1 -type d  -ctime +2 -exec /bin/rm -rf {} \;
6
  • Did you just delete every directory in your $HOME that is more than 2 days old ? Or have you been lucky and used sudo crontab ...
    – pLumo
    Feb 19, 2019 at 15:53
  • 6
    I think this question as it stands is very dangerous as other people might try if this works or why it doesn't work. DON'T TRY THIS AT HOME ...
    – pLumo
    Feb 19, 2019 at 15:57
  • I would suggest including what you are trying to do when you make a post like this. we can guess based on code but its better to say what you want it to do as well. Also you should way what it not working about it? does it do anything?
    – Jeff
    Feb 19, 2019 at 15:57
  • 1
    In Ubuntu, at least for me, find is in /usr/bin/find ...
    – pLumo
    Feb 19, 2019 at 16:00
  • 3
    I did something like this at work once on a production system. Took about 3 days to restore from tape backup. Lesson learned. Feb 19, 2019 at 16:04

2 Answers 2

8

So let's see what you're doing here:

/bin/find  .  -maxdepth 1 -type d  -ctime +2 -exec /bin/rm -rf {} \;

Find all folders in the current directory (.) created more than 2 days ago and execute rm -rf on it.

The current working directory for a cronjob is the users home directory, for root/sudo cronjobs it is /root.

If you were really lucky, you used sudo crontab, and it did no harm, as /root directory is usually not used in Ubuntu.

If not, you basically deleted all directories older than 2 days in your home. This should be more or less anything of importance. Desktop, Pictures, Documents, .config ...


What you should do instead:

Use full paths:

/bin/find /path/to/my/folder -maxdepth 1 -type d  -ctime +2 -exec /bin/rm -rf {} \;

In any way

  • be very careful with rm -rf, and do not use it unless you're 100% sure about it.
  • have a backup ready.
4
  • Hi RoVo, thanks , i was skipping the absolute path i.e. ( /path/to/my/folder ).
    – kmukeshk
    Feb 19, 2019 at 18:38
  • Now the script is working fine.
    – kmukeshk
    Feb 19, 2019 at 18:38
  • 1
    Hello, @kmukeshk, if this answer was helpful to you, then please consider marking it as the accepted answer (by click on the grey tick ✓ left to it) so others may more easily find it in the future. This is also a polite way to thank the person answering your question for helping you out.
    – pa4080
    Feb 19, 2019 at 19:12
  • /root directory is usually not used in Ubuntu, that doesn't mean applications that aren't written properly won't try to install stuff into user's home directory. It's probably one of the cases where two wrongs (the command in question and weird applications I've mentioned) don't make one right Feb 22, 2019 at 20:52
4

This script depends on what the current working directory (.) is, which is probably different when you run it manually vs. when cron executes it.

Use an absolute path to the folder you want to work on there instead.

2
  • 3
    cronjobs usually run from the home directory as pwd. For sudo crontab that is /root.
    – pLumo
    Feb 19, 2019 at 15:51
  • i have checked using absolute path, but no luck also.
    – kmukeshk
    Feb 19, 2019 at 18:12

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .