How to find -exec cd in linux / unix
The command cd
is a shell built-in, not found in /bin
or /usr/bin
.
Of course, you can't change directory to a file and your search doesn't limit itself to directories. And the cd
command would only affect the executed command, not the parent shell that executes the find
command.
Use:
cd $(find . -name config -type d | sed 1q)
Note that if your directory is not found, you'll be back in your home directory when the command completes. (The sed 1q
ensures you only pass one directory name to cd
; the Korn shell cd
takes two values on the command and does something fairly sensible, but Bash ignores the extras.)