File exist for multiple files

Hi,

I am unable to achieve the file exist conditions if there are multiple files same similar name for e.g. in a $Direct i am having files like
aus.txt
aus.txt_pr
aus.txt_2012

i need to put a file exist condition like which is not working

[-f $Direct/{$file}* ]
then
echo "File present"

but the below one worked but is there any way to check in -f condition itself

[-f `ls $Direct/{$file}*|wc -l ` -eq 0]
then
echo "File present"

try

cd $direct
if [ -f aus* ];
then
echo "File present"
else
echo "File not present"
fi

What OS and shell are you using?
Because this syntax seem unorthodox to me:

[-f `ls $Direct/{$file}*|wc -l ` -eq 0]

I don't want to put cd $Direct becoz there are multiple test conditions so i would like to check the files by invoking the directory like $direct/aus*

---------- Post updated at 10:40 AM ---------- Previous update was at 10:39 AM ----------

I am using Sun OS

 if [ -f ...

You need a space after the square bracket...

even

if [ -f $Direct/AUS* ];

is working

-f does not work that way. You cannot cram more than one file into an -f.

Yeah corona i agree with you -f will work for single file so is there any possible way to use with -f alone

I this case your -f may work:

if [ -f $Direct/$file* ]
then
   echo "File present"
else
   echo "File  not present"
fi

But it must be clear in your mind that the test tested ONE file that matched...
It will understand the variable $file* as one filename

So trust Corona688, if this happens to work, it only because it tested one file! And so will not solve your problem if you want to test for multiple files, it is more a question of what you are trying to achive Here the presence of one file suffice to fullfill the condition, therefore you will never know if you had more than one, I would answer like Corona688:
For multiple files file test operators are not to be used( unless of course you use a loop...)

1 Like

I suggest you start over learning shell basics. What you write here reveals a deep non-understanding of how the expansion of file names (aka "file globbing") in the shell works. You have fallen into the same pit legions of MS/DOS beginners trying to make the move to SCO / 386/ix / Linux did before you.

It is like that: if you write a file glob into a command:

ls -l file*ext

in DOS and similar operating systems the shell ("comand.com") would have passed "file*ext" as the argument to a (in DOS hypthetical) "ls"-command and this would have tried to figure out what to do with it. Not so in (any) Unix! There, the shell itself would expand the glob to a list of file names, for example:

ls -l fileA.ext fileB.ext fileC.ext

This list will then be fed to the "ls" command which in turn will do nothing more than to work off the already expanded list one filename after the other. You can even try:

echo *

and use a simple "echo" as a primitive "ls"-replacement because the shell will do its expansion regardless of what command the resulting list will later be fed to.

Now in light of this:

if [ -f AUS* ] ; then ...

regardless of changing to the directory before or using the complete path in the expression instead, will work if AND ONLY IF the file glob will match a single file! In this case it would be expanded into:

if [ -f AUSsomething.ext ] ; then ...

which would be a legal construct. If there are several files matching the expression "AUS*" it would be expanded to:

if [ -f AUSthis AUSthat AUSother ] ; then ...

and this is not legal at all! What is "test" (or "[") supposed to do with the other filenames following the first?

Now to the threads original problem:

you can use a loop where you test one file after the other (or even just the first, if you do not care about how many files there are):

typeset -i fileexist=0
typeset    file=""

ls -d "${directory}"/"${pattern}"* |\
while read file ; do
     if [ -f "$file" ] ; then
          fileexist=1
          # uncomment the following line to test for at least one file only
          # break
     fi
done

if [ $fileexist -eq 1 ] ; then
     print - "at least one file exists"
else
     print - "no such file"
fi

The "ls -d" prevents the ls from traversing the next level of subdirectories if there is a directory matching your glob too.

I hope this helps.

bakunin

6 Likes