check if two files exists at the same time in one location


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers check if two files exists at the same time in one location
# 1  
Old 01-27-2012
check if two files exists at the same time in one location

Hi friends,

I am trying to check if the two files i am expecting are created in a specific location.

if both files does not exist, do an echo
Code:
if [ [ ! -f out1.txt ] -a [ ! -f out2.txt ] ]; then
   echo "files not found"
fi

It gives me the following message:
Code:
bash: [: too many arguments

Please help! Smilie

Last edited by kokoro; 01-27-2012 at 01:13 PM.. Reason: add details
# 2  
Old 01-27-2012
Code:
if [ ! -f run ] && [ ! -f run.cpp ]
then
  echo "Files not found!"
fi

--ahamed
# 3  
Old 01-27-2012
Take a look at the following

In the following, you can see with the -e (exists) option.
1st passes, 2nd fails.

Code:
$ ls m*.txt
m12.txt  m45.txt  m45a.txt

$ if [ -e m45.txt -a -e m12.txt ] ; then echo "yes" ; else echo "no"; fi
yes

$ if [ -e m45.txt -a -e m13.txt ] ; then echo "yes" ; else echo "no"; fi
no

# 4  
Old 01-27-2012
I dont think you need the inner [ & ], so:-
Code:
if [ ! -f out1.txt -a ! -f out2.txt ]; then
   echo "both files not found"
fi

.....unless BASH is very different to sh, ksh etc. It is one if statement with an and in it. You might want to carefully consider your logic though. Here, you are saying if out1.txt and out2.txt both do not exist, then display the message. If one exists, you would not get the message. Is that what you want?

If you are checking that both should exist, then you could consider:-
  • If out1.txt does not exist or out2.txt does not exist, then error
  • If out1.txt and out2.txt both exist, continue, else error
These might be better. It just depends what you want to acheive.



I hope that this helps,

Robin
Liverpool/Blackburn
UK

Last edited by rbatte1; 01-27-2012 at 01:30 PM.. Reason: Highlighting
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to check more than 1 file specified files exists?

Hi all, One of my script crated created 2 files in a dirs Output.log and Output.tmp. Now in another script i need to check if both of the above mentioned files are present in a directory or not. I know to check one file but need to check both the files. Anyone could please tell me how... (3 Replies)
Discussion started by: girijajoshi
3 Replies

2. Post Here to Contact Site Administrators and Moderators

How to count successfully copy files source to target location with check directory in Linux?

Hi guys...please any one help me .... how to copy files from source to target location if 5 files copied successfully out of 10 files then implement success=10 and if remaining 5 files not copied successfully then count error=5 how to implement this condition with in loop i need code linux... (0 Replies)
Discussion started by: sravanreddy
0 Replies

3. Shell Programming and Scripting

File exists, but cannot be opened.How to check- whether it could be opened to read when it exists

Hi #Testing for file existence if ; then echo 'SCHOOL data is available for processing' else echo 'SCHOOL DATA IS NOT AVAILABLE FOR PROCESSING' : i wrote a script, where it begins by checking if file exists or not. If it exists, it truncates the database... (2 Replies)
Discussion started by: rxg
2 Replies

4. Shell Programming and Scripting

check if files exists

i writing a program that checks if any .txt files exist in the current directory if it does, then it lists the files... i have got everything right, except the validation part doesnt work! .... if then ls *.txt else echo "file not found" everytime it tells me file not found!! by... (3 Replies)
Discussion started by: bshell_1214
3 Replies

5. Shell Programming and Scripting

shell script to replicate the log files from one location to another in real time

Hi, On the server, we have app log files in this location /app/logs/error.log On the same server, in a real time, we would like to replicate that into /var/ directory. if someone has already done this, please share the script. Thanks in advance. (4 Replies)
Discussion started by: lookinginfo
4 Replies

6. Shell Programming and Scripting

to check files updation in sys time

Dear All, Pls help me on this issue. i want to write a script to check whether files updation happening in cuttent time or not. i have set of files in directory which wil update in time basis.. Requirement: If the files are updating in system time i just want to print "files are... (6 Replies)
Discussion started by: steve2216
6 Replies

7. Shell Programming and Scripting

Copy Files to Dir and Check If File Exists

Hi everyone. I am trying to write a bash script that will copy files from one directory to another but I need to be able to check the directory that I'm copying the files to and see if the file already exists. If it does I need to add a number at the end of the copied file. Thanks for your help. (3 Replies)
Discussion started by: snag49ers
3 Replies

8. Shell Programming and Scripting

Shell Script for Copy files from one location to another location

Create a script that copies files from one specified directory to another specified directory, in the order they were created in the original directory between specified times. Copy the files at a specified interval. (2 Replies)
Discussion started by: allways4u21
2 Replies

9. Shell Programming and Scripting

HOW TO CHECK ONLY .C FILES EXISTS OR NOT IN A FOLDER using IF in C shell script?

Hi friends.. I hav a problem.... I dont know how to check .c files exists r not in a folder using IF in C shell script actually i tried like this if(=~ *.c) even though some .c files or there in the current folder..it is not entering int o the if control statement...... (17 Replies)
Discussion started by: p.hemadrireddy
17 Replies

10. UNIX for Advanced & Expert Users

copy files from one location to similar location

I need help in forming a script to copy files from one location which has a sub directory structure to another location with similar sub directory structure, say location 1, /home/rick/tmp_files/1-12/00-25/ here 1-12 are the number of sub directories under tmp_files and 00-25 are sub... (1 Reply)
Discussion started by: pharos467
1 Replies
Login or Register to Ask a Question