Results 1 to 8 of 8
This is a DUMB question but the Google results were very busy... all scripting-related.
1) What's a simple way of determining if a file exists? find/slocate will print out multiple ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
- 11-15-2005 #1Just Joined!
- Join Date
- Nov 2005
- Posts
- 2
Checking for the file existence from the command line...
This is a DUMB question but the Google results were very busy... all scripting-related.
1) What's a simple way of determining if a file exists? find/slocate will print out multiple results if they exist. I just want a 'yes' or 'no' type response.
While I'm at it...
2) What's the best way to pipe output to grep as a file command argument (or any other commands for that matter)? For example, cat file_list | grep 'foo' will print out the lines of files that contain 'foo'... but what if I want to specify the lines themselves as file-name arguments to grep?
- 11-15-2005 #2
1) If you're using Bash or Perl, you can use the "-e" check to see if the file exists:
Code:Bash: file=/foo/bar if [-e "$file"]; then echo "Yay!" fi Perl: $file = "/foo/bar"; print "Yay!" if -e $file;
2) I would do this in Perl as such:
Basically, open the file, read it into an array, and use a foreach loop to feed each one into grep.Code:open FILE, "/foo/bar"; chomp(@lines = <FILE>); for $file (@lines) { system "grep 'foo' $file"; }
- 11-16-2005 #3
1) ls <file specification>
2) grep 'something' `cat file_list | grep 'foo'`
- 11-16-2005 #4Just Joined!
- Join Date
- Nov 2005
- Posts
- 2
Hey, the backquotes were exactly what I was looking for. Thanks!
- 11-17-2005 #5
- 11-19-2005 #6Linux Engineer
- Join Date
- Feb 2005
- Posts
- 1,044
I think using $(command) is preferrable to backticks - they're a lot easier to nest for one thing! And less easy to inadvertently use the wrong quotes, too.
Originally Posted by dre2xl
Code:grep 'something' $(cat file_list | grep 'foo')`
- 11-19-2005 #7What's a simple way of determining if a file exists? find/slocate will print out multiple results if they exist. I just want a 'yes' or 'no' type response.Also gives you metadata about the file if it does exist.Code:
file /path/to/file_here
- 11-19-2005 #8
If you are just looking in a specific directory, try
after updatedb.Code:slocate <filename> | grep <dir>
BryanLooking for a distro? Look here.
"There can be no doubt that all our knowledge begins with experience." - Immanuel Kant (Critique of Pure Reason)
Queen's University - Arts and Science 2008 (Sociology)
Registered Linux User #386147.


Reply With Quote
