Linux test command

Hi,

I was trying to test the presence of some files on Red hat Linux enterprise version 2.4.9 using the following command

test -f /home/pv/T20*

I am getting the error, too many parameters, when I have multiple files starting with T20 on that directory. When there is no files or just one file, no errors and the command works.

Any idea whay I can't check the presence of multiple files? Or is there any other command I can use for this purpose.

Thanks in advance for any help.

You could try iterating through the files instead

for file in /home/pv/T20*
do
    if [ -f "$file" ]; then
        echo "File $file exists"
    fi
done

Is this what you want?

cheers
ZB

I just want to see(by a a single commad) if there is any file starting with those letters present in that directory so that I can do some processing on those files. test -f /home/pv/T20* command works perfect on HP-UX and I assume it is a bug on this flavor of Linux.

Indeed, ran also the test command on both OS'ses and came to the same result.

Maybe you are looking for the find command, and when a file is found you can use the -exec option.

Cheers.
Dieter

Another way to test for files

if ls /home/pv/T20* >/dev/null 2>&1
then
    echo true
fi

However, this would return true for directories and empty files. Since you want to do some processing on the files why not use a for loop...

for file in $(ls /home/pv/T20*)
do
   [ -s $file ] || continue
   echo do something with $file
   :
done

If there are no files then the code within the for loop is not executed and this message is sent to standard error: "/home/pv/T20* not found"

PS: It is not good practice to rely on "if test -f" for multiple files. Here's why (on HP-UX)....

$ ls *.txt
b.txt c.txt
$ if test -f *.txt
> then
> echo true
> else
> echo false
> fi
true
$ mkdir a.txt
$ if test -f *.txt
> then
> echo true
> else
> echo false
> fi
false