Results 1 to 5 of 5

Thread: Unix: Check if a file exists with certain prefix

  1. #1
    Join Date
    Mar 2010
    Posts
    38

    Unix: Check if a file exists with certain prefix

    I have a shell script which is failing. I am on Unix right now. I need your help in this matter. My code is all about looking for files on a directory which is having a specific prefix. Usually all of the files have the prefix of "s" such as sMusic, sDocument, etc. My code is as follows:

    if [file exists with prefix s*] then
    perform operation1
    else
    perform operation2
    fi
    and my script is as follows:

    if [-e s*]; then
    operation1
    else
    operation2
    fi

    But the problem is that although you will find a lot of folders/files containing prefix of "s", the above code still fails and directly goes to the ELSE part. Need your help urgently!

  2. #2
    Join Date
    May 2008
    Posts
    2,297

    Re: Unix: Check if a file exists with certain prefix

    There are certain points that I would like to say you. First of all there should be a space in between "if [-e s*]" statement like this "if [ -e s* ]". Another thing that I would like to say here is that you should be careful when dealing with scripts. You explicitly use cd(1) to the directory for the directory on which you are using it. One more thing, if there is no file that is having "s" prefix then you may have the script that results you badly.

  3. #3
    Join Date
    Mar 2010
    Posts
    38

    Re: Unix: Check if a file exists with certain prefix

    Ok so I changed my code as you said. I thought of using "-f" switch. But the problem is that the exact filename is only returned with this. My new code looks like this
    if [ -f "${filePath}/s*" ]; then
    etc etc...

  4. #4
    Join Date
    Apr 2008
    Posts
    2,005

    Re: Unix: Check if a file exists with certain prefix

    That is because of the way scripts usually performs for pathname expansion. You have to let the expansion to occur at the right time.

    shopt -s nullglob
    i=(s*)
    if [ -n "$i" ]; then
    echo Condition passed
    else
    echo Condition failed
    fi

  5. #5
    Join Date
    May 2008
    Posts
    2,297

    Re: Unix: Check if a file exists with certain prefix

    The result completely depends on how you code it. You can code it like to expand it and iterate it as you want.

    #!/bin/sh
    cd /var/tmp || exit 255
    for x in s*; do
    echo "The file is $x"
    done

    This is obviously different from what you want to find the availability of the file, but instead it simply checks the presence of the file. So code it like this:

    #!/bin/sh
    cd /var/tmp || exit 255
    for x in s*; do
    echo operation1
    exit 0
    done

Similar Threads

  1. Check if URL exists (Java)
    By Kelvin Little in forum Software Development
    Replies: 7
    Last Post: 08-01-2010, 04:04 PM
  2. How to check if file exists in directory with Php
    By Zool in forum Software Development
    Replies: 3
    Last Post: 03-11-2009, 12:36 PM
  3. How to check if variable exists in C#
    By Hamlet in forum Software Development
    Replies: 3
    Last Post: 28-08-2009, 07:30 PM
  4. C++ code to check or search if a file exists or not?
    By RadhaV in forum Software Development
    Replies: 4
    Last Post: 17-02-2009, 11:33 PM
  5. How to Check File Exists or Not in Linux
    By Ettan in forum Software Development
    Replies: 0
    Last Post: 19-12-2008, 01:42 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,446,014.83054 seconds with 17 queries