General
cancel
Showing results for 
Search instead for 
Did you mean: 

looking for command that would CD in first dir found.

SOLVED
Go to solution
Daniel Simard
Frequent Advisor

looking for command that would CD in first dir found.

I would like to know how i could pipe the result of a directory search to 'CD' so that upon finding the directory, it automatically goes into it.

for example, i'm looking for directory called 'foobar', i know i have it somewhere, maybe even in more than 1 place, what i want to do is search for it

find / -name "foobar" -type d

AND, once it finds the location, the 'find' command displays where it found it, but i want to go further in my initial command and automatically CD to it.

find / -name "foobar" -type d | cd
(that doesn't work)

i've tried | cd %1 or $1 or $0,

how can this be done?
Si tu n'as pas ce que tu aimes, aimes ce que tu as.
1 REPLY
Ivan Ferreira
Honored Contributor
Solution

Re: looking for command that would CD in first dir found.

You can use:

cd $(find / -name "foobar" -type d)

That is called command substitution.

Also, remember that the find command may find more than one directory with the same name, then you must filter the results somehow, for example:

cd $(find / -name "foobar" -type d | head -1)
Por que hacerlo dificil si es posible hacerlo facil? - Why do it the hard way, when you can do it the easy way?