check if files exists


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting check if files exists
# 1  
Old 09-21-2011
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 [ -e .txt ]
then
ls *.txt
else
echo "file not found"

everytime it tells me file not found!!
by the way, it is bourne shell!!

thanks
# 2  
Old 09-21-2011
Code:
 
bash-3.00$ ls *.abc > /dev/null 2>&1 && (echo ".txt files are there";ls *.txt) || echo "No files"                                                           
No files
bash-3.00$ ls *.txt > /dev/null 2>&1 && (echo ".txt files are there";ls *.txt) || echo "No files"                                                           
.txt files are there
new.txt      old.txt

# 3  
Old 09-21-2011
Quote:
Originally Posted by itkamaraj
Code:
 
bash-3.00$ ls *.abc > /dev/null 2>&1 && (echo ".txt files are there";ls *.txt) || echo "No files"                                                           
No files
bash-3.00$ ls *.txt > /dev/null 2>&1 && (echo ".txt files are there";ls *.txt) || echo "No files"                                                           
.txt files are there
new.txt      old.txt

thanks, but is it bourne shell? i am only new to shell scripting!!! please explain!!

thanks
# 4  
Old 09-21-2011
Code:
 
ls *.abc > /dev/null 2>&1  ##redirect the output and error message to /dev/null
 
&& (echo ".txt files are there";ls *.txt) ##this block only executes when the first command (ls) executed successfully (exit code:0)
 
|| echo "No files"  ## will execute if the ls command is not successful ( means, exit code is not zero)

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 file exists

I need to check whether a file exists and has been changed. The file should contain a specific string. The file should also have been changed within the last ten seconds. How do I do that? (3 Replies)
Discussion started by: locoroco
3 Replies

2. 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

3. Shell Programming and Scripting

Check if PID exists

In a Shell Script what is the most generic way to find in the PID exists or not. If it does not exist how can I echo the user "PID does not exist" & terminate the unix script ? If the command can work on most flavors of operating system the more useful I will find it to be. Current system... (16 Replies)
Discussion started by: mohtashims
16 Replies

4. 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

5. UNIX for Dummies Questions & Answers

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 if -a ]; then echo "files not found" fi It gives me the following message: bash: Please help! :) (3 Replies)
Discussion started by: kokoro
3 Replies

6. Shell Programming and Scripting

how to check to see if a file exists?

I want to write a script to see if various files exist. What I want to do is have the script search in various directories if a file exist, and if not, then output something like "/path/file does not exist". I don't actually know of how to check and see if a file exists or not. What I have in mind... (2 Replies)
Discussion started by: astropi
2 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

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

9. Shell Programming and Scripting

How to check if database exists?

Hi folks! First off I'm working with a Sybase DB. I'm using you're basic ISQL command to connect to my Sybase DB... isql -S$DB_SERVER -D$DB_NAME -U$DB_USR -P$DB_PWD <<!EOF > $log_file My question is, is there a way to determine if a database exists using shell script? For example, if... (2 Replies)
Discussion started by: Fatbob
2 Replies

10. Shell Programming and Scripting

How to check if a direcorty exists?

Hi Good people :D How do I check if a directory exists, if it does then carry on rest of the script, otherwise exit. ------------- cd $mainfolder/system1 #unzips files arrived in last 24 hrs into temp directory find * -mmin -1440 -exec unzip {} \; I'd like to check here if temp... (2 Replies)
Discussion started by: SunnyK
2 Replies
Login or Register to Ask a Question