Bash – Fix ‘find -exec cd’ Error: No Such File or Directory

bashcd-commandfind

When I run this command it works:

$ find . -inum 888696 -exec ls '{}' \;
Conversation.pst  Outlook Data File  Outlook Data File.sbd  Trash      Unsent Messages
Inbox.pst     Outlook Data File.msf  Sent.pst       Trash.msf  Unsent Messages.msf

However, When replacing ls with cd it does not work:

$ find . -inum 888696 -exec cd '{}' \;
find: ‘cd’: No such file or directory

I know cd is a bash built-in, so I tried this which does not work either:

$ find . -inum 888696 -exec builtin cd '{}' \;
find: ‘builtin’: No such file or directory

How can I use cd along with find -exec command?


UPDATE

The reason I'm trying to use cd with find -exec is that the directory name is a strange one which shows up on my terminal as something like ????.

Best Answer

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:


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.

Related Question