How to create a cron job that automatically delete files that are older than 30 days?
![Creative The name of the picture](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgO9GURib1T8z7lCwjOGLQaGtrueEthgQ8LO42ZX8cOfTqDK4jvDDpKkLFwf2J49kYCMNW7d4ABih_XCb_2UXdq5fPJDkoyg7-8g_YfRUot-XnaXkNYycsNp7lA5_TW9td0FFpLQ2APzKcZ/s1600/1.jpg)
![Creative The name of the picture](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhYQ0N5W1qAOxLP7t7iOM6O6AzbZnkXUy16s7P_CWfOb5UbTQY_aDsc727chyphenhyphen5W4IppVNernMMQeaUFTB_rFzAd95_CDt-tnwN-nBx6JyUp2duGjPaL5-VgNO41AVsA_vu30EJcipdDG409/s400/Clash+Royale+CLAN+TAG%2523URR8PPP.png)
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"?
cron delete
add a comment |Â
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"?
cron delete
add a comment |Â
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"?
cron delete
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
cron delete
asked Apr 12 at 22:23
Ramez Dous
3419
3419
add a comment |Â
add a comment |Â
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
.
@steeldriver Thank you, adjusted.
â waltinator
Apr 12 at 23:05
I don't get the fifth point!
â Ramez Dous
Apr 14 at 17:33
add a comment |Â
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
What does the "| xargs" mean?
â Ramez Dous
Apr 14 at 17:31
add a comment |Â
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
.
@steeldriver Thank you, adjusted.
â waltinator
Apr 12 at 23:05
I don't get the fifth point!
â Ramez Dous
Apr 14 at 17:33
add a comment |Â
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
.
@steeldriver Thank you, adjusted.
â waltinator
Apr 12 at 23:05
I don't get the fifth point!
â Ramez Dous
Apr 14 at 17:33
add a comment |Â
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
.
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
.
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
add a comment |Â
@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
add a comment |Â
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
What does the "| xargs" mean?
â Ramez Dous
Apr 14 at 17:31
add a comment |Â
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
What does the "| xargs" mean?
â Ramez Dous
Apr 14 at 17:31
add a comment |Â
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
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
answered Apr 12 at 22:39
![](https://i.stack.imgur.com/aHSJn.jpg?s=32&g=1)
![](https://i.stack.imgur.com/aHSJn.jpg?s=32&g=1)
troylatroy
1,01211018
1,01211018
What does the "| xargs" mean?
â Ramez Dous
Apr 14 at 17:31
add a comment |Â
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
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e)
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom))
StackExchange.using('gps', function() StackExchange.gps.track('embedded_signup_form.view', location: 'question_page' ); );
$window.unbind('scroll', onScroll);
;
$window.on('scroll', onScroll);
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e)
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom))
StackExchange.using('gps', function() StackExchange.gps.track('embedded_signup_form.view', location: 'question_page' ); );
$window.unbind('scroll', onScroll);
;
$window.on('scroll', onScroll);
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e)
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom))
StackExchange.using('gps', function() StackExchange.gps.track('embedded_signup_form.view', location: 'question_page' ); );
$window.unbind('scroll', onScroll);
;
$window.on('scroll', onScroll);
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e)
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom))
StackExchange.using('gps', function() StackExchange.gps.track('embedded_signup_form.view', location: 'question_page' ); );
$window.unbind('scroll', onScroll);
;
$window.on('scroll', onScroll);
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password