How to find -exec cd in linux / unix
I'm searching for a config folder, and trying to change to that directory:
find . -name "config" -exec cd {} \;
There is one match, ./my-appli开发者_StackOverflow中文版cations/config
, but after I try this it says:
find: `cd': No such file or directory
What am I doing wrong?
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
break
done
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"`
精彩评论