$ find -exec cd => gives error: => find: ‘cd’: No such file or directory

The -exec option to find executes an external utility, possibly with some command line option and other arguments.

Your Unix does not provide cd as an external utility, only as a shell built-in, so find fails to execute it. At least macOS and Solaris do provide cd as an external utility.

There would be little or no use for executing cd in this way, except as a way of testing whether the pathname found by find is a directory into which you would be able to cd. The working directory in your interactive shell (or whatever is calling find) would not change anyway.

Related:

  • Understanding the -exec option of `find`
  • Script to change current directory (cd, pwd)
  • What is the point of the `cd` external command?

If you're having issues with a directory's name being strange or extremely difficult to type, and you want to change into that directory, then consider creating a symbolic link to the directory and then cd into it using that link instead:

find . -inum 888696 -exec ln -s {} thedir ';'

This would create a symbolic link named thedir that would point to the problematic directory. You may then change working directory with

cd thedir

(if the link exists in the current directory). This avoids modifying the directory in any way. Another idea would be to rename the directory in a similar way with find, but that would not be advisable if another program expects the directory to have that particular name.


find runs the -exec command itself, it doesn't involve a shell. Even if it did, the change of directory would only persist until that shell exited, immediately after the cd.

You'll need to get the filename out to the current shell to cd into it. Depending on how bad your filenames are, you could use command substitution:

cd "$(find . -inum 888696)"

That won't work if the filename ends in a newline, as command substitution eats trailing newlines. In that case you'd need to protect the newline and get rid of the one find adds when printing:

dir=$(find . -inum 888696; echo x)
cd "${dir%?x}"

Or, with GNU find, have it not print the trailing newline (but still protect any in the filename):

dir=$(find . -inum 888696 -printf "%px" -quit)
cd "${dir%x}"

Also using the -quit predicate (also a GNU extension), to stop looking after the first match as an optimisation.

Alternatively, you could start a new shell from within find, but it's a bit ugly:

find . -inum 888696 -exec bash -c 'cd "$1" && exec bash' sh {} \;

Not with exec, but this may be good enough for you:

cd "$(find . -inum 888696 -type d)"

The "-type d", just to be sure. Of what, I don't really know.