Find the answer to your Linux question:
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.
  1. #1
    Just 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?

  2. #2
    Linux Guru Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,252
    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:
    Code:
    open FILE, "/foo/bar";
    
    chomp&#40;@lines = <FILE>&#41;;
    
    for $file &#40;@lines&#41;
    &#123;
        system "grep 'foo' $file";
    &#125;
    Basically, open the file, read it into an array, and use a foreach loop to feed each one into grep.

  3. #3
    Banned CodeRoot's Avatar
    Join Date
    Sep 2005
    Posts
    567
    1) ls <file specification>

    2) grep 'something' `cat file_list | grep 'foo'`

  4. #4
    Just Joined!
    Join Date
    Nov 2005
    Posts
    2
    Hey, the backquotes were exactly what I was looking for. Thanks!

  5. #5
    Banned CodeRoot's Avatar
    Join Date
    Sep 2005
    Posts
    567
    "You are welcome..."

  6. #6
    scm
    scm is offline
    Linux Engineer
    Join Date
    Feb 2005
    Posts
    1,044
    Quote Originally Posted by dre2xl
    Hey, the backquotes were exactly what I was looking for. Thanks!
    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.
    Code:
    grep 'something' $&#40;cat file_list | grep 'foo'&#41;`

  7. #7
    Linux Guru anomie's Avatar
    Join Date
    Mar 2005
    Location
    Texas
    Posts
    1,692
    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.
    Code:
    file /path/to/file_here
    Also gives you metadata about the file if it does exist.

  8. #8
    Linux Guru bryansmith's Avatar
    Join Date
    Nov 2004
    Location
    /Ontario/Canada
    Posts
    2,619
    If you are just looking in a specific directory, try
    Code:
    slocate <filename> | grep <dir>
    after updatedb.

    Bryan
    Looking 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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •