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
?
Asked
Active
Viewed 1,634 times
2
-
Why was this down voted? – Soviero Jul 25 '12 at 06: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 09: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
2 Answers
5
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.

Andrew
- 8,002
- 3
- 36
- 44
1
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)

mgorven
- 30,615
- 7
- 79
- 122