Check if there is a certain file in a directory

198

To check if a file file.txt is present in the directory passed as the first argument to the script or function, use

[ -e "$1/file.txt" ]

Don't forget to double-quote variable substitutions.

This succeeds if the file exists, and fails if the file doesn't exist. Failure includes the case where the file exists but can't be reached, e.g. because you don't have the permission to traverse the directory.

Note that in shell scripts, and when it comes to process exit statuses, 0 means success and 1 (or more, up to 125) means failure. See What return/exit values can I use in bash functions/scripts? and Default exit code when process is terminated? for more details. So if you want to check whether a file exists, your script or function must return 0 if the file exists and 1 otherwise. Your snippet does the opposite: it checks if the file doesn't exist.

 if [ -e "$1/file.txt" ]; then
    exit 0
 else
    exit 1
 fi

is just a more complicated way of writing

[ -e "$1/file.txt" ]
exit

(exit with no status argument uses the status of the previously executed command). And if this is at the end of the script then exit is redundant.

If you did want to check that the file doesn't exist then you would invert the command with the ! shell operator:

! [ -e "$1/file.txt" ]

or the ! test/[ operator:

[ ! -e "$1/file.txt" ]
Share:
198

Related videos on Youtube

huMpty duMpty
Author by

huMpty duMpty

Updated on September 18, 2022

Comments

  • huMpty duMpty
    huMpty duMpty almost 2 years

    I am using third party web service and trying to log the outgoing SOAP envelop when the web service method is called.

    Came across this article about How to capture SOAP envelopes when consuming a web service in ASP.NET. and implemented similar way. B

    But for some reason ProcessMessage is not get called.

    My class and namespace looks like below

    namespace MyServiceClient
    {
        public class MySeriviceSoap : SoapExtension
        {
         ......
    

    And my app.config

    <?xml version="1.0"?>
    <configuration>
      <startup>
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
      </startup>
    
      <system.web>
        <webServices>
          <soapExtensionTypes>
            <add type="MyServiceClient.MySeriviceSoap, MyServiceClient, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" priority="1" />
          </soapExtensionTypes>
        </webServices>
      </system.web>
    </configuration>
    

    Any help on this please

    • George Vasiliou
      George Vasiliou over 7 years
      stackoverflow.com/questions/638975/… correct switch is -f, not -e...
    • Gilles 'SO- stop being evil'
      Gilles 'SO- stop being evil' over 7 years
      @GeorgeVasiliou No, -e is the correct switch. -f tests whether file.txt exists and is a regular file. -e just tests if the file exists.
  • huMpty duMpty
    huMpty duMpty almost 9 years
    Great! Will try this out
  • huMpty duMpty
    huMpty duMpty almost 9 years
    Tried using fully qualified type name, but having the same result. I have updated the question with new code
  • CodeCaster
    CodeCaster almost 9 years
    Oh, this is for Web Services (ASMX)... chances are you have a WCF client. Update your question with what client you're using.
  • huMpty duMpty
    huMpty duMpty almost 9 years
    It something been already implemented using web service (wsdl)
  • CodeCaster
    CodeCaster almost 9 years
    You don't seem to understand what I'm asking. Is the client generated through ASMX (wsdl.exe, Add Web Service Reference) or through WCF (svcutil.exe, Add Service Reference)?
  • huMpty duMpty
    huMpty duMpty almost 9 years
    Oh sorry, its been added as "Add Web Service Reference"