5

I want to find a directory using grep then change current directory to the resulting directory. For example:

$ ls | grep 1670 |

finds me directory haib12CJS1670. I am trying to do something like below:

$ ls | grep 1670 | cd

so that my directory is set to haib12CJS1670 at a single step. Obviously my way is not working. Any suggestions? Thank you

2
  • 5
    Do you have a good reason to be using grep instead of globbing (i.e., cd *1670*?)
    – Wooble
    Jul 25, 2012 at 13:39
  • That is great comment for me, since I did not even know I could do that.
    – Supertech
    Sep 17, 2016 at 19:02

1 Answer 1

8
 cd `ls | grep 1670`

should get your cd to work (note, those are "back-ticks")

An alternative approach (some would say preferred) would be to use the $ substitution. E.g.,

 cd $(ls | grep 1670)

though I can't get this to work with the tcsh, it works fine with bash.

The first solution is shell-agnostic :)

1
  • Wow, you are lightning fast Levon. Thank you!
    – Supertech
    Jul 25, 2012 at 13:40

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