Find and rm command deleted the files inside the directory and the directory itself

The name of the pictureThe name of the pictureThe name of the pictureClash 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







share|improve this question






















  • 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














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







share|improve this question






















  • 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












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







share|improve this question














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









share|improve this question













share|improve this question




share|improve this question








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










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.






share|improve this answer






















  • 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

















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 ;






share|improve this answer




















    Your Answer







    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "106"
    ;
    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: false,
    noModals: false,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    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%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






























    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.






    share|improve this answer






















    • 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














    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.






    share|improve this answer






















    • 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












    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.






    share|improve this answer














    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.







    share|improve this answer














    share|improve this answer



    share|improve this answer








    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
















    • 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












    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 ;






    share|improve this answer
























      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 ;






      share|improve this answer






















        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 ;






        share|improve this answer












        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 ;







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Feb 27 at 9:46









        Pedro

        59429




        59429






















             

            draft saved


            draft discarded


























             


            draft saved


            draft discarded














            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













































































            Popular posts from this blog

            How to check contact read email or not when send email to Individual?

            Bahrain

            Postfix configuration issue with fips on centos 7; mailgun relay