LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-05-2008, 03:52 AM   #1
vikas027
Senior Member
 
Registered: May 2007
Location: Sydney
Distribution: RHEL, CentOS, Ubuntu, Debian, OS X
Posts: 1,305

Rep: Reputation: 107Reputation: 107
Question 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
 
Old 06-05-2008, 03:58 AM   #2
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
Enter "help test" in bash. There are 19 more tests you can use a well.
 
Old 06-05-2008, 04:08 AM   #3
vikas027
Senior Member
 
Registered: May 2007
Location: Sydney
Distribution: RHEL, CentOS, Ubuntu, Debian, OS X
Posts: 1,305

Original Poster
Rep: Reputation: 107Reputation: 107
Smile

Thanks, didnt knew it was so simple.

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

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.
 
Old 06-05-2008, 04:11 AM   #4
vikas027
Senior Member
 
Registered: May 2007
Location: Sydney
Distribution: RHEL, CentOS, Ubuntu, Debian, OS X
Posts: 1,305

Original Poster
Rep: Reputation: 107Reputation: 107
Anyways, this is only for bash shell.

Do we have something universal for all shells ?

Regards,
Vikas
 
Old 06-05-2008, 04:21 AM   #5
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
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.
 
Old 06-05-2008, 04:45 AM   #6
vikas027
Senior Member
 
Registered: May 2007
Location: Sydney
Distribution: RHEL, CentOS, Ubuntu, Debian, OS X
Posts: 1,305

Original Poster
Rep: Reputation: 107Reputation: 107
Smile

Quote:
Originally Posted by jschiwal View Post
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 !
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Makefile:condition to check whether a file exists ? Ashok_mittal Linux - Newbie 4 12-06-2011 07:52 PM
Shell script problem. check file already exists sinister1 Linux - Server 8 11-20-2007 03:13 PM
ksh check if file exists (using wildcard) problem r18044 Linux - Newbie 5 02-22-2005 07:52 AM
SIOCADDRT: File exists SIOCCADDRT: File Exists Failed to bring up eth0. opsraja Linux - Networking 0 01-10-2005 08:29 AM
c++ check if file exists Genjix Programming 3 03-15-2004 12:08 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:52 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration