LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 04-01-2008, 10:44 AM   #1
brg14
LQ Newbie
 
Registered: Apr 2008
Posts: 3

Rep: Reputation: 0
Using grep to verify file exists


Hi,

I need a little help. I want to use grep -c to check that a filename exists on another server...

my script will ssh into the server, then use grep -c "filename" /opt/blah/blee/bloo

I will use the number returned (anything higher than 0) in an if statement to verify the file exists before trying to copy the file to another server (without this check I get an error and the script fails)

It's probably an easy answer, but I'm still new to this.

Thanks!
 
Old 04-01-2008, 11:30 AM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
I've not really understood the usage of grep -c to check if a file exists (please elaborate). Anyway, if you want to check if a file exists, you can do something like this
Code:
if ssh user@host "test -f /path/to/testfile"
then
   echo file exists
else
   echo file not found
fi
If you want to use grep -c and retrieve the number of lines that match a pattern in a particular file, you can do something like
Code:
count=$(ssh user@host "grep -c pattern testfile")
and have the value of the variable "count" at your disposal.
 
Old 04-01-2008, 11:40 AM   #3
brg14
LQ Newbie
 
Registered: Apr 2008
Posts: 3

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by colucix View Post
I've not really understood the usage of grep -c to check if a file exists (please elaborate). Anyway, if you want to check if a file exists, you can do something like this
Code:
if ssh user@host "test -f /path/to/testfile"
then
   echo file exists
else
   echo file not found
fi
If you want to use grep -c and retrieve the number of lines that match a pattern in a particular file, you can do something like
Code:
count=$(ssh user@host "grep -c pattern testfile")
and have the value of the variable "count" at your disposal.
Basically what I want to do is use grep to check and see if a log file exists on a server before the script copies the file. I thought there might be a way to use grep to count the number of times filename.log appears in a particular directory. If the count returns anything over 0, that means that file is in there and the script can go ahead and copy it.
 
Old 04-01-2008, 11:51 AM   #4
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
Well. Grep searches a given pattern in the file content, not a file inside a directory. You can try to use the command test as in my first example above. Furthermore, what do you mean for "to count the number of times filename.log appears in a particular directory"? The file is unique: it exists or not. Eventually you can look if one or more files whos names begin with a particular string exist and how many they are. Or maybe I have still misunderstood your post, sorry.
 
Old 04-01-2008, 11:57 AM   #5
brg14
LQ Newbie
 
Registered: Apr 2008
Posts: 3

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by colucix View Post
Well. Grep searches a given pattern in the file content, not a file inside a directory. You can try to use the command test as in my first example above. Furthermore, what do you mean for "to count the number of times filename.log appears in a particular directory"? The file is unique: it exists or not. Eventually you can look if one or more files whos names begin with a particular string exist and how many they are. Or maybe I have still misunderstood your post, sorry.
Well, it will be part of an IF statement in a script. Something like...

num = count number of times filename.log appears in directory

if
num > 0
then
copy file to new location for analysis

if the file does not exist, the script errors, so the if statment is necessary.

there may be another way to check if a file exists... I figured this would be the easiest (if grep can even do this). Sorry, I'm still new to this.

I will give TEST a shot.

Thanks.

Last edited by brg14; 04-01-2008 at 11:59 AM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to Verify File Types craigjward Programming 6 11-21-2007 08:16 AM
Grep-like filter exists? carl.waldbieser Programming 3 09-01-2005 12:34 AM
file not found by glob? - file exists? rgdatit Linux - Newbie 2 07-29-2005 03:16 AM
SIOCADDRT: File exists SIOCCADDRT: File Exists Failed to bring up eth0. opsraja Linux - Networking 0 01-10-2005 09:29 AM
Linker problem: can't find a file, but the file exists atlep Programming 5 08-16-2004 07:15 AM


All times are GMT -5. The time now is 04:21 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration