Find the answer to your Linux question:
Results 1 to 4 of 4
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 ...
  1. #1
    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

  2. #2
    Linux User
    Join Date
    Jun 2006
    Posts
    311
    Hi ancy,
    In your script, to see whether the file exits, you use
    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

  3. #3
    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

  4. #4
    Super Moderator devils casper's Avatar
    Join Date
    Jun 2006
    Location
    Chandigarh, India
    Posts
    24,308

    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 is amazing what you can accomplish if you do not care who gets the credit.
    New Users: Read This First

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •