Find and rm command deleted the files inside the directory and the directory itself
Clash Royale CLAN TAG#URR8PPP
up vote
-1
down vote
favorite
Can you please check this command? It's deleting the files and the current directory itself. Script is run via cron.
Directories:
/log/dir/
/log/dir/tmp/
Commands in script:
find /log/dir/ -mtime +7 -name "*" -exec rm -rf ;
find /log/dir/tmp -mtime +7 -name "*" -exec rm -rf ;
Testing:
Run the find /log/dir/ -mtime +7 -name "*"
to check the files and I got result. After running the script, the file was deleted. After some days, the folders got deleted as well (the dir in /log/dir/
and the tmp in /log/dir/tmp
).
Expectation should be only the files/directories INSIDE the path provided older than 7 days will be deleted.
Linux redhat6.5
linux files find directory rm
add a comment |Â
up vote
-1
down vote
favorite
Can you please check this command? It's deleting the files and the current directory itself. Script is run via cron.
Directories:
/log/dir/
/log/dir/tmp/
Commands in script:
find /log/dir/ -mtime +7 -name "*" -exec rm -rf ;
find /log/dir/tmp -mtime +7 -name "*" -exec rm -rf ;
Testing:
Run the find /log/dir/ -mtime +7 -name "*"
to check the files and I got result. After running the script, the file was deleted. After some days, the folders got deleted as well (the dir in /log/dir/
and the tmp in /log/dir/tmp
).
Expectation should be only the files/directories INSIDE the path provided older than 7 days will be deleted.
Linux redhat6.5
linux files find directory rm
add '-mindepth 1' and remove -name "*"
â MetNP
Feb 27 at 3:26
find /log/dir/ -mtime +7 -mindepth 1 -exec rm -rf ; --- like this?
â chelseagirl01
Feb 27 at 6:55
yes... '-mindepth 1' ensures not-deleting main directory, not mentioned bellow, but '-type f' will also disallow it well.
â MetNP
Mar 1 at 21:23
add a comment |Â
up vote
-1
down vote
favorite
up vote
-1
down vote
favorite
Can you please check this command? It's deleting the files and the current directory itself. Script is run via cron.
Directories:
/log/dir/
/log/dir/tmp/
Commands in script:
find /log/dir/ -mtime +7 -name "*" -exec rm -rf ;
find /log/dir/tmp -mtime +7 -name "*" -exec rm -rf ;
Testing:
Run the find /log/dir/ -mtime +7 -name "*"
to check the files and I got result. After running the script, the file was deleted. After some days, the folders got deleted as well (the dir in /log/dir/
and the tmp in /log/dir/tmp
).
Expectation should be only the files/directories INSIDE the path provided older than 7 days will be deleted.
Linux redhat6.5
linux files find directory rm
Can you please check this command? It's deleting the files and the current directory itself. Script is run via cron.
Directories:
/log/dir/
/log/dir/tmp/
Commands in script:
find /log/dir/ -mtime +7 -name "*" -exec rm -rf ;
find /log/dir/tmp -mtime +7 -name "*" -exec rm -rf ;
Testing:
Run the find /log/dir/ -mtime +7 -name "*"
to check the files and I got result. After running the script, the file was deleted. After some days, the folders got deleted as well (the dir in /log/dir/
and the tmp in /log/dir/tmp
).
Expectation should be only the files/directories INSIDE the path provided older than 7 days will be deleted.
Linux redhat6.5
linux files find directory rm
edited Feb 27 at 10:20
Jeff Schaller
31.2k846105
31.2k846105
asked Feb 27 at 3:15
chelseagirl01
32
32
add '-mindepth 1' and remove -name "*"
â MetNP
Feb 27 at 3:26
find /log/dir/ -mtime +7 -mindepth 1 -exec rm -rf ; --- like this?
â chelseagirl01
Feb 27 at 6:55
yes... '-mindepth 1' ensures not-deleting main directory, not mentioned bellow, but '-type f' will also disallow it well.
â MetNP
Mar 1 at 21:23
add a comment |Â
add '-mindepth 1' and remove -name "*"
â MetNP
Feb 27 at 3:26
find /log/dir/ -mtime +7 -mindepth 1 -exec rm -rf ; --- like this?
â chelseagirl01
Feb 27 at 6:55
yes... '-mindepth 1' ensures not-deleting main directory, not mentioned bellow, but '-type f' will also disallow it well.
â MetNP
Mar 1 at 21:23
add '-mindepth 1' and remove -name "*"
â MetNP
Feb 27 at 3:26
add '-mindepth 1' and remove -name "*"
â MetNP
Feb 27 at 3:26
find /log/dir/ -mtime +7 -mindepth 1 -exec rm -rf ; --- like this?
â chelseagirl01
Feb 27 at 6:55
find /log/dir/ -mtime +7 -mindepth 1 -exec rm -rf ; --- like this?
â chelseagirl01
Feb 27 at 6:55
yes... '-mindepth 1' ensures not-deleting main directory, not mentioned bellow, but '-type f' will also disallow it well.
â MetNP
Mar 1 at 21:23
yes... '-mindepth 1' ensures not-deleting main directory, not mentioned bellow, but '-type f' will also disallow it well.
â MetNP
Mar 1 at 21:23
add a comment |Â
2 Answers
2
active
oldest
votes
up vote
2
down vote
accepted
If you want to use find
to delete only regular files and no directories, then make absolutely sure that you use -type f
. This will prevent pathnames that refer to directories (or anything that is not a regular file) to be processed. The directories got deleted since their last modified timestamp fulfilled the criteria for deletion.
Also, if you are deleting files, don't use rm -r
since recursing into a regular file does not make sense. With most find
implementations you can also choose to use -delete
rather than -exec rm
.
The -name "*"
is a no-op and could be removed since every name matches *
.
If you run find over /log/dir
, then you don't need to run it on /log/dir/tmp
as the latter will be processed as part of the former. If you intend to only look in these two directories, but not in subdirectories, add -maxdepth 1
to find
(if it supports it), and in that case, you do need to process them separately (but this may be done in one find
invocation as the utility takes any number of top-directories).
Suggestion, depending on what it is you want to achieve:
find /log/dir /log/dir/tmp -maxdepth 1 -type f -mtime +7 -delete
The mtime
of a directory is updated when a file is added or deleted from the directory. It is therefore unclear whether your attempts to delete directories based on the last modified timestamp is a good idea to start with.
For rotation of log files, you may also look into using logrotate
or some similar utility, from a cron job.
Yes, it turned out that it deleted the directory itself because it fits the criteria of being older than 7days. Let's say the two directories are different: /log/dir and /otherdir/tmp Can I still include it in one line such as find /log/dir /otherdir/tmp -maxdepth 1 -type f -mtime +7 -delete Will that be okay?
â chelseagirl01
Feb 28 at 1:20
@chelseagirl01 That command will look for regular files that have a modification timestamp of more than a week ago and delete them. It will look inside the two directories/log/dir
and/otherdir/tmp
, but not in their subdirectories. If that's what you want, then it will be okay.
â Kusalananda
Feb 28 at 6:34
add a comment |Â
up vote
0
down vote
You need to define what you mean by 'older than 7 days'. Could be accessed more than 7 days ago, changed more than 7 days ago or created more than 7 days ago. All different situations. Also are you sure you want to consider files and directories?
If you are looking to delete files and directories, then the 2nd command is redundant, since /log/dir/tmp
would be listed under /log/dir
.
Using -r
on rm is careless in this situation and will probably delete more than what you want to. Find is already listing all files/directories recursively so you are jumping ahead of find's output by deleting recursively. Stick to rm -f
and let find list the files to delete.
What I think you are trying to do is simply:
find /log/dir/ -type f -ctime +7 -exec rm -f ;
add a comment |Â
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
accepted
If you want to use find
to delete only regular files and no directories, then make absolutely sure that you use -type f
. This will prevent pathnames that refer to directories (or anything that is not a regular file) to be processed. The directories got deleted since their last modified timestamp fulfilled the criteria for deletion.
Also, if you are deleting files, don't use rm -r
since recursing into a regular file does not make sense. With most find
implementations you can also choose to use -delete
rather than -exec rm
.
The -name "*"
is a no-op and could be removed since every name matches *
.
If you run find over /log/dir
, then you don't need to run it on /log/dir/tmp
as the latter will be processed as part of the former. If you intend to only look in these two directories, but not in subdirectories, add -maxdepth 1
to find
(if it supports it), and in that case, you do need to process them separately (but this may be done in one find
invocation as the utility takes any number of top-directories).
Suggestion, depending on what it is you want to achieve:
find /log/dir /log/dir/tmp -maxdepth 1 -type f -mtime +7 -delete
The mtime
of a directory is updated when a file is added or deleted from the directory. It is therefore unclear whether your attempts to delete directories based on the last modified timestamp is a good idea to start with.
For rotation of log files, you may also look into using logrotate
or some similar utility, from a cron job.
Yes, it turned out that it deleted the directory itself because it fits the criteria of being older than 7days. Let's say the two directories are different: /log/dir and /otherdir/tmp Can I still include it in one line such as find /log/dir /otherdir/tmp -maxdepth 1 -type f -mtime +7 -delete Will that be okay?
â chelseagirl01
Feb 28 at 1:20
@chelseagirl01 That command will look for regular files that have a modification timestamp of more than a week ago and delete them. It will look inside the two directories/log/dir
and/otherdir/tmp
, but not in their subdirectories. If that's what you want, then it will be okay.
â Kusalananda
Feb 28 at 6:34
add a comment |Â
up vote
2
down vote
accepted
If you want to use find
to delete only regular files and no directories, then make absolutely sure that you use -type f
. This will prevent pathnames that refer to directories (or anything that is not a regular file) to be processed. The directories got deleted since their last modified timestamp fulfilled the criteria for deletion.
Also, if you are deleting files, don't use rm -r
since recursing into a regular file does not make sense. With most find
implementations you can also choose to use -delete
rather than -exec rm
.
The -name "*"
is a no-op and could be removed since every name matches *
.
If you run find over /log/dir
, then you don't need to run it on /log/dir/tmp
as the latter will be processed as part of the former. If you intend to only look in these two directories, but not in subdirectories, add -maxdepth 1
to find
(if it supports it), and in that case, you do need to process them separately (but this may be done in one find
invocation as the utility takes any number of top-directories).
Suggestion, depending on what it is you want to achieve:
find /log/dir /log/dir/tmp -maxdepth 1 -type f -mtime +7 -delete
The mtime
of a directory is updated when a file is added or deleted from the directory. It is therefore unclear whether your attempts to delete directories based on the last modified timestamp is a good idea to start with.
For rotation of log files, you may also look into using logrotate
or some similar utility, from a cron job.
Yes, it turned out that it deleted the directory itself because it fits the criteria of being older than 7days. Let's say the two directories are different: /log/dir and /otherdir/tmp Can I still include it in one line such as find /log/dir /otherdir/tmp -maxdepth 1 -type f -mtime +7 -delete Will that be okay?
â chelseagirl01
Feb 28 at 1:20
@chelseagirl01 That command will look for regular files that have a modification timestamp of more than a week ago and delete them. It will look inside the two directories/log/dir
and/otherdir/tmp
, but not in their subdirectories. If that's what you want, then it will be okay.
â Kusalananda
Feb 28 at 6:34
add a comment |Â
up vote
2
down vote
accepted
up vote
2
down vote
accepted
If you want to use find
to delete only regular files and no directories, then make absolutely sure that you use -type f
. This will prevent pathnames that refer to directories (or anything that is not a regular file) to be processed. The directories got deleted since their last modified timestamp fulfilled the criteria for deletion.
Also, if you are deleting files, don't use rm -r
since recursing into a regular file does not make sense. With most find
implementations you can also choose to use -delete
rather than -exec rm
.
The -name "*"
is a no-op and could be removed since every name matches *
.
If you run find over /log/dir
, then you don't need to run it on /log/dir/tmp
as the latter will be processed as part of the former. If you intend to only look in these two directories, but not in subdirectories, add -maxdepth 1
to find
(if it supports it), and in that case, you do need to process them separately (but this may be done in one find
invocation as the utility takes any number of top-directories).
Suggestion, depending on what it is you want to achieve:
find /log/dir /log/dir/tmp -maxdepth 1 -type f -mtime +7 -delete
The mtime
of a directory is updated when a file is added or deleted from the directory. It is therefore unclear whether your attempts to delete directories based on the last modified timestamp is a good idea to start with.
For rotation of log files, you may also look into using logrotate
or some similar utility, from a cron job.
If you want to use find
to delete only regular files and no directories, then make absolutely sure that you use -type f
. This will prevent pathnames that refer to directories (or anything that is not a regular file) to be processed. The directories got deleted since their last modified timestamp fulfilled the criteria for deletion.
Also, if you are deleting files, don't use rm -r
since recursing into a regular file does not make sense. With most find
implementations you can also choose to use -delete
rather than -exec rm
.
The -name "*"
is a no-op and could be removed since every name matches *
.
If you run find over /log/dir
, then you don't need to run it on /log/dir/tmp
as the latter will be processed as part of the former. If you intend to only look in these two directories, but not in subdirectories, add -maxdepth 1
to find
(if it supports it), and in that case, you do need to process them separately (but this may be done in one find
invocation as the utility takes any number of top-directories).
Suggestion, depending on what it is you want to achieve:
find /log/dir /log/dir/tmp -maxdepth 1 -type f -mtime +7 -delete
The mtime
of a directory is updated when a file is added or deleted from the directory. It is therefore unclear whether your attempts to delete directories based on the last modified timestamp is a good idea to start with.
For rotation of log files, you may also look into using logrotate
or some similar utility, from a cron job.
edited May 20 at 8:06
answered Feb 27 at 8:50
Kusalananda
103k13202318
103k13202318
Yes, it turned out that it deleted the directory itself because it fits the criteria of being older than 7days. Let's say the two directories are different: /log/dir and /otherdir/tmp Can I still include it in one line such as find /log/dir /otherdir/tmp -maxdepth 1 -type f -mtime +7 -delete Will that be okay?
â chelseagirl01
Feb 28 at 1:20
@chelseagirl01 That command will look for regular files that have a modification timestamp of more than a week ago and delete them. It will look inside the two directories/log/dir
and/otherdir/tmp
, but not in their subdirectories. If that's what you want, then it will be okay.
â Kusalananda
Feb 28 at 6:34
add a comment |Â
Yes, it turned out that it deleted the directory itself because it fits the criteria of being older than 7days. Let's say the two directories are different: /log/dir and /otherdir/tmp Can I still include it in one line such as find /log/dir /otherdir/tmp -maxdepth 1 -type f -mtime +7 -delete Will that be okay?
â chelseagirl01
Feb 28 at 1:20
@chelseagirl01 That command will look for regular files that have a modification timestamp of more than a week ago and delete them. It will look inside the two directories/log/dir
and/otherdir/tmp
, but not in their subdirectories. If that's what you want, then it will be okay.
â Kusalananda
Feb 28 at 6:34
Yes, it turned out that it deleted the directory itself because it fits the criteria of being older than 7days. Let's say the two directories are different: /log/dir and /otherdir/tmp Can I still include it in one line such as find /log/dir /otherdir/tmp -maxdepth 1 -type f -mtime +7 -delete Will that be okay?
â chelseagirl01
Feb 28 at 1:20
Yes, it turned out that it deleted the directory itself because it fits the criteria of being older than 7days. Let's say the two directories are different: /log/dir and /otherdir/tmp Can I still include it in one line such as find /log/dir /otherdir/tmp -maxdepth 1 -type f -mtime +7 -delete Will that be okay?
â chelseagirl01
Feb 28 at 1:20
@chelseagirl01 That command will look for regular files that have a modification timestamp of more than a week ago and delete them. It will look inside the two directories
/log/dir
and /otherdir/tmp
, but not in their subdirectories. If that's what you want, then it will be okay.â Kusalananda
Feb 28 at 6:34
@chelseagirl01 That command will look for regular files that have a modification timestamp of more than a week ago and delete them. It will look inside the two directories
/log/dir
and /otherdir/tmp
, but not in their subdirectories. If that's what you want, then it will be okay.â Kusalananda
Feb 28 at 6:34
add a comment |Â
up vote
0
down vote
You need to define what you mean by 'older than 7 days'. Could be accessed more than 7 days ago, changed more than 7 days ago or created more than 7 days ago. All different situations. Also are you sure you want to consider files and directories?
If you are looking to delete files and directories, then the 2nd command is redundant, since /log/dir/tmp
would be listed under /log/dir
.
Using -r
on rm is careless in this situation and will probably delete more than what you want to. Find is already listing all files/directories recursively so you are jumping ahead of find's output by deleting recursively. Stick to rm -f
and let find list the files to delete.
What I think you are trying to do is simply:
find /log/dir/ -type f -ctime +7 -exec rm -f ;
add a comment |Â
up vote
0
down vote
You need to define what you mean by 'older than 7 days'. Could be accessed more than 7 days ago, changed more than 7 days ago or created more than 7 days ago. All different situations. Also are you sure you want to consider files and directories?
If you are looking to delete files and directories, then the 2nd command is redundant, since /log/dir/tmp
would be listed under /log/dir
.
Using -r
on rm is careless in this situation and will probably delete more than what you want to. Find is already listing all files/directories recursively so you are jumping ahead of find's output by deleting recursively. Stick to rm -f
and let find list the files to delete.
What I think you are trying to do is simply:
find /log/dir/ -type f -ctime +7 -exec rm -f ;
add a comment |Â
up vote
0
down vote
up vote
0
down vote
You need to define what you mean by 'older than 7 days'. Could be accessed more than 7 days ago, changed more than 7 days ago or created more than 7 days ago. All different situations. Also are you sure you want to consider files and directories?
If you are looking to delete files and directories, then the 2nd command is redundant, since /log/dir/tmp
would be listed under /log/dir
.
Using -r
on rm is careless in this situation and will probably delete more than what you want to. Find is already listing all files/directories recursively so you are jumping ahead of find's output by deleting recursively. Stick to rm -f
and let find list the files to delete.
What I think you are trying to do is simply:
find /log/dir/ -type f -ctime +7 -exec rm -f ;
You need to define what you mean by 'older than 7 days'. Could be accessed more than 7 days ago, changed more than 7 days ago or created more than 7 days ago. All different situations. Also are you sure you want to consider files and directories?
If you are looking to delete files and directories, then the 2nd command is redundant, since /log/dir/tmp
would be listed under /log/dir
.
Using -r
on rm is careless in this situation and will probably delete more than what you want to. Find is already listing all files/directories recursively so you are jumping ahead of find's output by deleting recursively. Stick to rm -f
and let find list the files to delete.
What I think you are trying to do is simply:
find /log/dir/ -type f -ctime +7 -exec rm -f ;
answered Feb 27 at 9:46
Pedro
59429
59429
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
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%2funix.stackexchange.com%2fquestions%2f426839%2ffind-and-rm-command-deleted-the-files-inside-the-directory-and-the-directory-its%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
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');
);
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');
);
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
add '-mindepth 1' and remove -name "*"
â MetNP
Feb 27 at 3:26
find /log/dir/ -mtime +7 -mindepth 1 -exec rm -rf ; --- like this?
â chelseagirl01
Feb 27 at 6:55
yes... '-mindepth 1' ensures not-deleting main directory, not mentioned bellow, but '-type f' will also disallow it well.
â MetNP
Mar 1 at 21:23