Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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
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 at 20:51

2 Answers 2

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 at 14:46

Your Answer

 
discard

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.