Shell script – find a directory and cd to it
Quite frequently, I reach for a combo of find and cd. Here is how to do it using a single command.
Let’s create a bunch of nested directories, just for the sake of the exercise:
mkdir -p x/y/z
Result:
tree x x └── y └── z
Suppose I want to locate the y directory and cd to it:
cd "$(find x -name y)"
Similarly, I might want to locate a text file and open it in my favourite editor:
cd ~ touch x/y/z/test.txt vi "$(find x -name test.txt)"