Home > Back-end >  How to check if a file is present in a particular directory in Unix?
How to check if a file is present in a particular directory in Unix?

Time:11-26

I am new to Unix and I want to know how to check if a file is present in a particular directory in Unix? Please help me through this.

Thank you.

CodePudding user response:

We can make use of [ls] and [$?] command to do this. Use below command :

ls -l filename.txt

Followed by,

echo $?

If the file exists, the [ls] command will be successful.Hence [echo $?] will print 0.
If the file does not exists, then [ls] command will fail and hence [echo $?] will print 1.

  •  Tags:  
  • unix
  • Related