You want wildcard expansion to find a path anywhere on the system. You can do that, if you specify the pattern correctly:
cd /**/thedirectoryiwanttogointo
will work in zsh and fish by default, in Bash if you first shopt -s globstar
, in tcsh if you set globstar
, in ksh93 if you set -o globstar
, in yash
if you set -o extended-glob
.
This works because the wildcard is used within an absolute path starting with /
instead of being relative to the current directory, and when you opt into using the **
pattern it expands to any number of subdirectories.
While this will work, it is likely to take an extremely long time. Your shell has to search your entire system for the matching file(s). You can cut that down by providing a longer prefix to trim out more of the search space:
cd /home/pulsar/**/thedirectoryiwanttogointo/
The further you drill down the less searching there will be to do. If the directory tree is fairly sparse, this won't take too long and is actually practical to use. The trailing /
ensures that the expansion is only to a directory, which removes one variety of ambiguity that could occur.
If there's more than one match still, the result will depend on your shell. Bash and fish will switch to the alphabetically first match, ignoring the ambiguity. tcsh and yash will report an error. zsh and ksh will interpret it as an attempt to use one of their advanced directory-switching features and probably report an error.
If you're likely to use the same directory repeatedly, either putting the parent into CDPATH
or creating a shell variable $FOO
you can use in place of the long path is a better option. You can combine the shell variable with **
expansion to save more typing if you're using it often.
cd */foo
to mean "find a directory calledfoo
anywhere on the system andcd
to it"? – Michael Homer Nov 07 '15 at 04:17*
expands where it is, which is the current directory when it's at the start of the path - consider, say,*.txt
) – Michael Homer Nov 07 '15 at 04:22*/foo
expanded to the path tofoo
found anywhere on the system,*.txt
would have to expand to every.txt
on the system. Paths that don't start with a/
are always treated as relative to the current directory. If there is a file./x/foo
then*/foo
will expand to the (relative) path to it, and you couldcd
to that. Searching the entire system would at the least be very time-consuming. There may be some other way of accommodating your workflow, though. – Michael Homer Nov 07 '15 at 05:30cd **/foo
should work in zsh. – sendmoreinfo Nov 07 '15 at 11:40