How to create a cron job that automatically delete files that are older than 30 days?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP








up vote
0
down vote

favorite












I'm using Ubuntu 14 desktop. I use this machine to backup other machines and as an FTP server for the security cameras.



I need to create a cron job that automatically deletes files that are older than 30 days. I did some search and I think I found the right command but I want to make sure I'm not missing something before executing it.



* 4 * * * find /home/USER/DIRECTORY1/DIRECTORY2/ -mindepth 1 -type f -mtime 29 -delete


Do I need to put "sudo" at the before the find command?



Do I need to put "+" before the number of days "29"?










share|improve this question

























    up vote
    0
    down vote

    favorite












    I'm using Ubuntu 14 desktop. I use this machine to backup other machines and as an FTP server for the security cameras.



    I need to create a cron job that automatically deletes files that are older than 30 days. I did some search and I think I found the right command but I want to make sure I'm not missing something before executing it.



    * 4 * * * find /home/USER/DIRECTORY1/DIRECTORY2/ -mindepth 1 -type f -mtime 29 -delete


    Do I need to put "sudo" at the before the find command?



    Do I need to put "+" before the number of days "29"?










    share|improve this question























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      I'm using Ubuntu 14 desktop. I use this machine to backup other machines and as an FTP server for the security cameras.



      I need to create a cron job that automatically deletes files that are older than 30 days. I did some search and I think I found the right command but I want to make sure I'm not missing something before executing it.



      * 4 * * * find /home/USER/DIRECTORY1/DIRECTORY2/ -mindepth 1 -type f -mtime 29 -delete


      Do I need to put "sudo" at the before the find command?



      Do I need to put "+" before the number of days "29"?










      share|improve this question













      I'm using Ubuntu 14 desktop. I use this machine to backup other machines and as an FTP server for the security cameras.



      I need to create a cron job that automatically deletes files that are older than 30 days. I did some search and I think I found the right command but I want to make sure I'm not missing something before executing it.



      * 4 * * * find /home/USER/DIRECTORY1/DIRECTORY2/ -mindepth 1 -type f -mtime 29 -delete


      Do I need to put "sudo" at the before the find command?



      Do I need to put "+" before the number of days "29"?







      cron delete






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Apr 12 at 22:23









      Ramez Dous

      3419




      3419




















          2 Answers
          2






          active

          oldest

          votes

















          up vote
          3
          down vote













          First, put your find ... command in a bash script, and call that script from your crontab. If you have an encrypted home directory (cat /home/.ecryptfs/$USER/.ecryptfs/Private.mnt) you'll have to store your script outside your $HOME directory tree. Keeping a command in crontab makes configuring, logging and debugging harder, and the crontab command parser isn't as clever as bash's.



          Second, always, Always, ALWAYS test find with -print, and get it to work, before considering -delete.



          Third, the find test "-mtime 29" is telling find "Find the file's mtime, and return True if it's equal to 29. You should use -mtime +29, which find sees as "more than 29", which is what you want. From man find:



           Numeric arguments can be specified as

          +n for greater than n,

          -n for less than n,

          n for exactly n.


          Fourth, be sure you have Write access to the directories in /home/USER/DIRECTORY1/DIRECTORY2/.



          Fifth, do you mean /home/USER/DIRECTORY1/DIRECTORY2/ or /home/$USER/DIRECTORY1/DIRECTORY2/? If $USER is for the user's userid, you have a problem: cron doesn't define $USER in the runtime enviroinment. It does define $HOME, so you could use $HOME/DIRECTORY1/DIRECTORY2.






          share|improve this answer






















          • @steeldriver Thank you, adjusted.
            – waltinator
            Apr 12 at 23:05










          • I don't get the fifth point!
            – Ramez Dous
            Apr 14 at 17:33

















          up vote
          2
          down vote













          I use one to delete backups older than 10 days and it looks something like this:



          50 17 * * * find /path/to/files/filename* -type f -mtime +10 | xargs rm



          I use filename* because they are for backups so they would look like this:



          filename04-04-2018.tar.gz
          filename04-05-2018.tar.gz
          filename04-06-2018.tar.gz





          share|improve this answer




















          • What does the "| xargs" mean?
            – Ramez Dous
            Apr 14 at 17:31










          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',
          convertImagesToLinks: true,
          noModals: false,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













           

          draft saved


          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f1024484%2fhow-to-create-a-cron-job-that-automatically-delete-files-that-are-older-than-30%23new-answer', 'question_page');

          );

          Post as a guest






























          2 Answers
          2






          active

          oldest

          votes








          2 Answers
          2






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          3
          down vote













          First, put your find ... command in a bash script, and call that script from your crontab. If you have an encrypted home directory (cat /home/.ecryptfs/$USER/.ecryptfs/Private.mnt) you'll have to store your script outside your $HOME directory tree. Keeping a command in crontab makes configuring, logging and debugging harder, and the crontab command parser isn't as clever as bash's.



          Second, always, Always, ALWAYS test find with -print, and get it to work, before considering -delete.



          Third, the find test "-mtime 29" is telling find "Find the file's mtime, and return True if it's equal to 29. You should use -mtime +29, which find sees as "more than 29", which is what you want. From man find:



           Numeric arguments can be specified as

          +n for greater than n,

          -n for less than n,

          n for exactly n.


          Fourth, be sure you have Write access to the directories in /home/USER/DIRECTORY1/DIRECTORY2/.



          Fifth, do you mean /home/USER/DIRECTORY1/DIRECTORY2/ or /home/$USER/DIRECTORY1/DIRECTORY2/? If $USER is for the user's userid, you have a problem: cron doesn't define $USER in the runtime enviroinment. It does define $HOME, so you could use $HOME/DIRECTORY1/DIRECTORY2.






          share|improve this answer






















          • @steeldriver Thank you, adjusted.
            – waltinator
            Apr 12 at 23:05










          • I don't get the fifth point!
            – Ramez Dous
            Apr 14 at 17:33














          up vote
          3
          down vote













          First, put your find ... command in a bash script, and call that script from your crontab. If you have an encrypted home directory (cat /home/.ecryptfs/$USER/.ecryptfs/Private.mnt) you'll have to store your script outside your $HOME directory tree. Keeping a command in crontab makes configuring, logging and debugging harder, and the crontab command parser isn't as clever as bash's.



          Second, always, Always, ALWAYS test find with -print, and get it to work, before considering -delete.



          Third, the find test "-mtime 29" is telling find "Find the file's mtime, and return True if it's equal to 29. You should use -mtime +29, which find sees as "more than 29", which is what you want. From man find:



           Numeric arguments can be specified as

          +n for greater than n,

          -n for less than n,

          n for exactly n.


          Fourth, be sure you have Write access to the directories in /home/USER/DIRECTORY1/DIRECTORY2/.



          Fifth, do you mean /home/USER/DIRECTORY1/DIRECTORY2/ or /home/$USER/DIRECTORY1/DIRECTORY2/? If $USER is for the user's userid, you have a problem: cron doesn't define $USER in the runtime enviroinment. It does define $HOME, so you could use $HOME/DIRECTORY1/DIRECTORY2.






          share|improve this answer






















          • @steeldriver Thank you, adjusted.
            – waltinator
            Apr 12 at 23:05










          • I don't get the fifth point!
            – Ramez Dous
            Apr 14 at 17:33












          up vote
          3
          down vote










          up vote
          3
          down vote









          First, put your find ... command in a bash script, and call that script from your crontab. If you have an encrypted home directory (cat /home/.ecryptfs/$USER/.ecryptfs/Private.mnt) you'll have to store your script outside your $HOME directory tree. Keeping a command in crontab makes configuring, logging and debugging harder, and the crontab command parser isn't as clever as bash's.



          Second, always, Always, ALWAYS test find with -print, and get it to work, before considering -delete.



          Third, the find test "-mtime 29" is telling find "Find the file's mtime, and return True if it's equal to 29. You should use -mtime +29, which find sees as "more than 29", which is what you want. From man find:



           Numeric arguments can be specified as

          +n for greater than n,

          -n for less than n,

          n for exactly n.


          Fourth, be sure you have Write access to the directories in /home/USER/DIRECTORY1/DIRECTORY2/.



          Fifth, do you mean /home/USER/DIRECTORY1/DIRECTORY2/ or /home/$USER/DIRECTORY1/DIRECTORY2/? If $USER is for the user's userid, you have a problem: cron doesn't define $USER in the runtime enviroinment. It does define $HOME, so you could use $HOME/DIRECTORY1/DIRECTORY2.






          share|improve this answer














          First, put your find ... command in a bash script, and call that script from your crontab. If you have an encrypted home directory (cat /home/.ecryptfs/$USER/.ecryptfs/Private.mnt) you'll have to store your script outside your $HOME directory tree. Keeping a command in crontab makes configuring, logging and debugging harder, and the crontab command parser isn't as clever as bash's.



          Second, always, Always, ALWAYS test find with -print, and get it to work, before considering -delete.



          Third, the find test "-mtime 29" is telling find "Find the file's mtime, and return True if it's equal to 29. You should use -mtime +29, which find sees as "more than 29", which is what you want. From man find:



           Numeric arguments can be specified as

          +n for greater than n,

          -n for less than n,

          n for exactly n.


          Fourth, be sure you have Write access to the directories in /home/USER/DIRECTORY1/DIRECTORY2/.



          Fifth, do you mean /home/USER/DIRECTORY1/DIRECTORY2/ or /home/$USER/DIRECTORY1/DIRECTORY2/? If $USER is for the user's userid, you have a problem: cron doesn't define $USER in the runtime enviroinment. It does define $HOME, so you could use $HOME/DIRECTORY1/DIRECTORY2.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Apr 12 at 23:04

























          answered Apr 12 at 22:43









          waltinator

          20.6k74068




          20.6k74068











          • @steeldriver Thank you, adjusted.
            – waltinator
            Apr 12 at 23:05










          • I don't get the fifth point!
            – Ramez Dous
            Apr 14 at 17:33
















          • @steeldriver Thank you, adjusted.
            – waltinator
            Apr 12 at 23:05










          • I don't get the fifth point!
            – Ramez Dous
            Apr 14 at 17:33















          @steeldriver Thank you, adjusted.
          – waltinator
          Apr 12 at 23:05




          @steeldriver Thank you, adjusted.
          – waltinator
          Apr 12 at 23:05












          I don't get the fifth point!
          – Ramez Dous
          Apr 14 at 17:33




          I don't get the fifth point!
          – Ramez Dous
          Apr 14 at 17:33












          up vote
          2
          down vote













          I use one to delete backups older than 10 days and it looks something like this:



          50 17 * * * find /path/to/files/filename* -type f -mtime +10 | xargs rm



          I use filename* because they are for backups so they would look like this:



          filename04-04-2018.tar.gz
          filename04-05-2018.tar.gz
          filename04-06-2018.tar.gz





          share|improve this answer




















          • What does the "| xargs" mean?
            – Ramez Dous
            Apr 14 at 17:31














          up vote
          2
          down vote













          I use one to delete backups older than 10 days and it looks something like this:



          50 17 * * * find /path/to/files/filename* -type f -mtime +10 | xargs rm



          I use filename* because they are for backups so they would look like this:



          filename04-04-2018.tar.gz
          filename04-05-2018.tar.gz
          filename04-06-2018.tar.gz





          share|improve this answer




















          • What does the "| xargs" mean?
            – Ramez Dous
            Apr 14 at 17:31












          up vote
          2
          down vote










          up vote
          2
          down vote









          I use one to delete backups older than 10 days and it looks something like this:



          50 17 * * * find /path/to/files/filename* -type f -mtime +10 | xargs rm



          I use filename* because they are for backups so they would look like this:



          filename04-04-2018.tar.gz
          filename04-05-2018.tar.gz
          filename04-06-2018.tar.gz





          share|improve this answer












          I use one to delete backups older than 10 days and it looks something like this:



          50 17 * * * find /path/to/files/filename* -type f -mtime +10 | xargs rm



          I use filename* because they are for backups so they would look like this:



          filename04-04-2018.tar.gz
          filename04-05-2018.tar.gz
          filename04-06-2018.tar.gz






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Apr 12 at 22:39









          troylatroy

          1,01211018




          1,01211018











          • What does the "| xargs" mean?
            – Ramez Dous
            Apr 14 at 17:31
















          • What does the "| xargs" mean?
            – Ramez Dous
            Apr 14 at 17:31















          What does the "| xargs" mean?
          – Ramez Dous
          Apr 14 at 17:31




          What does the "| xargs" mean?
          – Ramez Dous
          Apr 14 at 17:31

















           

          draft saved


          draft discarded















































           


          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f1024484%2fhow-to-create-a-cron-job-that-automatically-delete-files-that-are-older-than-30%23new-answer', 'question_page');

          );

          Post as a guest













































































          Popular posts from this blog

          pylint3 and pip3 broken

          Missing snmpget and snmpwalk

          How to enroll fingerprints to Ubuntu 17.10 with VFS491