I this case your -f may work:
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...)
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:
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:
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:
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:
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:
which would be a legal construct. If there are several files matching the expression "AUS*" it would be expanded to:
and this is not legal at all! What is "test" (or "[") supposed to do with the other filenames following the first?
Moderator's Comments:
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):
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:
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)
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)
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)
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)
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)
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)
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)
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)
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)