conditional find output missing












3















Im trying to build a conditional statement to search for files of a certain size (in this case 1Gb.



if [ "find /location/sub/int/ -size +1G" ]
then
> /location/sub/int/large_file_audit.txt
fi


I run this and it creates a file but the file is empty, how can I get the results of the find to populate into the file? what am I doing wrong?










share|improve this question





























    3















    Im trying to build a conditional statement to search for files of a certain size (in this case 1Gb.



    if [ "find /location/sub/int/ -size +1G" ]
    then
    > /location/sub/int/large_file_audit.txt
    fi


    I run this and it creates a file but the file is empty, how can I get the results of the find to populate into the file? what am I doing wrong?










    share|improve this question



























      3












      3








      3








      Im trying to build a conditional statement to search for files of a certain size (in this case 1Gb.



      if [ "find /location/sub/int/ -size +1G" ]
      then
      > /location/sub/int/large_file_audit.txt
      fi


      I run this and it creates a file but the file is empty, how can I get the results of the find to populate into the file? what am I doing wrong?










      share|improve this question
















      Im trying to build a conditional statement to search for files of a certain size (in this case 1Gb.



      if [ "find /location/sub/int/ -size +1G" ]
      then
      > /location/sub/int/large_file_audit.txt
      fi


      I run this and it creates a file but the file is empty, how can I get the results of the find to populate into the file? what am I doing wrong?







      command-line bash find






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Jan 18 at 21:56









      Ravexina

      32.3k1483113




      32.3k1483113










      asked Jan 18 at 21:32









      ShadwarShadwar

      162




      162






















          2 Answers
          2






          active

          oldest

          votes


















          6














          Your test if [ "find /location/sub/int/ -size +1G" ] doesn't work the way you intend because it tests the non-emptiness of the string "find /location/sub/int/ -size +1G" - which will always be true. In any case, the redirection > /location/sub/int/large_file_audit.txt will not magically pick up the standard output of the preceding command, so will always create an empty file.



          Perhaps the closest to your intent in Bash would be to put the results of find into an array, and then test whether it has any elements:



          mapfile -t files < <(find /location/sub/int/ -size +1G)

          if (( ${#files[@] > 0 )); then
          printf '%sn' "${files[@]}" > /location/sub/int/large_file_audit.txt
          fi


          This won't gracefully handle filenames containing newlines - with newer versions of bash, you could make the find and mapfile null-delimited, but there's not much benefit if you're outputting them as a newline-delimited list anyhow.






          share|improve this answer































            4














            One liner workaround:



            find -size +10G | grep ".*" > file.log || (rm file.log; echo "Can't find anything")


            Or:



            find -size +10G | grep ".*" > file.log || rm file.log




            Note that find returns 1 (False) when files are not processed correctly for any reason, so I suggest using something like:



            #!/bin/bash
            RESULTS=$(find /path -size +1G)
            if [ -n "$RESULTS" ];
            then
            echo "$RESULTS" > /path/file.log
            fi


            First run the find and put the results in a variable, then if the variable contained anything save that into a log file.






            share|improve this answer





















            • 2





              While the answer is all correct, you might want to touch on why original if [ "find /location/sub/int/ -size +1G" ] does not work as intended ( and what it actually does instead of what user expects ).

              – Sergiy Kolodyazhnyy
              Jan 18 at 22:54






            • 1





              find returns 1 if I ask it to look somewhere that doesn't exist, but if the path exists but -size finds nothing, it does return 0. (and 1 is false).

              – Xen2050
              Jan 19 at 2:54











            • @Xen2050 To be exact , find returns 1 for any case where directory entries were not processed correctly as stated in EXIT STATUS part of the manual, which includes permission denied error on subdirectories or parent directory (try with chmod -r or touch testdir/subdir/foobar2;chmod -x testdir ).

              – Sergiy Kolodyazhnyy
              Jan 19 at 4:40











            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',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            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%2faskubuntu.com%2fquestions%2f1110989%2fconditional-find-output-missing%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            6














            Your test if [ "find /location/sub/int/ -size +1G" ] doesn't work the way you intend because it tests the non-emptiness of the string "find /location/sub/int/ -size +1G" - which will always be true. In any case, the redirection > /location/sub/int/large_file_audit.txt will not magically pick up the standard output of the preceding command, so will always create an empty file.



            Perhaps the closest to your intent in Bash would be to put the results of find into an array, and then test whether it has any elements:



            mapfile -t files < <(find /location/sub/int/ -size +1G)

            if (( ${#files[@] > 0 )); then
            printf '%sn' "${files[@]}" > /location/sub/int/large_file_audit.txt
            fi


            This won't gracefully handle filenames containing newlines - with newer versions of bash, you could make the find and mapfile null-delimited, but there's not much benefit if you're outputting them as a newline-delimited list anyhow.






            share|improve this answer




























              6














              Your test if [ "find /location/sub/int/ -size +1G" ] doesn't work the way you intend because it tests the non-emptiness of the string "find /location/sub/int/ -size +1G" - which will always be true. In any case, the redirection > /location/sub/int/large_file_audit.txt will not magically pick up the standard output of the preceding command, so will always create an empty file.



              Perhaps the closest to your intent in Bash would be to put the results of find into an array, and then test whether it has any elements:



              mapfile -t files < <(find /location/sub/int/ -size +1G)

              if (( ${#files[@] > 0 )); then
              printf '%sn' "${files[@]}" > /location/sub/int/large_file_audit.txt
              fi


              This won't gracefully handle filenames containing newlines - with newer versions of bash, you could make the find and mapfile null-delimited, but there's not much benefit if you're outputting them as a newline-delimited list anyhow.






              share|improve this answer


























                6












                6








                6







                Your test if [ "find /location/sub/int/ -size +1G" ] doesn't work the way you intend because it tests the non-emptiness of the string "find /location/sub/int/ -size +1G" - which will always be true. In any case, the redirection > /location/sub/int/large_file_audit.txt will not magically pick up the standard output of the preceding command, so will always create an empty file.



                Perhaps the closest to your intent in Bash would be to put the results of find into an array, and then test whether it has any elements:



                mapfile -t files < <(find /location/sub/int/ -size +1G)

                if (( ${#files[@] > 0 )); then
                printf '%sn' "${files[@]}" > /location/sub/int/large_file_audit.txt
                fi


                This won't gracefully handle filenames containing newlines - with newer versions of bash, you could make the find and mapfile null-delimited, but there's not much benefit if you're outputting them as a newline-delimited list anyhow.






                share|improve this answer













                Your test if [ "find /location/sub/int/ -size +1G" ] doesn't work the way you intend because it tests the non-emptiness of the string "find /location/sub/int/ -size +1G" - which will always be true. In any case, the redirection > /location/sub/int/large_file_audit.txt will not magically pick up the standard output of the preceding command, so will always create an empty file.



                Perhaps the closest to your intent in Bash would be to put the results of find into an array, and then test whether it has any elements:



                mapfile -t files < <(find /location/sub/int/ -size +1G)

                if (( ${#files[@] > 0 )); then
                printf '%sn' "${files[@]}" > /location/sub/int/large_file_audit.txt
                fi


                This won't gracefully handle filenames containing newlines - with newer versions of bash, you could make the find and mapfile null-delimited, but there's not much benefit if you're outputting them as a newline-delimited list anyhow.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Jan 18 at 23:51









                steeldriversteeldriver

                67.8k11110182




                67.8k11110182

























                    4














                    One liner workaround:



                    find -size +10G | grep ".*" > file.log || (rm file.log; echo "Can't find anything")


                    Or:



                    find -size +10G | grep ".*" > file.log || rm file.log




                    Note that find returns 1 (False) when files are not processed correctly for any reason, so I suggest using something like:



                    #!/bin/bash
                    RESULTS=$(find /path -size +1G)
                    if [ -n "$RESULTS" ];
                    then
                    echo "$RESULTS" > /path/file.log
                    fi


                    First run the find and put the results in a variable, then if the variable contained anything save that into a log file.






                    share|improve this answer





















                    • 2





                      While the answer is all correct, you might want to touch on why original if [ "find /location/sub/int/ -size +1G" ] does not work as intended ( and what it actually does instead of what user expects ).

                      – Sergiy Kolodyazhnyy
                      Jan 18 at 22:54






                    • 1





                      find returns 1 if I ask it to look somewhere that doesn't exist, but if the path exists but -size finds nothing, it does return 0. (and 1 is false).

                      – Xen2050
                      Jan 19 at 2:54











                    • @Xen2050 To be exact , find returns 1 for any case where directory entries were not processed correctly as stated in EXIT STATUS part of the manual, which includes permission denied error on subdirectories or parent directory (try with chmod -r or touch testdir/subdir/foobar2;chmod -x testdir ).

                      – Sergiy Kolodyazhnyy
                      Jan 19 at 4:40
















                    4














                    One liner workaround:



                    find -size +10G | grep ".*" > file.log || (rm file.log; echo "Can't find anything")


                    Or:



                    find -size +10G | grep ".*" > file.log || rm file.log




                    Note that find returns 1 (False) when files are not processed correctly for any reason, so I suggest using something like:



                    #!/bin/bash
                    RESULTS=$(find /path -size +1G)
                    if [ -n "$RESULTS" ];
                    then
                    echo "$RESULTS" > /path/file.log
                    fi


                    First run the find and put the results in a variable, then if the variable contained anything save that into a log file.






                    share|improve this answer





















                    • 2





                      While the answer is all correct, you might want to touch on why original if [ "find /location/sub/int/ -size +1G" ] does not work as intended ( and what it actually does instead of what user expects ).

                      – Sergiy Kolodyazhnyy
                      Jan 18 at 22:54






                    • 1





                      find returns 1 if I ask it to look somewhere that doesn't exist, but if the path exists but -size finds nothing, it does return 0. (and 1 is false).

                      – Xen2050
                      Jan 19 at 2:54











                    • @Xen2050 To be exact , find returns 1 for any case where directory entries were not processed correctly as stated in EXIT STATUS part of the manual, which includes permission denied error on subdirectories or parent directory (try with chmod -r or touch testdir/subdir/foobar2;chmod -x testdir ).

                      – Sergiy Kolodyazhnyy
                      Jan 19 at 4:40














                    4












                    4








                    4







                    One liner workaround:



                    find -size +10G | grep ".*" > file.log || (rm file.log; echo "Can't find anything")


                    Or:



                    find -size +10G | grep ".*" > file.log || rm file.log




                    Note that find returns 1 (False) when files are not processed correctly for any reason, so I suggest using something like:



                    #!/bin/bash
                    RESULTS=$(find /path -size +1G)
                    if [ -n "$RESULTS" ];
                    then
                    echo "$RESULTS" > /path/file.log
                    fi


                    First run the find and put the results in a variable, then if the variable contained anything save that into a log file.






                    share|improve this answer















                    One liner workaround:



                    find -size +10G | grep ".*" > file.log || (rm file.log; echo "Can't find anything")


                    Or:



                    find -size +10G | grep ".*" > file.log || rm file.log




                    Note that find returns 1 (False) when files are not processed correctly for any reason, so I suggest using something like:



                    #!/bin/bash
                    RESULTS=$(find /path -size +1G)
                    if [ -n "$RESULTS" ];
                    then
                    echo "$RESULTS" > /path/file.log
                    fi


                    First run the find and put the results in a variable, then if the variable contained anything save that into a log file.







                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited Jan 19 at 4:41









                    Sergiy Kolodyazhnyy

                    72.1k9148314




                    72.1k9148314










                    answered Jan 18 at 21:47









                    RavexinaRavexina

                    32.3k1483113




                    32.3k1483113








                    • 2





                      While the answer is all correct, you might want to touch on why original if [ "find /location/sub/int/ -size +1G" ] does not work as intended ( and what it actually does instead of what user expects ).

                      – Sergiy Kolodyazhnyy
                      Jan 18 at 22:54






                    • 1





                      find returns 1 if I ask it to look somewhere that doesn't exist, but if the path exists but -size finds nothing, it does return 0. (and 1 is false).

                      – Xen2050
                      Jan 19 at 2:54











                    • @Xen2050 To be exact , find returns 1 for any case where directory entries were not processed correctly as stated in EXIT STATUS part of the manual, which includes permission denied error on subdirectories or parent directory (try with chmod -r or touch testdir/subdir/foobar2;chmod -x testdir ).

                      – Sergiy Kolodyazhnyy
                      Jan 19 at 4:40














                    • 2





                      While the answer is all correct, you might want to touch on why original if [ "find /location/sub/int/ -size +1G" ] does not work as intended ( and what it actually does instead of what user expects ).

                      – Sergiy Kolodyazhnyy
                      Jan 18 at 22:54






                    • 1





                      find returns 1 if I ask it to look somewhere that doesn't exist, but if the path exists but -size finds nothing, it does return 0. (and 1 is false).

                      – Xen2050
                      Jan 19 at 2:54











                    • @Xen2050 To be exact , find returns 1 for any case where directory entries were not processed correctly as stated in EXIT STATUS part of the manual, which includes permission denied error on subdirectories or parent directory (try with chmod -r or touch testdir/subdir/foobar2;chmod -x testdir ).

                      – Sergiy Kolodyazhnyy
                      Jan 19 at 4:40








                    2




                    2





                    While the answer is all correct, you might want to touch on why original if [ "find /location/sub/int/ -size +1G" ] does not work as intended ( and what it actually does instead of what user expects ).

                    – Sergiy Kolodyazhnyy
                    Jan 18 at 22:54





                    While the answer is all correct, you might want to touch on why original if [ "find /location/sub/int/ -size +1G" ] does not work as intended ( and what it actually does instead of what user expects ).

                    – Sergiy Kolodyazhnyy
                    Jan 18 at 22:54




                    1




                    1





                    find returns 1 if I ask it to look somewhere that doesn't exist, but if the path exists but -size finds nothing, it does return 0. (and 1 is false).

                    – Xen2050
                    Jan 19 at 2:54





                    find returns 1 if I ask it to look somewhere that doesn't exist, but if the path exists but -size finds nothing, it does return 0. (and 1 is false).

                    – Xen2050
                    Jan 19 at 2:54













                    @Xen2050 To be exact , find returns 1 for any case where directory entries were not processed correctly as stated in EXIT STATUS part of the manual, which includes permission denied error on subdirectories or parent directory (try with chmod -r or touch testdir/subdir/foobar2;chmod -x testdir ).

                    – Sergiy Kolodyazhnyy
                    Jan 19 at 4:40





                    @Xen2050 To be exact , find returns 1 for any case where directory entries were not processed correctly as stated in EXIT STATUS part of the manual, which includes permission denied error on subdirectories or parent directory (try with chmod -r or touch testdir/subdir/foobar2;chmod -x testdir ).

                    – Sergiy Kolodyazhnyy
                    Jan 19 at 4:40


















                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Ask Ubuntu!


                    • 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%2faskubuntu.com%2fquestions%2f1110989%2fconditional-find-output-missing%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

                    Human spaceflight

                    Can not write log (Is /dev/pts mounted?) - openpty in Ubuntu-on-Windows?

                    張江高科駅