How can I cd to a directory without writing its name?

Any entity in (most) file systems on Linux has an unique identifier called an inode. Notice that a file can have more than one name (hardlink), but directories have just one link in all the filesystems I know of. Notice that the concept of inode is local to the filesystem, so that in two different devices (partition or whatever) the uniqueness is not guaranteed.

You can see the inode of your directory with ls -ldi:

[:~] % ls -ldi tmp/uffa                     
20873234 drwxrwxr-x 2 romano romano 4096 Jun 26  2014 tmp/uffa

So you know that your directory has inode 20873234. Now you can switch to it with the command:

[:~] % cd "$(find ~ -inum 20873234 2> /dev/null)"  
[:~/tmp/uffa] %

(Note that the 2> /dev/null is to shut up error messages about unreadable directories along the path.)

This command will scan the entirety of your home directory, so be patient.1

But what about using cd and the TAB-completion of your shell?


1. This must be the most2 inefficient thing I ever posted ... an ode to entropy. The reason is that the “primary key” under which the access to a file or directory is optimized, fast and direct is — guess what? — the pathname: /dir/dir/file_or_dir. Moreover, to grant you access to a given directory, the system needs to check all the permissions in the path... so again, accessing by inode means scanning recursively the filesystem.

2. Well, you can make thing worse by scanning from /.3

3. But then it won't work, because inodes are unique only on a per-filesystem (per-mount) basis.


You can make Tab rotate the available folders instead of listing them. Edit the file ~/.inputrc and add

"\C-i": menu-complete
"\e[Z": "\e-1\C-i"

If you want it for all users, edit /etc/inputrc instead.

Press Ctrl + x and Ctrl + r to make it effective.

Now use cdTab to navigate to your folder without writing its name. cdShift + Tab will rotate in the other direction.

Worth remembering that cd - will take you to the last visited folder.


You can use shell wildcards.

For instance, I can do

cd a?b?c?d

or

cd a\*b\*c\*d

And it will expand the wildcards to the actual name and change to that directory. Assuming that's the only directory which matches.

If you have both a b c d and a1b2c3d, then cd a?b?c?d will expand to either cd a1b2c3d a b c d or cd a b c d a1b2c3d (the actual order will depend on the kernel, filesystem...), and bash will silently move you to the first path.

On the other hand, you often not have so similarly named folders, so something like a*d is enough to expand that without having to type all the intermediate characters. This is specially useful when you are not actually able to type it (eg. the names are in a different script, or even a different encoding), and you would otherwise have needed to octal-encode the filename.


You can find this directory in a file manager, e.g. nautilus and just drag and drop it to terminal.

If you previously type cd in terminal, you will get the command.