In terminal is there a way for Terminal to guess a file based on an incomplete file path? For example: Instead of /Users/me/foo1/foo2/foo3/foo4/TargetFile, is there a way to just do /foo3/foo4/TargetFile? I want to be able to access the file no matter where it is. Thanks.

  • You can create a environmental variable for that file on the .bash_profile. Something like: myFile="$HOME/foo1/foo2/foo3/foo4/TargetFile . After that, access to the file with $myFile
    – user161254
    Commented Aug 18, 2017 at 22:37
  • Would this work on any computer, or just my own? I need it to be universal.
    – Ben A.
    Commented Aug 18, 2017 at 22:52
  • Only in your own. In other Macs or Linux you have to manually type the variable the first time.
    – user161254
    Commented Aug 18, 2017 at 22:55
  • Not sure what your use case is. If you are using a script, place it in the same or a related folder. Then you can use relative paths - just drop the first / or use ../ for folders above your current level.
    – Araho
    Commented Aug 18, 2017 at 23:01
  • @Araho Would you mind explaining what you mean by that and what a related folder is?
    – Ben A.
    Commented Aug 18, 2017 at 23:03

3 Answers 3


If the file's path is relative to the current working directory your could use:

find . -name targetfile

You could wrap this in a shell function:

mylocate () {
    find . -name "$1"

# Usage
mylocate targetfile

In zsh you can use the globstar feature:

printf '%s\n' **/targetfile

In ksh:

set -G
printf '%s\n' **/targetfile

In bash4:

shopt -s globstar
printf '%s\n' **/targetfile
  • 1
    I often use nullglob with globstar -- that way you do not get the literal string "**/targetfile" if there is no such file. Commented Aug 20, 2017 at 2:15

There's a couple of things you can do:

Use the ~

In the example you cited, you can use the tilde as a shortcut to your home directory


If you want to access a file "no matter where it is" create a symlink and make sure it's in your path

export $PATH
ln -s /Users/me/foo1/foo2/foo3/foo4/TargetFile /User/me/TargetFile`

(You can put the first line in your .bash_profile so it's available in every session)

Your path can be anywhere you like - I'm just using your home directory as an example.

  • My issue is that I want it to be accessible on any computer, where I would only know the location of the file within nested folders which I know the names of. This means that I might not know where the user put the file.
    – Ben A.
    Commented Aug 18, 2017 at 22:56
  • Environment variables, aliases, paths, etc. are different for each user. You would have to put it each profile and in the skeleton profile so it propagates to new users.
    – Allan
    Commented Aug 18, 2017 at 22:59
  • What do you mean by that? Would you mind explaining?
    – Ben A.
    Commented Aug 18, 2017 at 23:27
  • Your shell has an "environment" which has a number of settings. Type printenv in Terminal to get a list of what's set. This environment will change for each user. This means, if you want it to be "universal" across all users/machines, you need to set it for everyone. It sounds like UNIX admin is new for you. I suggest starting here: tutorialspoint.com/unix
    – Allan
    Commented Aug 18, 2017 at 23:43

If you know the target file is in the user's $PATH, you can say

target=$(which TargetFile)
"$target" arg ...

Otherwise, you can find the file with

while IFS= read -r possible_target; do
    if is_this possible_target_the_one_you_want; then
done < <(locate '*/TargetFile')
"$target" arg ...

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .