File exist for multiple files


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting File exist for multiple files
# 8  
Old 05-22-2014
Yeah corona i agree with you -f will work for single file so is there any possible way to use with -f alone
# 9  
Old 05-22-2014
I this case your -f may work:
Code:
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...)

Last edited by vbe; 05-22-2014 at 01:11 PM..
This User Gave Thanks to vbe For This Post:
# 10  
Old 05-22-2014
Quote:
Originally Posted by Makarand Dodmis
even
Code:
if [ -f $Direct/AUS* ];

is working
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:

Code:
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:

Code:
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:

Code:
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:

Code:
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:

Code:
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:

Code:
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?

Moderator's Comments:
Mod Comment Don't get me wrong: it is not shameful not to know these things. It is perfectly OK to need to get it explained. But it is shameful to constantly offer advice about things you evidently do not know yourself and - because not always one of us moderators might be there to correct what you say - it is potentially harmful to all the people who come here in the hope of being helped. They do not have the expertise to understand that you wrong them by giving them bad (and often dangerous) advice.

I have told you already that i won't tolerate this and i sent you a PM about it to make sure you read it.

I am true to my word: you get a one-week pause to think things over and to come to a conclusion about how you want to participate here. Use the time carefully: the next time i revoke your posting rights it will be forever.


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):

Code:
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

Last edited by bakunin; 05-22-2014 at 04:21 PM..
These 6 Users Gave Thanks to bakunin For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Check if 10 files exist

Hi All, Whenever i get 10 files(file names like sales*) then another file need to create. May i know how to implement this in KSH. (4 Replies)
Discussion started by: siddireddy
4 Replies

2. Shell Programming and Scripting

Shell script to check files if exist else touch the file

Hi All, Thanks in Advance I wrote the following code if then echo "version is 1.1" for i in "subscriber promplan mapping dedicatedaccount faflistSub faflistAcc accumulator pam_account" do FILE="SDP_DUMP_$i.csv" echo "$FILE" ... (5 Replies)
Discussion started by: aealexanderraj
5 Replies

3. UNIX for Dummies Questions & Answers

Checking if multiple directories exist

I need to create multiple directories if those directories do not exist already. How would you go by doing this. What I have so far. array=(one two three) for I in ${array} do if ] then mkdir ${I} fi doneI have a good feeling this is done incorrectly. The error I am... (2 Replies)
Discussion started by: jrymer
2 Replies

4. Shell Programming and Scripting

Code to remove files when corresponding file doesnt exist isnt working.

I am trying to add some code to the begging of a script so that it will remove all the .transcript files, when their is no coressponding .wav file. But it doesnt work. This is the code I have added: for transcriptfile in `$voicemaildir/*.transcript`; do wavfile=`echo $transcriptfile | cut -d'.'... (2 Replies)
Discussion started by: ghurty
2 Replies

5. Shell Programming and Scripting

check if multiple folders exist

I want to check if some directories with common prefix exist under current directory with bash, say, I have dictories like: dirct_1 dirct_2 dirct_3 ... in the current directory. I did: if then echo " directories exist " else echo " directories not exist " fi (3 Replies)
Discussion started by: cristalp
3 Replies

6. Shell Programming and Scripting

Find out whether files exist.

I have the following data stored in a file. 1 /home/file13 /home/file2 2 /home/file41 /home/file654 3 /home/file61 /home/file45 4 /home/file81 /home/file43 ... I want to print the first column provided the files represented by the second and third column exist. How to do that? (3 Replies)
Discussion started by: kevintse
3 Replies

7. UNIX for Dummies Questions & Answers

copy files as space exist in file name..

Hi, i am having a directory in which files are having space in the name . $ls -1 aa b.txt my file.pdf lost file.csv foo_file.txti want to copy those file to some where with date +%F as extension . But it failed for the file having space. #!/bin/sh ls -1 >tt for var in `cat tt` do b=$var... (2 Replies)
Discussion started by: posix
2 Replies

8. Shell Programming and Scripting

Delete files if they exist

In a directory a number of files named res0.om res1.om ... resN.om where N can be any unknown number between 1 and 999 Please help me filling out the gaps in the following csh script: I need to delete all files exept res0.om The easy way is rm res1* rm res2* rm res3* rm res4*... (5 Replies)
Discussion started by: pederlol
5 Replies

9. Shell Programming and Scripting

How to check a file exist and do a copy of other files

Hi, I would like to perform bash which would check the file A.txt to be size 0 or not. If the size is 0, I would copy file B.txt to replace A.txt. Please help. Thanks. -Jason (6 Replies)
Discussion started by: ahjiefreak
6 Replies

10. Shell Programming and Scripting

Compare data in 2 files and delete if file exist

Hi there, I have written a script called "compare" (see below) to make comparison between 2 files namely test_put.log and Output_A0.log #!/bin/ksh while read file do found="no" while read line do echo $line | grep $file > /dev/null if then echo $file found found="yes" break fi... (3 Replies)
Discussion started by: lweegp
3 Replies
Login or Register to Ask a Question