Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to check if a file is present in a particular directory in Unix?

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading