Q. There is a way to cd a particular directory even if you spelled incorrectly on the command line. The command was added to my shell startup file. After formatting my hard disk, I lost my original file. Can you tell me the shell option or command that will automatically correct errors in the spelling of a directory name in a cd command?

Advertisement

A. There is a bash shell variable called cdspell If set, minor errors in the spelling of a directory component in a cd command will be corrected. The errors checked for are transposed characters, a missing character, and one character too many. If a correction is found, the corrected file name is printed, and the command proceeds. This option
is only used by interactive shells.

To toggle the values of cdspell variables controlling optional behavior use shopt command as follows:
$ shopt -s cdspell
$ cd /etx
$ pwd

Add above command to your .bash_profile file.

🥺 Was this helpful? Please add a comment to show your appreciation or feedback.

nixCrat Tux Pixel Penguin
Hi! 🤠
I'm Vivek Gite, and I write about Linux, macOS, Unix, IT, programming, infosec, and open source. Subscribe to my RSS feed or email newsletter for updates.

2 comments… add one
  • Corey Hart Jul 13, 2007 @ 17:39

    csh/tcsh users:

    To enable spelling correction, add one of the following to ~/.tcshrc

    * set correct = cmd
    * set correct = all

    The first option causes spelling correction to be applied for commands only
    The second tries to correct all entries on the command line

  • 🛡️ Vivek Gite (Author and Admin) nixCraft Jul 14, 2007 @ 22:12

    Corey,

    Nice, thanks for sharing tips with us!

Leave a Reply

Your email address will not be published. Required fields are marked *

Use HTML <pre>...</pre> for code samples. Your comment will appear only after approval by the site admin.