How to locate a file in the system path?

Discussion in 'Command Line' started by joe99, Mar 15, 2014.

  1. joe99

    joe99 New Member

    Messages:
    3
    Likes Received:
    1
    Trophy Points:
    3
    How to locate a file in the system path?
    I want to search for a file within the PATH environment variable.
    How can this be made?

  2. Snipert

    Snipert New Member

    Messages:
    10
    Likes Received:
    2
    Trophy Points:
    3
    Best Answer
    This is a odd question. which & whereis are specified commands for finding files along the PATH variable..
    But for a generic file that otherwise wouldn't come up, I'd try using find. A quick idea is this:
    #find $(echo $PATH | tr : " ") -iname filename.ext

    It's a hack, but here's my reasoning:
    The PATH variable contains : to seperate each path. We need to change that to spaces as the find syntax is --> find /path/dir /path/dir2 -commands searchterm
    tr(anslate) does this, so by getting the output of tr by using $() we get all the paths needed, without needing to "hard-code" it.
  3. arochester

    arochester Active Member

    Messages:
    761
    Likes Received:
    240
    Trophy Points:
    43
    Not sure that I fully understand this.

    http://www.linfo.org/path_env_var.html

    2 useful commands are: whereis and which.

    E.g.
    whereis chromium
    chromium: /usr/bin/chromium /etc/chromium /usr/lib/chromium /usr/bin/X11/chromium /usr/share/man/man1/chromium.1.gz

    and

    which chromium
    /usr/bin/chromium

    "which" finds the executable.
  4. joe99

    joe99 New Member

    Messages:
    3
    Likes Received:
    1
    Trophy Points:
    3
    Hi Arochester,

    This is a quiz question. What is requested is to show how can we locate a generic file in the system path.

    "How to locate a file called “HelloWorld.java” in the system path?"

    I would appreciate your help on this question.
  5. Snipert

    Snipert New Member

    Messages:
    10
    Likes Received:
    2
    Trophy Points:
    3
    Best Answer
    This is a odd question. which & whereis are specified commands for finding files along the PATH variable..
    But for a generic file that otherwise wouldn't come up, I'd try using find. A quick idea is this:
    #find $(echo $PATH | tr : " ") -iname filename.ext

    It's a hack, but here's my reasoning:
    The PATH variable contains : to seperate each path. We need to change that to spaces as the find syntax is --> find /path/dir /path/dir2 -commands searchterm
    tr(anslate) does this, so by getting the output of tr by using $() we get all the paths needed, without needing to "hard-code" it.
  6. joe99

    joe99 New Member

    Messages:
    3
    Likes Received:
    1
    Trophy Points:
    3
    That's a very clever approach, and it works!
    Thank you Snipert!
    Snipert likes this.

Share This Page