Find the answer to your Linux question:
Results 1 to 5 of 5
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1

    Checking to see if there are files in a directory


    I'm trying to convert a script from Unix over to SuSE Linux. Most of it is ok, but the following is giving me problems

    Code:
    #!/bin/bash
    if [ -f /apps/v1.0/import/* ]
    then
      #Do something
    else
      #Do something else
    fi
    Basically when I run it I get a binary operator expected error if there is more than one file in the directory.

    So could someone recomend a way to test if there are files in a directory.

    Thanks

  2. #2
    Linux Guru lakerdonald's Avatar
    Join Date
    Jun 2004
    Location
    St. Petersburg, FL
    Posts
    5,035
    You could try this:
    Code:
    if [ `ls | wc -w` -ge 0 ]; then
    do something
    fi

  3. #3

    Re: Checking to see if there are files in a directory

    Quote Originally Posted by Monday
    I'm trying to convert a script from Unix over to SuSE Linux.
    How did the original script do it ?

  4. $spacer_open
    $spacer_close
  5. #4
    Linux Engineer
    Join Date
    Mar 2005
    Posts
    1,431
    And which language was the unix script written in? Maybe you can use the same on linux?

  6. #5
    change the if statement to use test instead of square brackets, then the presence of additional files will be ignored:
    Code:
    if test -f /apps/v1.0/import/*
    The original script is badly coded, BTW, because it won't work on UNIX either if more than one file is present.

Posting Permissions

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