File exist for multiple files


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting File exist for multiple files
# 1  
Old 05-22-2014
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

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

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

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

# 2  
Old 05-22-2014
try
Code:
cd $direct
if [ -f aus* ];
then
echo "File present"
else
echo "File not present"
fi

# 3  
Old 05-22-2014
What OS and shell are you using?
Because this syntax seem unorthodox to me:
Code:
[-f `ls $Direct/{$file}*|wc -l ` -eq 0]

# 4  
Old 05-22-2014
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
# 5  
Old 05-22-2014
Code:
 if [ -f ...

You need a space after the square bracket...
# 6  
Old 05-22-2014
even
Code:
if [ -f $Direct/AUS* ];

is working
# 7  
Old 05-22-2014
Quote:
Originally Posted by Makarand Dodmis
try
Code:
cd $direct
if [ -f aus* ];
then
echo "File present"
else
echo "File not present"
fi

-f does not work that way. You cannot cram more than one file into an -f.
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