Checking the existance of multiple files


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Checking the existance of multiple files
# 8  
Old 10-14-2010
Try, instead "-e", use "-f".

Regards!
# 9  
Old 10-14-2010
Quote:
Originally Posted by vivek_damodaran
Guys - I am looking for a one line command, if possible please
Code:
exists(){ [ -e "$1" ];};if exists /path/filename.*; then echo exists; else echo nope; fi

Smilie
# 10  
Old 10-14-2010
I tried running the script with -x option and got the below message.

ksh -x script.ksh

+ [ -e /path/filename1.20101001_18_18_40.tar.gz /path/filename1.20100930_18_18_40.tar.gz /path/filename1.20101002_18_18_40.tar.gz ]
script.ksh[3]: [: /path/filename1.20100930_18_18_40.tar.gz: unexpected operator/operand


So it's becuase of its finding multiple files
# 11  
Old 10-14-2010
This above script from post #1 appears to work in a Posix Shell.
Code:
if [ -e /path/filename.* ]
then
        <do some work> 
else
        <no files>
fi


The script from post #5 is an improvement on the one in post #4 because it does not fail in the "ls" when there are no files.

However the script does not work if there are space characters in filenames and more importantly does not give the desired result. We need to put quotes round the filename and exit the do-done loop when we find the first file.
Strangely the script works when there are no files because it fails to expand the pattern and pases "/path/filename*" as the sole paramter to the "test -e" statement which then does not find the file.
Overall it is actually no better than the script in post #1 because it relies on "test -e" which is what I suspect is the actual problem.

Code:
for i in /path/filename.*
do
	if [ -e "$i" ]
	then
		echo "File Exists"
                          break
	else
		echo "Fail"
	fi
done



---------- Post updated at 12:25 ---------- Previous update was at 12:17 ----------

Posts crossing in the post here.

I agree totally with post #7 . Use "-f" not "-e" . The "-e" option does not work in "ksh".
# 12  
Old 10-14-2010
I do not think -e has anything to do with it. -e should work in ksh. The problem is simply that wild card expansion is done by the shell before the list is past to test -e. This command only expects a single parameter. Since we only need to test if one of the files exist, we can cut of the rest of the list. This can be accomplished by either only using "$1" inside a function or looping and breaking. If there are no files then the * simply does not get expanded which is no problem for test -e.

Last edited by Scrutinizer; 10-14-2010 at 08:35 AM..
# 13  
Old 10-14-2010
And a one-liner.

Code:
[ -f /path/filename* ] && echo "File Exists" || echo "Fail"



---------- Post updated at 12:35 ---------- Previous update was at 12:31 ----------

Quote:
I do not think -e has anything to do with it. -e should work in ksh.
It works in Posix shells but gives a syntax error in normal ksh88.


Eample session in ksh88.

Code:
ksh
# touch abc
# if [ -f abc ]
> then
> echo "ok"
> fi
ok
# if [ -e abc ]
> then
> echo "ok"
> fi
ksh: test: argument expected

# 14  
Old 10-14-2010
Maybe are you using: PD KSH?

This will probably work:
Code:
# ls -1 f*
f
ff
fff
ffff
fffff

###########################
for mFile in f*
do
	[ -e ${mFile} ] && echo "File: [${mFile}] Exists." || echo "Fail: [${mFile}]."
done

I tried this in my Sun environment, and it worked:
Code:
if [ -e f* ]; then echo "Exists"; else echo "Not Exists"; fi
Exists

My Sun environment has this version: KornShell Software

Regards!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Merge multiple tab delimited files with index checking

Hello, I have 40 data files where the first three columns are the same (in theory) and the 4th column is different. Here is an example of three files, file 2: A_f0_r179_pred.txt Id Group Name E0 1 V N(,)'1 0.2904 2 V N(,)'2 0.3180 3 V N(,)'3 0.3277 4 V N(,)'4 0.3675 5 V N(,)'5 0.3456 ... (8 Replies)
Discussion started by: LMHmedchem
8 Replies

2. Shell Programming and Scripting

CSV joining and checking multiple files

Hello, For our work we use several scripts to gather/combine data for use in our webshop. Untill now we did not had any problems but since a couple days we noticed some mismatches between imports. It happened that several barcodes where matched even though it was a complete other product. Of... (19 Replies)
Discussion started by: SDohmen
19 Replies

3. Shell Programming and Scripting

Checking File record equal to multiple of 70 or nearest number to multiple of 70

Hello, I have a file with below content - Example 3 6 69 139 210 345 395 418 490 492 I would like the result as - Multiple of 70 or nearest number in the file less than the multiple of 70 69 139 (5 Replies)
Discussion started by: Mannu2525
5 Replies

4. Shell Programming and Scripting

Checking Multiple File existance in a UNIX folder(Note: File names are all different)

HI Guys, I have some 8 files with different name and extensions. I need to check if they are present in a specific folder or not and also want that script to show me which all are not present. I can write if condition for each file but from a developer perspective , i feel that is not a good... (3 Replies)
Discussion started by: shankarpanda003
3 Replies

5. Shell Programming and Scripting

Record count checking for multiple files through for-loop

Hi Friends, I wrote one shell script to check the record count in two files and that will send us the notification activity if found zero record count. What i did is I created for loop and checking the count for both of the files but what is happening is for first file has data then it's... (13 Replies)
Discussion started by: victory
13 Replies

6. Shell Programming and Scripting

Error while checking file existance

Kindly help on below script: << i='find ...' if then echo 'File Exists' else echo 'File Does Not Exist' >> If i has some file name then it runs properly but if i has nothing ( blank value) then it throws an error. I dont exactly remember right now but error seems like:... (2 Replies)
Discussion started by: ravigupta2u
2 Replies

7. Shell Programming and Scripting

how to check existance of multiple directories

Hi, I would like to check whether all the directories exists or not. I tried the below but it gives some error. below is the excerpt from my original script 24 #Check if the required directories are exists 25 dirExists() { 26 27 if 28 then 29 echo "required... (1 Reply)
Discussion started by: lookinginfo
1 Replies

8. Shell Programming and Scripting

The checking of folder existance is not working...

Hi all, I have the following code: if ; then echo 'folder not exist'; else echo 'folder exist'; fi The "testing" folder is not exist in /home/batch , but thhe result is 'folder exist'. It seems that the code cannot detect that the folder "testing" not exist. ANybody know the... (1 Reply)
Discussion started by: suigion
1 Replies

9. Shell Programming and Scripting

checking existance of files and directories

I have a list of files and their directories, starting from a particular location. e.g. temp/new/a.c temp/new/b.c temp/old/a.c temp/old/older/b.c etc. Now I want to check the existence of all these files in the respective directories . I am trying something like cat "fileList" |... (4 Replies)
Discussion started by: herojeett
4 Replies

10. UNIX for Dummies Questions & Answers

Checking file existance by extension

I'm sure this can't be too tough, but. I want to check the existance of file(s) by extension. There may be 0,1,N files. If any files exist, then remove them. Thanks for any insight. (3 Replies)
Discussion started by: niswonp
3 Replies
Login or Register to Ask a Question