How to "cd" the directory after "grep"?

I want to find the directory using grep , then change the current directory to the resulting directory. For instance:

 $ ls | grep 1670 | 

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

 $ ls | grep 1670 | cd 

so that my directory is installed on haib12CJS1670 in one step. Obviously my path does not work. Any suggestions? Thanks you

+6
source share
1 answer
  cd `ls | grep 1670` 

should get your cd to work (note that these are "reverse ticks")

An alternative approach (preferable by some assumptions) would be to replace $ . For instance.

  cd $(ls | grep 1670) 

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

The first solution is a shell :)

+8
source

Source: https://habr.com/ru/post/921307/


All Articles