searching if the file exists


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting searching if the file exists
# 1  
Old 04-25-2010
searching if the file exists

Hi All,

I want to search multiple files but i dont know their exact names

ls -ltr a*
a1.240410
a2.240410
a3.240410
a4.240410

these file names keep changing acco to date or randomly

so I wanna check if any file with a* exists and then perform an actiion

I was trying

Code:
 
if [ -e "a*" ]; then
....
..
fi

but looks like with if i hav to give complete file name like
if [ -e a1.240410 ]

please help.. I wanna check if a* files exist . thanks!

---------- Post updated at 09:52 AM ---------- Previous update was at 09:45 AM ----------

Also if i try to do ls -ltr a* and if no files exist then i dont want any error like " no such file or directory"

like

(ODSSmilierd)/home/r104809> ls -ltr proc* | wc -l
proc*: No such file or directory
0

I dont want this error I just need the count 0

what change will help me?
Thanks guys!!
# 2  
Old 04-25-2010
Code:
if ls a* > /dev/null 2>&1; then
  echo "files exist"
else
  echo "files do not exist"
fi

# 3  
Old 04-25-2010
This will do stuff on the files if they exist

Code:
for FILE in a*
do 
  ls -l $FILE
  strings $FILE
  . . .
done

# 4  
Old 04-25-2010
masta
Why scottn is right and you are not ?
# 5  
Old 04-25-2010
No person is wrong.

Quote:
Originally Posted by danmero
masta
Why scottn is right and you are not ?
No person is wrong.

Scottn's solution is, in my personal opinion, not as ideal as my own proposal.

The reason is that Scottn's solution requires to invoke `ls -l`, where mine uses pure shell globs. I see no reasons to venture outside the capability of the shell. There is no reason to test for the existence of the files when a pattern-match (glob) will reveal their existence. No reason to see if the ls command returns true... it's a waste of time

have a nice day
# 6  
Old 04-26-2010
What is wrong with this?
Code:
$ [ -f m* ] && echo "file exists" || echo "file does not exist"
file exists
$ [ -f a* ] && echo "file exists" || echo "file does not exist"
file does not exist

I am able to check for any files using wild cards as you asked for using the -f (exists and is a file) and -e (exists).

Why do you think this does not work?

Also to add to the example masta gives:
Code:
for F in m*
do
    # Gather a list of files for use later
    FILES="${FILES} ${F}"
    # or you can simply preform actions here on each
    # individual file
done

# The variable FILES will either be empty or have a list of files you asked for
# If nothing matches m* then it is empty

echo "${FILES}" # or use the var FILES as needed.
# While '/bin/echo' exists echo is also built-in in bash and sh. 
# Since we give no path echo is overridden here.
# man bash and look under 'SHELL BUILTIN COMMANDS'

I am using 100% pure shell globs and built-ins and no external calls this way.


masta, you make good point however you do use an external invoke of "ls -l" and "strings" and you do it multiple times (1 for each file) whereas scottn only makes the call once.
The difference here is presumably meant to imply "only in your initial check for existence of the files" and that what you do "with" these files once found is another matter. We can't use built-ins for everything after all Smilie

I just meant to add that it may seem confusing to new script writers if the distinction is not made.

Code:
for FILE in a*    # Pure shell glob here but...
do 
  ls -l $FILE       # This is just as much of an external call as scottn does!
  strings $FILE
  . . .
done

I do however tend to agree with what masta means to show you, scottn has a valid working solution however we should (in my opinion) try to use the built-in functions of the shell we are in if external applications (ls in scottn's case) is not needed.

Now again this is a personal preference and scottn's method does have an advantage in one case. If the built-ins we rely on are shell specific (example: parameter expansion, let, set, unset, export, pushd, popd) then we cannot port this script in it's raw form to another shell (ie from bash to ksh/csh/sh). In this case using the external "ls" and "echo" commands means that we can use this script in any shell that can find the ls command in it's path!

Last edited by ddreggors; 04-26-2010 at 03:18 AM..
# 7  
Old 04-26-2010
Quote:
Originally Posted by ddreggors
Also to add to the example masta gives:
Code:
for F in m*
do
    # Gather a list of files for use later
    FILES="${FILES} ${F}"
    # or you can simply preform actions here on each
    # individual file
done

# The variable FILES will either be empty or have a list of files you asked for
# If nothing matches m* then it is empty

echo "${FILES}" # or use the var FILES as needed.
# While '/bin/echo' exists echo is also built-in in bash and sh.
# Since we give no path echo is overridden here.
# man bash and look under 'SHELL BUILTIN COMMANDS'

Your assumption about FILE being empty if there are no matching files is incorrect. Shell globs that do not match anything do not simply disappear, or expand to nothing; they remain unexpanded, and the for loop's list will be that single value.

Test run:
Code:
$ for F in m*; do echo "$F"; done
m*

That code, and masta's solution, will both execute the loop at least once. If that is undesirable, the existence of the value in F must be tested; it cannot be taken for granted.

Regards,
Alister

Last edited by alister; 04-26-2010 at 04:24 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Search if file exists for a file pattern stored in array

Hi experts, I have two arrays one has the file paths to be searched in , and the other has the files to be serached.For eg searchfile.dat will have abc303 xyz123 i have to search for files that could be abc303*.dat or for that matter any extension . abc303*.dat.gz The following code... (2 Replies)
Discussion started by: 100bees
2 Replies

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

3. Windows & DOS: Issues & Discussions

Script that, if file exists in Samba share, moves file to Unix server

I'm looking to do pretty much what the title says. I want a script that runs, it can run on Unix or Windows, doesn't matter, and searches a Samba shares for a .txt file. If the file exists, the script will move (or possibly copy) the file from the Samba share into a directory on our Unix... (3 Replies)
Discussion started by: twcostello
3 Replies

4. Shell Programming and Scripting

searching a file with a specified text without using conventional file searching commands

without using conventional file searching commands like find etc, is it possible to locate a file if i just know that the file that i'm searching for contains a particular text like "Hello world" or something? (5 Replies)
Discussion started by: arindamlive
5 Replies

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

6. Shell Programming and Scripting

Newbie.. Find if a file exists and open, if not create the desired file..

Hey all, I'm brand new to script writing, I'm wanting to make a script that will ask for a file and then retrieve that file if it exists, and if it doesn't exist, create the file with the desired name, and I'm completely stuck.. so far.. #! bin/bash echo "Enter desired file" read "$file" if ... (5 Replies)
Discussion started by: Byrang
5 Replies

7. Shell Programming and Scripting

Grep pattern from different file and display if it exists in the required file

Hi, I have two files say xxx.txt and yyy.txt. xxx.txt is with list of patterns within double quotes. Eg. "this is the line1" "this is the line2" The yyy.txt with lot of lines. eg: "This is a test message which contains rubbish information just to fill the page which is of no use. this is... (3 Replies)
Discussion started by: abinash
3 Replies

8. Shell Programming and Scripting

File Exists

Hi, I have scenerio where i need to check 3 files whether they are of current day files and then count should not be equal to zero. how to achieve the same please let me know. Vishal (17 Replies)
Discussion started by: shady4u
17 Replies

9. Shell Programming and Scripting

Need to write a script in UNIX to find a file if another file exists

So I have a lot of Java applications on my servers all having their own folder from the applications subdirectory. Now, I need to do the following. Search all the applications subdirectories for message.jar. If the message.jar file exists, I need to search the application directory for... (1 Reply)
Discussion started by: mmdawg
1 Replies

10. Shell Programming and Scripting

Check File Exists and compare to previous day file script

We have data files that are ftp'd every morning to a SUN server. The file names are exactly the same except for that each has the date included in its name. I have to write script to do 2 things: STEP 1) Verify that the file arrived in morning. STEP 2) Compare the file size of the current... (3 Replies)
Discussion started by: rbknisely
3 Replies
Login or Register to Ask a Question