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?

  • 2
    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*/
| improve this answer | |

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

   cd *this*
| 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

This works for me:

>>pwd | xclip

>>cd `xclip -o`
| improve this answer | |
  • Why do you need xclip there? – muru Sep 10 '19 at 14:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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