Welcome to Linux Forums! With a comprehensive Linux Forum, information on various types of Linux software and many Linux Reviews articles, we have all the knowledge you need a click away, or accessible via our knowledgeable members.
Find the answer to your Linux question:
New to Linux Forums? Register here for free!
    Linux Forums > Your Distro > Redhat / Fedora Linux Help > To find file exists in current working directory bash script

Forgot Password?
 Redhat / Fedora Linux Help   Help and discussion related to Redhat and Fedora Linux.

Site Navigation
Linux Articles
Linux Forums
Linux Downloads
Linux Hosting
Free Magazines
Job Board
IRC Chat
RSS Feeds


Linux Forum Topics
Linux Forums
Your Distro
Linux Resources
GNU Linux Zone
The Community
Reply
 
Thread Tools Display Modes
Old 07-20-2006   #1 (permalink)
Just Joined!
 
Join Date: Jul 2006
Posts: 25
To find file exists in current working directory bash script

Hi ALL,

Can u help me out in solving this problem in bash script

My current working directory is ancy/sample

there are two file
ancy.sh ancy.txt

In ancy.sh script i need to check whether ancy.txt is available

I need to check whether the file exists in current working directory
ancy,txt is stored in a variable called filename

and i need to check whether its a readble file

Help me out in this
ancy is offline  


Reply With Quote
Old 07-20-2006   #2 (permalink)
Linux User
 
Join Date: Jun 2006
Posts: 311
Hi ancy,
In your script, to see whether the file exits, you use
Quote:
if [ ! -f "$filename" ]
then
echo "$filename does not exists in the current directory"
else
echo "$filename does exists in the current directory"
fi
To see whether a file is readable you use -r option like the way you can use -f for file existence check.

Read the following website to know the very basics of Bash scripting : -

http://www.phptr.com/articles/articl...&seqNum=4&rl=1

With Regards,
Thinker
Thinker is offline   Reply With Quote
Old 07-21-2006   #3 (permalink)
Just Joined!
 
Join Date: Jul 2006
Posts: 25
Hi,

I executed the above statemnet


but it shows an compilation error

if[: not found]

I need to check whether the file exists in current working directory using unix bash scripting
ancy is offline   Reply With Quote
Old 07-21-2006   #4 (permalink)
Super Moderator
 
devils casper's Avatar
 
Join Date: Jun 2006
Location: Chandigarh, India
Posts: 21,049
Send a message via Yahoo to devils casper
Smile

hi ancy !!!

Quote:
Originally Posted by ancy
if[: not found]
there must be a space between 'if' and '['

if [ ! -f "$filename" ]

take care of spaces....

<=== { casper } ===>
__________________
It takes a lot of time to be a genius, you have to sit around so much doing nothing, really doing nothing. - Gertrude Stein
New Users: Read This First
devils casper is offline   Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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 On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off

Free Magazines
Run Your Own Web Server Using Linux & Apache - Free 191 Page Preview
Learn about everything you'll need to build and maintain your Linux servers, and to deploy Web applications to them.
subscribe
Open Source Security Myths Dispelled
Dispel the five major myths surrounding Open Source Security and gain the tools necessary to make a truly informed decision for your IT organization
subscribe
InformationWeek
InformationWeek is the only newsweekly you'll need to stay on top of the latest developments in information technology.
subscribe



All times are GMT. The time now is 01:38 AM.






© 2000 - 2009 - All Rights Reserved - Property of  MAS Media

Content Relevant URLs by vBSEO 3.3.0 RC2