Script using find runs fine when I execute it, but does not run correctly from cron












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









share|improve this question









New contributor




kmukeshk is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





















  • 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 ...

    – RoVo
    Feb 19 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 ...

    – RoVo
    Feb 19 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 at 15:57






  • 1





    In Ubuntu, at least for me, find is in /usr/bin/find ...

    – RoVo
    Feb 19 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.

    – glenn jackman
    Feb 19 at 16:04


















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









share|improve this question









New contributor




kmukeshk is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





















  • 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 ...

    – RoVo
    Feb 19 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 ...

    – RoVo
    Feb 19 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 at 15:57






  • 1





    In Ubuntu, at least for me, find is in /usr/bin/find ...

    – RoVo
    Feb 19 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.

    – glenn jackman
    Feb 19 at 16:04
















1












1








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









share|improve this question









New contributor




kmukeshk is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.












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






scripts cron find






share|improve this question









New contributor




kmukeshk is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question









New contributor




kmukeshk is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question








edited 8 hours ago









Zanna

50.8k13136241




50.8k13136241






New contributor




kmukeshk is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked Feb 19 at 15:40









kmukeshkkmukeshk

61




61




New contributor




kmukeshk is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





kmukeshk is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






kmukeshk is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.













  • 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 ...

    – RoVo
    Feb 19 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 ...

    – RoVo
    Feb 19 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 at 15:57






  • 1





    In Ubuntu, at least for me, find is in /usr/bin/find ...

    – RoVo
    Feb 19 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.

    – glenn jackman
    Feb 19 at 16:04





















  • 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 ...

    – RoVo
    Feb 19 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 ...

    – RoVo
    Feb 19 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 at 15:57






  • 1





    In Ubuntu, at least for me, find is in /usr/bin/find ...

    – RoVo
    Feb 19 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.

    – glenn jackman
    Feb 19 at 16:04



















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 ...

– RoVo
Feb 19 at 15:53







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 ...

– RoVo
Feb 19 at 15:53






6




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 ...

– RoVo
Feb 19 at 15:57





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 ...

– RoVo
Feb 19 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 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 at 15:57




1




1





In Ubuntu, at least for me, find is in /usr/bin/find ...

– RoVo
Feb 19 at 16:00







In Ubuntu, at least for me, find is in /usr/bin/find ...

– RoVo
Feb 19 at 16:00






3




3





I did something like this at work once on a production system. Took about 3 days to restore from tape backup. Lesson learned.

– glenn jackman
Feb 19 at 16:04







I did something like this at work once on a production system. Took about 3 days to restore from tape backup. Lesson learned.

– glenn jackman
Feb 19 at 16:04












2 Answers
2






active

oldest

votes


















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.






share|improve this answer


























  • Hi RoVo, thanks , i was skipping the absolute path i.e. ( /path/to/my/folder ).

    – kmukeshk
    Feb 19 at 18:38











  • Now the script is working fine.

    – kmukeshk
    Feb 19 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 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

    – Sergiy Kolodyazhnyy
    8 hours ago



















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.






share|improve this answer



















  • 3





    cronjobs usually run from the home directory as pwd. For sudo crontab that is /root.

    – RoVo
    Feb 19 at 15:51













  • i have checked using absolute path, but no luck also.

    – kmukeshk
    Feb 19 at 18:12











Your Answer








StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "89"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});






kmukeshk is a new contributor. Be nice, and check out our Code of Conduct.










draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f1119559%2fscript-using-find-runs-fine-when-i-execute-it-but-does-not-run-correctly-from-c%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









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.






share|improve this answer


























  • Hi RoVo, thanks , i was skipping the absolute path i.e. ( /path/to/my/folder ).

    – kmukeshk
    Feb 19 at 18:38











  • Now the script is working fine.

    – kmukeshk
    Feb 19 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 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

    – Sergiy Kolodyazhnyy
    8 hours ago
















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.






share|improve this answer


























  • Hi RoVo, thanks , i was skipping the absolute path i.e. ( /path/to/my/folder ).

    – kmukeshk
    Feb 19 at 18:38











  • Now the script is working fine.

    – kmukeshk
    Feb 19 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 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

    – Sergiy Kolodyazhnyy
    8 hours ago














8












8








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.






share|improve this answer















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.







share|improve this answer














share|improve this answer



share|improve this answer








edited Feb 19 at 16:24

























answered Feb 19 at 16:17









RoVoRoVo

7,3661841




7,3661841













  • Hi RoVo, thanks , i was skipping the absolute path i.e. ( /path/to/my/folder ).

    – kmukeshk
    Feb 19 at 18:38











  • Now the script is working fine.

    – kmukeshk
    Feb 19 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 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

    – Sergiy Kolodyazhnyy
    8 hours ago



















  • Hi RoVo, thanks , i was skipping the absolute path i.e. ( /path/to/my/folder ).

    – kmukeshk
    Feb 19 at 18:38











  • Now the script is working fine.

    – kmukeshk
    Feb 19 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 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

    – Sergiy Kolodyazhnyy
    8 hours ago

















Hi RoVo, thanks , i was skipping the absolute path i.e. ( /path/to/my/folder ).

– kmukeshk
Feb 19 at 18:38





Hi RoVo, thanks , i was skipping the absolute path i.e. ( /path/to/my/folder ).

– kmukeshk
Feb 19 at 18:38













Now the script is working fine.

– kmukeshk
Feb 19 at 18:38





Now the script is working fine.

– kmukeshk
Feb 19 at 18:38




1




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 at 19:12





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 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

– Sergiy Kolodyazhnyy
8 hours ago





/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

– Sergiy Kolodyazhnyy
8 hours ago













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.






share|improve this answer



















  • 3





    cronjobs usually run from the home directory as pwd. For sudo crontab that is /root.

    – RoVo
    Feb 19 at 15:51













  • i have checked using absolute path, but no luck also.

    – kmukeshk
    Feb 19 at 18:12
















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.






share|improve this answer



















  • 3





    cronjobs usually run from the home directory as pwd. For sudo crontab that is /root.

    – RoVo
    Feb 19 at 15:51













  • i have checked using absolute path, but no luck also.

    – kmukeshk
    Feb 19 at 18:12














4












4








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.






share|improve this answer













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.







share|improve this answer












share|improve this answer



share|improve this answer










answered Feb 19 at 15:42









Byte CommanderByte Commander

65k27178299




65k27178299








  • 3





    cronjobs usually run from the home directory as pwd. For sudo crontab that is /root.

    – RoVo
    Feb 19 at 15:51













  • i have checked using absolute path, but no luck also.

    – kmukeshk
    Feb 19 at 18:12














  • 3





    cronjobs usually run from the home directory as pwd. For sudo crontab that is /root.

    – RoVo
    Feb 19 at 15:51













  • i have checked using absolute path, but no luck also.

    – kmukeshk
    Feb 19 at 18:12








3




3





cronjobs usually run from the home directory as pwd. For sudo crontab that is /root.

– RoVo
Feb 19 at 15:51







cronjobs usually run from the home directory as pwd. For sudo crontab that is /root.

– RoVo
Feb 19 at 15:51















i have checked using absolute path, but no luck also.

– kmukeshk
Feb 19 at 18:12





i have checked using absolute path, but no luck also.

– kmukeshk
Feb 19 at 18:12










kmukeshk is a new contributor. Be nice, and check out our Code of Conduct.










draft saved

draft discarded


















kmukeshk is a new contributor. Be nice, and check out our Code of Conduct.













kmukeshk is a new contributor. Be nice, and check out our Code of Conduct.












kmukeshk is a new contributor. Be nice, and check out our Code of Conduct.
















Thanks for contributing an answer to Ask Ubuntu!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f1119559%2fscript-using-find-runs-fine-when-i-execute-it-but-does-not-run-correctly-from-c%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

How did Captain America manage to do this?

迪纳利

南乌拉尔铁路局