Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Is it possible to redirect a command's output to cd? For example, I searched for a directory using locate and got the path to it. Now, instead of writing a cd path, can I redirect the locate output to cd?

I tried this:

$ locate Descargas | grep Descargas$
$ locate Descargas | grep Descargas$ | cd
$ locate Descargas | grep Descargas$ > cd
$ locate Descargas | grep Descargas$ < cd

No luck. This probably isn't particularly useful, but I'm curious.

share|improve this question
up vote 15 down vote accepted

You want command substitution, not redirection:

cd "$(locate Descargas | grep -F 'Descargas$')"

The bits between the $( and the ) are run as a command and the output (stripped of any final newline) is substituted into the overall command.

This can also be done with ‘back ticks’ (“`”):

cd "`locate Descargas | grep -F 'Descargas$'`"

The dollar-paren syntax is generally preferred because it is easier to deal with in nested situations:

# contrived
cd "$(grep '^dir: ' "$(locate interesting-places | head -1)" | sed 's/^[^ ]*//')"
share|improve this answer
Thanks! I didn't know that functionality – oli206 Nov 14 '10 at 12:54

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.