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

I want to go to a directory with using filter

For example there is a file named this-is-awsome

ls | grep this-is-awsome | xargs cd

How can I go to a directory with filter?

share|improve this question
Here is your answer cd is not an external command - it is a shell builtin function. It runs in the context of the current shell, and not, as external commands do, in a fork/exec'd context as a separate process. – Raza Jul 1 '14 at 20:51

Salton's comment explains the problem. Here are some solutions:

cd "$(ls | grep this)"

This is probably not so good, with all the usual caveats about parsing the output of ls applying to it.

A slightly better version (assumes GNU find):

cd "$(find -maxdepth 1 -type d -name '*this*')"

Yet another (maybe even better) solution if you're using Bash:

shopt -s nullglob
cd *this*/
share|improve this answer

When you have one file with "this", just use

   cd *this*
share|improve this answer
1) This might pick up a file, which is why I have appended a / in my glob 2) This will error out if the glob fails to match, which is why I thought shopt -s nullglob was necessary. – Joseph R. Jul 3 '14 at 14:46

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.