remove only empty folders with empty flag - is it safe












0














we create the following command in order to remove only the empty folders that older then 100min



find /tmp -type  d -empty -mmin +100 -printf '%p was deleted!!n' -delete


because we intend to run it from cron job on production machines



we want to understand if empty flag will also ignore folders with links or empty files



or in other words how empty flag check the folder ?










share|improve this question






















  • Empty means empty. A directory which contains anything (symbolic links, other directories, regular files empty or not, device files, sockets, whatever) is not empty.
    – AlexP
    Dec 17 at 23:32










  • @AlexP I would say that's an answer instead of a comment. If you post that and ping me, I'll come back and upvote...
    – Fabby
    Dec 17 at 23:43










  • Test, test, test
    – Jeff Schaller
    Dec 17 at 23:51










  • Test ? what you mean about test? ( it is difficult to cover all cases )
    – yael
    Dec 17 at 23:54












  • @yael I see you asking about safety and running code in production. Have a Test system and create test cases and & etc. No one here can test your environment like you can.
    – Jeff Schaller
    Dec 18 at 0:30
















0














we create the following command in order to remove only the empty folders that older then 100min



find /tmp -type  d -empty -mmin +100 -printf '%p was deleted!!n' -delete


because we intend to run it from cron job on production machines



we want to understand if empty flag will also ignore folders with links or empty files



or in other words how empty flag check the folder ?










share|improve this question






















  • Empty means empty. A directory which contains anything (symbolic links, other directories, regular files empty or not, device files, sockets, whatever) is not empty.
    – AlexP
    Dec 17 at 23:32










  • @AlexP I would say that's an answer instead of a comment. If you post that and ping me, I'll come back and upvote...
    – Fabby
    Dec 17 at 23:43










  • Test, test, test
    – Jeff Schaller
    Dec 17 at 23:51










  • Test ? what you mean about test? ( it is difficult to cover all cases )
    – yael
    Dec 17 at 23:54












  • @yael I see you asking about safety and running code in production. Have a Test system and create test cases and & etc. No one here can test your environment like you can.
    – Jeff Schaller
    Dec 18 at 0:30














0












0








0


2





we create the following command in order to remove only the empty folders that older then 100min



find /tmp -type  d -empty -mmin +100 -printf '%p was deleted!!n' -delete


because we intend to run it from cron job on production machines



we want to understand if empty flag will also ignore folders with links or empty files



or in other words how empty flag check the folder ?










share|improve this question













we create the following command in order to remove only the empty folders that older then 100min



find /tmp -type  d -empty -mmin +100 -printf '%p was deleted!!n' -delete


because we intend to run it from cron job on production machines



we want to understand if empty flag will also ignore folders with links or empty files



or in other words how empty flag check the folder ?







linux bash shell-script find






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Dec 17 at 23:27









yael

2,42112159




2,42112159












  • Empty means empty. A directory which contains anything (symbolic links, other directories, regular files empty or not, device files, sockets, whatever) is not empty.
    – AlexP
    Dec 17 at 23:32










  • @AlexP I would say that's an answer instead of a comment. If you post that and ping me, I'll come back and upvote...
    – Fabby
    Dec 17 at 23:43










  • Test, test, test
    – Jeff Schaller
    Dec 17 at 23:51










  • Test ? what you mean about test? ( it is difficult to cover all cases )
    – yael
    Dec 17 at 23:54












  • @yael I see you asking about safety and running code in production. Have a Test system and create test cases and & etc. No one here can test your environment like you can.
    – Jeff Schaller
    Dec 18 at 0:30


















  • Empty means empty. A directory which contains anything (symbolic links, other directories, regular files empty or not, device files, sockets, whatever) is not empty.
    – AlexP
    Dec 17 at 23:32










  • @AlexP I would say that's an answer instead of a comment. If you post that and ping me, I'll come back and upvote...
    – Fabby
    Dec 17 at 23:43










  • Test, test, test
    – Jeff Schaller
    Dec 17 at 23:51










  • Test ? what you mean about test? ( it is difficult to cover all cases )
    – yael
    Dec 17 at 23:54












  • @yael I see you asking about safety and running code in production. Have a Test system and create test cases and & etc. No one here can test your environment like you can.
    – Jeff Schaller
    Dec 18 at 0:30
















Empty means empty. A directory which contains anything (symbolic links, other directories, regular files empty or not, device files, sockets, whatever) is not empty.
– AlexP
Dec 17 at 23:32




Empty means empty. A directory which contains anything (symbolic links, other directories, regular files empty or not, device files, sockets, whatever) is not empty.
– AlexP
Dec 17 at 23:32












@AlexP I would say that's an answer instead of a comment. If you post that and ping me, I'll come back and upvote...
– Fabby
Dec 17 at 23:43




@AlexP I would say that's an answer instead of a comment. If you post that and ping me, I'll come back and upvote...
– Fabby
Dec 17 at 23:43












Test, test, test
– Jeff Schaller
Dec 17 at 23:51




Test, test, test
– Jeff Schaller
Dec 17 at 23:51












Test ? what you mean about test? ( it is difficult to cover all cases )
– yael
Dec 17 at 23:54






Test ? what you mean about test? ( it is difficult to cover all cases )
– yael
Dec 17 at 23:54














@yael I see you asking about safety and running code in production. Have a Test system and create test cases and & etc. No one here can test your environment like you can.
– Jeff Schaller
Dec 18 at 0:30




@yael I see you asking about safety and running code in production. Have a Test system and create test cases and & etc. No one here can test your environment like you can.
– Jeff Schaller
Dec 18 at 0:30










1 Answer
1






active

oldest

votes


















4














Empty means empty. If there is any sort of file in a directory, then the directory isn't empty. To illustrate:



$ mkdir dir{1..8}; 
ln -s /etc/ dir1/workingLink;
ln -s noSuchFile dir2/brokenLink;
mkfifo dir3/fifo;
touch dir4/emptyFile;
echo foo > dir5/nonEmptyFile;
touch dir6/.hiddenFile;
mkdir dir7/subdir
$ tree -a
.
├── dir1
│   └── workingLink -> /etc/
├── dir2
│   └── brokenLink -> noSuchFile
├── dir3
│   └── fifo
├── dir4
│   └── emptyFile
├── dir5
│   └── nonEmptyFile
├── dir6
│   └── .hiddenFile
├── dir7
│   └── subdir
└── dir8


So, we have a directory with a working symlink, one with a broken link (pointing to a non-existant file), one with a FiFo (a named pipe), one with an empty file, one with a file that isn't empty, one with a hidden file, one with a subdirectory and only one that's empty. Which one will be deleted?



$ find . -type d -empty -printf '%p WAS DELETED!n' -delete 
-or -printf '%p: not empty!n'
./dir8 WAS DELETED!
./dir3/fifo: not empty!
./dir3: not empty!
./dir5/nonEmptyFile: not empty!
./dir5: not empty!
./dir6/.hiddenFile: not empty!
./dir6: not empty!
./dir2/brokenLink: not empty!
./dir2: not empty!
./dir4/emptyFile: not empty!
./dir4: not empty!
./dir7/subdir WAS DELETED!
./dir7 WAS DELETED!
./dir1/workingLink: not empty!
./dir1: not empty!
.: not empty!


So, three things were deleted:




  1. The empty directory dir8.

  2. The empty (sub) directory dir7/subdir

  3. The (now) empty directory dir7. This was deleted because find first deleted dir7/subdir and then dir7 was empty so that was deleted as well.


So, any type of content in a directory will stop it from being deleted by this command, but you need to be careful in case the only thing in a directory is other, empty, directories. In that case, the find command will also delete the parent directory since it will be empty by the time it finishes.



If you don't want to remove subdirectories which might cause a parent to be removed, with GNU find (the default on Linux) you can use the -maxdepth flag to limit find to only the current directory:



$ find . -maxdepth 1 -type d -empty -printf '%p WAS DELETED!n' 
-delete -or -printf '%p: not empty!n'
./dir8 WAS DELETED!
./dir3: not empty!
./dir5: not empty!
./dir6: not empty!
./dir2: not empty!
./dir4: not empty!
./dir7: not empty!
./dir1: not empty!
.: not empty!





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',
    autoActivateHeartbeat: false,
    convertImagesToLinks: false,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    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
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f489585%2fremove-only-empty-folders-with-empty-flag-is-it-safe%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    4














    Empty means empty. If there is any sort of file in a directory, then the directory isn't empty. To illustrate:



    $ mkdir dir{1..8}; 
    ln -s /etc/ dir1/workingLink;
    ln -s noSuchFile dir2/brokenLink;
    mkfifo dir3/fifo;
    touch dir4/emptyFile;
    echo foo > dir5/nonEmptyFile;
    touch dir6/.hiddenFile;
    mkdir dir7/subdir
    $ tree -a
    .
    ├── dir1
    │   └── workingLink -> /etc/
    ├── dir2
    │   └── brokenLink -> noSuchFile
    ├── dir3
    │   └── fifo
    ├── dir4
    │   └── emptyFile
    ├── dir5
    │   └── nonEmptyFile
    ├── dir6
    │   └── .hiddenFile
    ├── dir7
    │   └── subdir
    └── dir8


    So, we have a directory with a working symlink, one with a broken link (pointing to a non-existant file), one with a FiFo (a named pipe), one with an empty file, one with a file that isn't empty, one with a hidden file, one with a subdirectory and only one that's empty. Which one will be deleted?



    $ find . -type d -empty -printf '%p WAS DELETED!n' -delete 
    -or -printf '%p: not empty!n'
    ./dir8 WAS DELETED!
    ./dir3/fifo: not empty!
    ./dir3: not empty!
    ./dir5/nonEmptyFile: not empty!
    ./dir5: not empty!
    ./dir6/.hiddenFile: not empty!
    ./dir6: not empty!
    ./dir2/brokenLink: not empty!
    ./dir2: not empty!
    ./dir4/emptyFile: not empty!
    ./dir4: not empty!
    ./dir7/subdir WAS DELETED!
    ./dir7 WAS DELETED!
    ./dir1/workingLink: not empty!
    ./dir1: not empty!
    .: not empty!


    So, three things were deleted:




    1. The empty directory dir8.

    2. The empty (sub) directory dir7/subdir

    3. The (now) empty directory dir7. This was deleted because find first deleted dir7/subdir and then dir7 was empty so that was deleted as well.


    So, any type of content in a directory will stop it from being deleted by this command, but you need to be careful in case the only thing in a directory is other, empty, directories. In that case, the find command will also delete the parent directory since it will be empty by the time it finishes.



    If you don't want to remove subdirectories which might cause a parent to be removed, with GNU find (the default on Linux) you can use the -maxdepth flag to limit find to only the current directory:



    $ find . -maxdepth 1 -type d -empty -printf '%p WAS DELETED!n' 
    -delete -or -printf '%p: not empty!n'
    ./dir8 WAS DELETED!
    ./dir3: not empty!
    ./dir5: not empty!
    ./dir6: not empty!
    ./dir2: not empty!
    ./dir4: not empty!
    ./dir7: not empty!
    ./dir1: not empty!
    .: not empty!





    share|improve this answer


























      4














      Empty means empty. If there is any sort of file in a directory, then the directory isn't empty. To illustrate:



      $ mkdir dir{1..8}; 
      ln -s /etc/ dir1/workingLink;
      ln -s noSuchFile dir2/brokenLink;
      mkfifo dir3/fifo;
      touch dir4/emptyFile;
      echo foo > dir5/nonEmptyFile;
      touch dir6/.hiddenFile;
      mkdir dir7/subdir
      $ tree -a
      .
      ├── dir1
      │   └── workingLink -> /etc/
      ├── dir2
      │   └── brokenLink -> noSuchFile
      ├── dir3
      │   └── fifo
      ├── dir4
      │   └── emptyFile
      ├── dir5
      │   └── nonEmptyFile
      ├── dir6
      │   └── .hiddenFile
      ├── dir7
      │   └── subdir
      └── dir8


      So, we have a directory with a working symlink, one with a broken link (pointing to a non-existant file), one with a FiFo (a named pipe), one with an empty file, one with a file that isn't empty, one with a hidden file, one with a subdirectory and only one that's empty. Which one will be deleted?



      $ find . -type d -empty -printf '%p WAS DELETED!n' -delete 
      -or -printf '%p: not empty!n'
      ./dir8 WAS DELETED!
      ./dir3/fifo: not empty!
      ./dir3: not empty!
      ./dir5/nonEmptyFile: not empty!
      ./dir5: not empty!
      ./dir6/.hiddenFile: not empty!
      ./dir6: not empty!
      ./dir2/brokenLink: not empty!
      ./dir2: not empty!
      ./dir4/emptyFile: not empty!
      ./dir4: not empty!
      ./dir7/subdir WAS DELETED!
      ./dir7 WAS DELETED!
      ./dir1/workingLink: not empty!
      ./dir1: not empty!
      .: not empty!


      So, three things were deleted:




      1. The empty directory dir8.

      2. The empty (sub) directory dir7/subdir

      3. The (now) empty directory dir7. This was deleted because find first deleted dir7/subdir and then dir7 was empty so that was deleted as well.


      So, any type of content in a directory will stop it from being deleted by this command, but you need to be careful in case the only thing in a directory is other, empty, directories. In that case, the find command will also delete the parent directory since it will be empty by the time it finishes.



      If you don't want to remove subdirectories which might cause a parent to be removed, with GNU find (the default on Linux) you can use the -maxdepth flag to limit find to only the current directory:



      $ find . -maxdepth 1 -type d -empty -printf '%p WAS DELETED!n' 
      -delete -or -printf '%p: not empty!n'
      ./dir8 WAS DELETED!
      ./dir3: not empty!
      ./dir5: not empty!
      ./dir6: not empty!
      ./dir2: not empty!
      ./dir4: not empty!
      ./dir7: not empty!
      ./dir1: not empty!
      .: not empty!





      share|improve this answer
























        4












        4








        4






        Empty means empty. If there is any sort of file in a directory, then the directory isn't empty. To illustrate:



        $ mkdir dir{1..8}; 
        ln -s /etc/ dir1/workingLink;
        ln -s noSuchFile dir2/brokenLink;
        mkfifo dir3/fifo;
        touch dir4/emptyFile;
        echo foo > dir5/nonEmptyFile;
        touch dir6/.hiddenFile;
        mkdir dir7/subdir
        $ tree -a
        .
        ├── dir1
        │   └── workingLink -> /etc/
        ├── dir2
        │   └── brokenLink -> noSuchFile
        ├── dir3
        │   └── fifo
        ├── dir4
        │   └── emptyFile
        ├── dir5
        │   └── nonEmptyFile
        ├── dir6
        │   └── .hiddenFile
        ├── dir7
        │   └── subdir
        └── dir8


        So, we have a directory with a working symlink, one with a broken link (pointing to a non-existant file), one with a FiFo (a named pipe), one with an empty file, one with a file that isn't empty, one with a hidden file, one with a subdirectory and only one that's empty. Which one will be deleted?



        $ find . -type d -empty -printf '%p WAS DELETED!n' -delete 
        -or -printf '%p: not empty!n'
        ./dir8 WAS DELETED!
        ./dir3/fifo: not empty!
        ./dir3: not empty!
        ./dir5/nonEmptyFile: not empty!
        ./dir5: not empty!
        ./dir6/.hiddenFile: not empty!
        ./dir6: not empty!
        ./dir2/brokenLink: not empty!
        ./dir2: not empty!
        ./dir4/emptyFile: not empty!
        ./dir4: not empty!
        ./dir7/subdir WAS DELETED!
        ./dir7 WAS DELETED!
        ./dir1/workingLink: not empty!
        ./dir1: not empty!
        .: not empty!


        So, three things were deleted:




        1. The empty directory dir8.

        2. The empty (sub) directory dir7/subdir

        3. The (now) empty directory dir7. This was deleted because find first deleted dir7/subdir and then dir7 was empty so that was deleted as well.


        So, any type of content in a directory will stop it from being deleted by this command, but you need to be careful in case the only thing in a directory is other, empty, directories. In that case, the find command will also delete the parent directory since it will be empty by the time it finishes.



        If you don't want to remove subdirectories which might cause a parent to be removed, with GNU find (the default on Linux) you can use the -maxdepth flag to limit find to only the current directory:



        $ find . -maxdepth 1 -type d -empty -printf '%p WAS DELETED!n' 
        -delete -or -printf '%p: not empty!n'
        ./dir8 WAS DELETED!
        ./dir3: not empty!
        ./dir5: not empty!
        ./dir6: not empty!
        ./dir2: not empty!
        ./dir4: not empty!
        ./dir7: not empty!
        ./dir1: not empty!
        .: not empty!





        share|improve this answer












        Empty means empty. If there is any sort of file in a directory, then the directory isn't empty. To illustrate:



        $ mkdir dir{1..8}; 
        ln -s /etc/ dir1/workingLink;
        ln -s noSuchFile dir2/brokenLink;
        mkfifo dir3/fifo;
        touch dir4/emptyFile;
        echo foo > dir5/nonEmptyFile;
        touch dir6/.hiddenFile;
        mkdir dir7/subdir
        $ tree -a
        .
        ├── dir1
        │   └── workingLink -> /etc/
        ├── dir2
        │   └── brokenLink -> noSuchFile
        ├── dir3
        │   └── fifo
        ├── dir4
        │   └── emptyFile
        ├── dir5
        │   └── nonEmptyFile
        ├── dir6
        │   └── .hiddenFile
        ├── dir7
        │   └── subdir
        └── dir8


        So, we have a directory with a working symlink, one with a broken link (pointing to a non-existant file), one with a FiFo (a named pipe), one with an empty file, one with a file that isn't empty, one with a hidden file, one with a subdirectory and only one that's empty. Which one will be deleted?



        $ find . -type d -empty -printf '%p WAS DELETED!n' -delete 
        -or -printf '%p: not empty!n'
        ./dir8 WAS DELETED!
        ./dir3/fifo: not empty!
        ./dir3: not empty!
        ./dir5/nonEmptyFile: not empty!
        ./dir5: not empty!
        ./dir6/.hiddenFile: not empty!
        ./dir6: not empty!
        ./dir2/brokenLink: not empty!
        ./dir2: not empty!
        ./dir4/emptyFile: not empty!
        ./dir4: not empty!
        ./dir7/subdir WAS DELETED!
        ./dir7 WAS DELETED!
        ./dir1/workingLink: not empty!
        ./dir1: not empty!
        .: not empty!


        So, three things were deleted:




        1. The empty directory dir8.

        2. The empty (sub) directory dir7/subdir

        3. The (now) empty directory dir7. This was deleted because find first deleted dir7/subdir and then dir7 was empty so that was deleted as well.


        So, any type of content in a directory will stop it from being deleted by this command, but you need to be careful in case the only thing in a directory is other, empty, directories. In that case, the find command will also delete the parent directory since it will be empty by the time it finishes.



        If you don't want to remove subdirectories which might cause a parent to be removed, with GNU find (the default on Linux) you can use the -maxdepth flag to limit find to only the current directory:



        $ find . -maxdepth 1 -type d -empty -printf '%p WAS DELETED!n' 
        -delete -or -printf '%p: not empty!n'
        ./dir8 WAS DELETED!
        ./dir3: not empty!
        ./dir5: not empty!
        ./dir6: not empty!
        ./dir2: not empty!
        ./dir4: not empty!
        ./dir7: not empty!
        ./dir1: not empty!
        .: not empty!






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Dec 18 at 0:04









        terdon

        128k31249423




        128k31249423






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Unix & Linux Stack Exchange!


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





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • 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%2funix.stackexchange.com%2fquestions%2f489585%2fremove-only-empty-folders-with-empty-flag-is-it-safe%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

            Morgemoulin

            Scott Moir

            Souastre