How to find -exec cd in linux / unix How to find -exec cd in linux / unix unix unix

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.)


In case you have more than one config directory:

select config in $(find . -name config -type d)do  cd $config  breakdone


find runs -exec programs as subprocesses and subprocesses cannot affect their parent process. So, it cannot be done. You may want to try

cd `find . -name "config"`