LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   check whether a file exists or not ?? (https://www.linuxquestions.org/questions/programming-9/check-whether-a-file-exists-or-not-647141/)

vikas027 06-05-2008 03:52 AM

check whether a file exists or not ??
 
Hi all,

Is there any simple command to find out whether file exists or not ?

Somewhat there is for checking a folder, for eg checking a folder /tmp/vikas
Code:

if [ -d /tmp/vikas/passwd/$a ];
then
echo "found"
else
echo "not found"
fi

Thanks
VIKAS

jschiwal 06-05-2008 03:58 AM

Enter "help test" in bash. There are 19 more tests you can use a well.

vikas027 06-05-2008 04:08 AM

Thanks, didnt knew it was so simple.

I was counting chars of output and deciding whether a file/folder exists or not. How stupid :eek:

Code:

bash # ls -l /etc/passwd | wc -c
54

bash # ls -l /etc/tmp | wc -c
ls: /etc/tmp: No such file or directory
0

If file exists output would be more than 0.

Didnt knew it was so simple.

Thanks a ton.

vikas027 06-05-2008 04:11 AM

Anyways, this is only for bash shell.

Do we have something universal for all shells ?

Regards,
Vikas

jschiwal 06-05-2008 04:21 AM

test also exists as an external command, probably in /usr/bin/test. It has a man page and info manual.

This is similar to "[" which is a built-in but also exists as a regular command, something required by the Posix standard.

vikas027 06-05-2008 04:45 AM

Quote:

Originally Posted by jschiwal (Post 3175333)
test also exists as an external command, probably in /usr/bin/test. It has a man page and info manual.

This is similar to "[" which is a built-in but also exists as a regular command, something required by the Posix standard.


Ok, I thought that it works on bash shell only. I have checked it on another shells also.

Thanks friend !


All times are GMT -5. The time now is 04:49 PM.