Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I want to do ls | grep something and cd to the one thing that is listed. I tried with | and searched, but nothing found. Is it using xargs?

share|improve this question
    
Why was this down voted? –  Soviero Jul 25 '12 at 6:28
    
You don't provide any kind of example or input; please tell us what exactly you wish to achieve. –  adaptr Jul 25 '12 at 9:26
    
xargs won't work, as it is a separate process, but the current working directory is specific to each process. So you have to use a shell builtin command, not some binary you start as a separate process. –  MvG Jul 26 '12 at 21:51
add comment

2 Answers

You can do

$ cd $( ls | grep foo )

but that will only work if the result is relative to where you are currently; for anything more you'll want find (it will print paths) and a very specific grep pattern.

share|improve this answer
add comment

If the pattern you want to match can be expressed as a glob, and you know that there will only be one match, you can let your shell do the matching:

$ cd *foo*

Otherwise the easiest option is to command substitution to provide the parameter to cd:

$ cd $(ls | grep '*foo*' | head -n1)
share|improve this answer
add comment

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.