One liner to check for file exists

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
2
down vote

favorite












Objective: Check for presence of backup .tgz file containing today's date; output 1 for OK, 0 for no file.



I'm a sucker for one liners :)
For example in PHP (and pretty much similar in Javascript), in various scenarios I like to do something like



<?php
echo (date("d")==1)?"Monday":"Not Monday";
?>


Is there similar syntax in Bash? I know how to check for presence of a regular file using -f FILENAME, I only want 1 or 0 returned from my oneliner :)










share|improve this question

















  • 1




    Possible duplicate of Shell script check if file exists?
    – mrc02_kr
    24 mins ago






  • 2




    Why isn't -f enough then? That already returns 0 or 1. Do you want the 0 or 1 to be printed or returned?
    – terdon♦
    20 mins ago














up vote
2
down vote

favorite












Objective: Check for presence of backup .tgz file containing today's date; output 1 for OK, 0 for no file.



I'm a sucker for one liners :)
For example in PHP (and pretty much similar in Javascript), in various scenarios I like to do something like



<?php
echo (date("d")==1)?"Monday":"Not Monday";
?>


Is there similar syntax in Bash? I know how to check for presence of a regular file using -f FILENAME, I only want 1 or 0 returned from my oneliner :)










share|improve this question

















  • 1




    Possible duplicate of Shell script check if file exists?
    – mrc02_kr
    24 mins ago






  • 2




    Why isn't -f enough then? That already returns 0 or 1. Do you want the 0 or 1 to be printed or returned?
    – terdon♦
    20 mins ago












up vote
2
down vote

favorite









up vote
2
down vote

favorite











Objective: Check for presence of backup .tgz file containing today's date; output 1 for OK, 0 for no file.



I'm a sucker for one liners :)
For example in PHP (and pretty much similar in Javascript), in various scenarios I like to do something like



<?php
echo (date("d")==1)?"Monday":"Not Monday";
?>


Is there similar syntax in Bash? I know how to check for presence of a regular file using -f FILENAME, I only want 1 or 0 returned from my oneliner :)










share|improve this question













Objective: Check for presence of backup .tgz file containing today's date; output 1 for OK, 0 for no file.



I'm a sucker for one liners :)
For example in PHP (and pretty much similar in Javascript), in various scenarios I like to do something like



<?php
echo (date("d")==1)?"Monday":"Not Monday";
?>


Is there similar syntax in Bash? I know how to check for presence of a regular file using -f FILENAME, I only want 1 or 0 returned from my oneliner :)







bash files






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked 36 mins ago









DavDav

375




375







  • 1




    Possible duplicate of Shell script check if file exists?
    – mrc02_kr
    24 mins ago






  • 2




    Why isn't -f enough then? That already returns 0 or 1. Do you want the 0 or 1 to be printed or returned?
    – terdon♦
    20 mins ago












  • 1




    Possible duplicate of Shell script check if file exists?
    – mrc02_kr
    24 mins ago






  • 2




    Why isn't -f enough then? That already returns 0 or 1. Do you want the 0 or 1 to be printed or returned?
    – terdon♦
    20 mins ago







1




1




Possible duplicate of Shell script check if file exists?
– mrc02_kr
24 mins ago




Possible duplicate of Shell script check if file exists?
– mrc02_kr
24 mins ago




2




2




Why isn't -f enough then? That already returns 0 or 1. Do you want the 0 or 1 to be printed or returned?
– terdon♦
20 mins ago




Why isn't -f enough then? That already returns 0 or 1. Do you want the 0 or 1 to be printed or returned?
– terdon♦
20 mins ago










4 Answers
4






active

oldest

votes

















up vote
6
down vote













You can simply do this :



#to check if it's a regular file
[ -f "/you/file.file" ] && echo 1 || echo 0
#to check if a file exist
[ -e "/you/file.file" ] && echo 1 || echo 0


In shell this mean [ test -e if file exists ] end of test && if command return true execute the command after, || if command return false execute command after.

This should work in shell and bash






share|improve this answer





























    up vote
    5
    down vote













    if [ ! -f /tmp/foo.txt ]; then echo "File not found!"; else echo "file found"; fi





    share|improve this answer


















    • 2




      @mrc02_kr the definition of a "one liner" is not very precise. The point is a simple, short command. That doesn't mean we need to put it all on one line, not when it is obviously a single command.
      – terdon♦
      22 mins ago

















    up vote
    2
    down vote













    You can use this command:



    test -e *$(date).tgz && echo 1 || echo 0





    share|improve this answer
















    • 3




      Note that this doesn't actually return 1 or 0. It prints 1 or 0 whihc is a different thing. If you want to return 1 or 0, just use the test -e part and forget the rest. The test command already returns 0 or 1 depending on its status.
      – terdon♦
      21 mins ago

















    up vote
    0
    down vote













    With zsh:



    ()echo $# *"$(date +%Y-%m-%d)"*.tgz(DN)


    Would output the number of files in the current directory whose name contains the current date in YYYY-mm-dd format and end in .tgz as a decimal number.



    To use as the condition in an if statement, you can do:



    if ()(($#)) *"$(date +%Y-%m-%d)"*.tgz(DN); then
    echo found
    else
    echo none found
    fi


    In bash, the equivalent could be:



    (shopt -s nullglob dotglob; set -- *"$(date +%Y-%m-%d)"*.tgz; echo "$#")


    and



    if (shopt -s nullglob dotglob; set -- *"$(date +%Y-%m-%d)"*.tgz; (($#))); then
    echo found
    else
    echo none found
    fi




    share




















      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%2f474244%2fone-liner-to-check-for-file-exists%23new-answer', 'question_page');

      );

      Post as a guest






























      4 Answers
      4






      active

      oldest

      votes








      4 Answers
      4






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes








      up vote
      6
      down vote













      You can simply do this :



      #to check if it's a regular file
      [ -f "/you/file.file" ] && echo 1 || echo 0
      #to check if a file exist
      [ -e "/you/file.file" ] && echo 1 || echo 0


      In shell this mean [ test -e if file exists ] end of test && if command return true execute the command after, || if command return false execute command after.

      This should work in shell and bash






      share|improve this answer


























        up vote
        6
        down vote













        You can simply do this :



        #to check if it's a regular file
        [ -f "/you/file.file" ] && echo 1 || echo 0
        #to check if a file exist
        [ -e "/you/file.file" ] && echo 1 || echo 0


        In shell this mean [ test -e if file exists ] end of test && if command return true execute the command after, || if command return false execute command after.

        This should work in shell and bash






        share|improve this answer
























          up vote
          6
          down vote










          up vote
          6
          down vote









          You can simply do this :



          #to check if it's a regular file
          [ -f "/you/file.file" ] && echo 1 || echo 0
          #to check if a file exist
          [ -e "/you/file.file" ] && echo 1 || echo 0


          In shell this mean [ test -e if file exists ] end of test && if command return true execute the command after, || if command return false execute command after.

          This should work in shell and bash






          share|improve this answer














          You can simply do this :



          #to check if it's a regular file
          [ -f "/you/file.file" ] && echo 1 || echo 0
          #to check if a file exist
          [ -e "/you/file.file" ] && echo 1 || echo 0


          In shell this mean [ test -e if file exists ] end of test && if command return true execute the command after, || if command return false execute command after.

          This should work in shell and bash







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited 18 mins ago









          terdon♦

          124k29234408




          124k29234408










          answered 30 mins ago









          Kiwy

          5,66153454




          5,66153454






















              up vote
              5
              down vote













              if [ ! -f /tmp/foo.txt ]; then echo "File not found!"; else echo "file found"; fi





              share|improve this answer


















              • 2




                @mrc02_kr the definition of a "one liner" is not very precise. The point is a simple, short command. That doesn't mean we need to put it all on one line, not when it is obviously a single command.
                – terdon♦
                22 mins ago














              up vote
              5
              down vote













              if [ ! -f /tmp/foo.txt ]; then echo "File not found!"; else echo "file found"; fi





              share|improve this answer


















              • 2




                @mrc02_kr the definition of a "one liner" is not very precise. The point is a simple, short command. That doesn't mean we need to put it all on one line, not when it is obviously a single command.
                – terdon♦
                22 mins ago












              up vote
              5
              down vote










              up vote
              5
              down vote









              if [ ! -f /tmp/foo.txt ]; then echo "File not found!"; else echo "file found"; fi





              share|improve this answer














              if [ ! -f /tmp/foo.txt ]; then echo "File not found!"; else echo "file found"; fi






              share|improve this answer














              share|improve this answer



              share|improve this answer








              edited 18 mins ago









              terdon♦

              124k29234408




              124k29234408










              answered 28 mins ago









              Goro

              7,83453472




              7,83453472







              • 2




                @mrc02_kr the definition of a "one liner" is not very precise. The point is a simple, short command. That doesn't mean we need to put it all on one line, not when it is obviously a single command.
                – terdon♦
                22 mins ago












              • 2




                @mrc02_kr the definition of a "one liner" is not very precise. The point is a simple, short command. That doesn't mean we need to put it all on one line, not when it is obviously a single command.
                – terdon♦
                22 mins ago







              2




              2




              @mrc02_kr the definition of a "one liner" is not very precise. The point is a simple, short command. That doesn't mean we need to put it all on one line, not when it is obviously a single command.
              – terdon♦
              22 mins ago




              @mrc02_kr the definition of a "one liner" is not very precise. The point is a simple, short command. That doesn't mean we need to put it all on one line, not when it is obviously a single command.
              – terdon♦
              22 mins ago










              up vote
              2
              down vote













              You can use this command:



              test -e *$(date).tgz && echo 1 || echo 0





              share|improve this answer
















              • 3




                Note that this doesn't actually return 1 or 0. It prints 1 or 0 whihc is a different thing. If you want to return 1 or 0, just use the test -e part and forget the rest. The test command already returns 0 or 1 depending on its status.
                – terdon♦
                21 mins ago














              up vote
              2
              down vote













              You can use this command:



              test -e *$(date).tgz && echo 1 || echo 0





              share|improve this answer
















              • 3




                Note that this doesn't actually return 1 or 0. It prints 1 or 0 whihc is a different thing. If you want to return 1 or 0, just use the test -e part and forget the rest. The test command already returns 0 or 1 depending on its status.
                – terdon♦
                21 mins ago












              up vote
              2
              down vote










              up vote
              2
              down vote









              You can use this command:



              test -e *$(date).tgz && echo 1 || echo 0





              share|improve this answer












              You can use this command:



              test -e *$(date).tgz && echo 1 || echo 0






              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered 30 mins ago









              mrc02_kr

              857319




              857319







              • 3




                Note that this doesn't actually return 1 or 0. It prints 1 or 0 whihc is a different thing. If you want to return 1 or 0, just use the test -e part and forget the rest. The test command already returns 0 or 1 depending on its status.
                – terdon♦
                21 mins ago












              • 3




                Note that this doesn't actually return 1 or 0. It prints 1 or 0 whihc is a different thing. If you want to return 1 or 0, just use the test -e part and forget the rest. The test command already returns 0 or 1 depending on its status.
                – terdon♦
                21 mins ago







              3




              3




              Note that this doesn't actually return 1 or 0. It prints 1 or 0 whihc is a different thing. If you want to return 1 or 0, just use the test -e part and forget the rest. The test command already returns 0 or 1 depending on its status.
              – terdon♦
              21 mins ago




              Note that this doesn't actually return 1 or 0. It prints 1 or 0 whihc is a different thing. If you want to return 1 or 0, just use the test -e part and forget the rest. The test command already returns 0 or 1 depending on its status.
              – terdon♦
              21 mins ago










              up vote
              0
              down vote













              With zsh:



              ()echo $# *"$(date +%Y-%m-%d)"*.tgz(DN)


              Would output the number of files in the current directory whose name contains the current date in YYYY-mm-dd format and end in .tgz as a decimal number.



              To use as the condition in an if statement, you can do:



              if ()(($#)) *"$(date +%Y-%m-%d)"*.tgz(DN); then
              echo found
              else
              echo none found
              fi


              In bash, the equivalent could be:



              (shopt -s nullglob dotglob; set -- *"$(date +%Y-%m-%d)"*.tgz; echo "$#")


              and



              if (shopt -s nullglob dotglob; set -- *"$(date +%Y-%m-%d)"*.tgz; (($#))); then
              echo found
              else
              echo none found
              fi




              share
























                up vote
                0
                down vote













                With zsh:



                ()echo $# *"$(date +%Y-%m-%d)"*.tgz(DN)


                Would output the number of files in the current directory whose name contains the current date in YYYY-mm-dd format and end in .tgz as a decimal number.



                To use as the condition in an if statement, you can do:



                if ()(($#)) *"$(date +%Y-%m-%d)"*.tgz(DN); then
                echo found
                else
                echo none found
                fi


                In bash, the equivalent could be:



                (shopt -s nullglob dotglob; set -- *"$(date +%Y-%m-%d)"*.tgz; echo "$#")


                and



                if (shopt -s nullglob dotglob; set -- *"$(date +%Y-%m-%d)"*.tgz; (($#))); then
                echo found
                else
                echo none found
                fi




                share






















                  up vote
                  0
                  down vote










                  up vote
                  0
                  down vote









                  With zsh:



                  ()echo $# *"$(date +%Y-%m-%d)"*.tgz(DN)


                  Would output the number of files in the current directory whose name contains the current date in YYYY-mm-dd format and end in .tgz as a decimal number.



                  To use as the condition in an if statement, you can do:



                  if ()(($#)) *"$(date +%Y-%m-%d)"*.tgz(DN); then
                  echo found
                  else
                  echo none found
                  fi


                  In bash, the equivalent could be:



                  (shopt -s nullglob dotglob; set -- *"$(date +%Y-%m-%d)"*.tgz; echo "$#")


                  and



                  if (shopt -s nullglob dotglob; set -- *"$(date +%Y-%m-%d)"*.tgz; (($#))); then
                  echo found
                  else
                  echo none found
                  fi




                  share












                  With zsh:



                  ()echo $# *"$(date +%Y-%m-%d)"*.tgz(DN)


                  Would output the number of files in the current directory whose name contains the current date in YYYY-mm-dd format and end in .tgz as a decimal number.



                  To use as the condition in an if statement, you can do:



                  if ()(($#)) *"$(date +%Y-%m-%d)"*.tgz(DN); then
                  echo found
                  else
                  echo none found
                  fi


                  In bash, the equivalent could be:



                  (shopt -s nullglob dotglob; set -- *"$(date +%Y-%m-%d)"*.tgz; echo "$#")


                  and



                  if (shopt -s nullglob dotglob; set -- *"$(date +%Y-%m-%d)"*.tgz; (($#))); then
                  echo found
                  else
                  echo none found
                  fi





                  share











                  share


                  share










                  answered 9 mins ago









                  Stéphane Chazelas

                  288k53532868




                  288k53532868



























                       

                      draft saved


                      draft discarded















































                       


                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function ()
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f474244%2fone-liner-to-check-for-file-exists%23new-answer', 'question_page');

                      );

                      Post as a guest













































































                      Comments

                      Popular posts from this blog

                      What does second last employer means? [closed]

                      List of Gilmore Girls characters

                      Confectionery