Find existence of file?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Find existence of file?
# 1  
Old 03-23-2012
Find existence of file?

Hi Guys,
I am using c shell script to find whether the file exist or not. I am using if statment to achieve this. I would like to tell the user which file doesn't exist and for this I would need your assistance. Example:

Code:
if (! -e filename1.txt || ! -e filename2.txt || ! -e filename3.txt) then
echo "One of the filename.txt is missing"
endif
 
O/p (let's say  filename2.txt is not that in directory)
One of the filename.txt is missing

Instead of this ouput is there a cool way of telling user precisely which filename is not present. If possible I wouldn't want to change if and endif structure.
Thanks in advance
# 2  
Old 04-01-2012
Code:
$ for i in file1 file2 file3; do [ ! -e "$i.txt" ] && echo $i.txt is missing; done

# 3  
Old 04-01-2012
Quote:
Originally Posted by dixits
I am using c shell script

That's a mistake:
Top Ten Reasons not to use the C shell
Csh problems
Csh Programming Considered Harmful
Quote:
to find whether the file exist or not. I am using if statment to achieve this. I would like to tell the user which file doesn't exist and for this I would need your assistance. Example:

Code:
if (! -e filename1.txt || ! -e filename2.txt || ! -e filename3.txt) then
echo "One of the filename.txt is missing"
endif
 
O/p (let's say  filename2.txt is not that in directory)
One of the filename.txt is missing

Instead of this ouput is there a cool way of telling user precisely which filename is not present. If possible I wouldn't want to change if and endif structure.

Using a standard Unix shell:
Code:
for f in filename1.txt filename2.txt filename3.txt
do
  [ -e "$f" ] || printf "%s missing\n" "$f"
done

This User Gave Thanks to cfajohnson For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

File existence

Hope someone can help me on this In a directory ,files are dynamically generated.I need a script to do the following if files are not received for more than 2 hours or if the received file is empty then do something How can I put that in a script.Thank you eg. in cd /dir_name the... (13 Replies)
Discussion started by: haadiya
13 Replies

2. Shell Programming and Scripting

Script to check for the file existence, if file exists it should echo the no of modified days

Hi, I am looking for a shell script with the following. 1. It should check whether a particular file exists in a location #!/bin/sh if ; then echo "xxx.txt File Exists" else echo "File Not Found" fi 2. If file exists, it should check for the modified date and run a command... (2 Replies)
Discussion started by: karthikeyan_mac
2 Replies

3. Shell Programming and Scripting

File existence

Hi I'm using the below command in shell script to check for file exists in the path if ..... fi path and test are variables path and the file exists but the commands inside if condition is executed (! operator used) Is the above way of checking for file existence is correct? ... (4 Replies)
Discussion started by: vinoth_kumar
4 Replies

4. Shell Programming and Scripting

Parse file from remote server to calculate count of string existence in that file

Hi I need to parse the file of same name which exist on different servers and calculate the count of string existed in both files. Say a file abc.log exist on 2 servers. I want to search for string "test" on both files and calculate the total count of search string's existence. For... (6 Replies)
Discussion started by: poweroflinux
6 Replies

5. Shell Programming and Scripting

File existence and increment

count=0; while read line; do ] && let count=count+1; done < file_name.txt echo echo "$count of 10 files found " echo The scenario is a follows : I have a file which contains a list of filenames present in particular directory . I am checking fo the existence of the file and... (5 Replies)
Discussion started by: ultimatix
5 Replies

6. Shell Programming and Scripting

Help to Monitor the existence of a file

Hi Folks, Please tell me the unix shell script command to check for the existence of some .done file in a location on the UNIX server. (1 Reply)
Discussion started by: dinesh1985
1 Replies

7. Shell Programming and Scripting

How to check for file existence?

i want to check if the file is in the directory or not, and also it should be handle error conditions, like missing files and report the error and exit. i did something like this: file ="hello" if !test -e "${file}" then echo "No such files exist!" exit 1 else do something....... fi ... (1 Reply)
Discussion started by: mingming88
1 Replies

8. Shell Programming and Scripting

Find Existence of File with wild card using Csh

Hi All, I would like to find out the existence of files with wild card using CSH. I have used the below code but does not seem to work. Can any expert give me some advice ? set nonomatch set pattern = "_xxx" set filetype = ( *$pattern* ) if ( -e $filetype) then echo... (2 Replies)
Discussion started by: Raynon
2 Replies

9. Shell Programming and Scripting

File existence using ls

Hi I want to check a particular file is available or not. But i know only the pattern of that file sat AB1234*.txt.I need the latest file name and it ll be used in the script. How can i do this using ls -ltr command. Thanks, LathishSundar V (2 Replies)
Discussion started by: lathish
2 Replies

10. Shell Programming and Scripting

File existence

Hey all, I have total new with shell scripting so I don't know if what I need to do even possible, here it is...for a duration of time (say...1 hour) I need to check for the existence of a particular file, if it exists then I will invoke a java program or I will continue to check until a)... (2 Replies)
Discussion started by: mpang_
2 Replies
Login or Register to Ask a Question