Go to directory with unknown name

A general approach to be able to operate on any directory (or file) is to use its inode number.

First, use ls to get the inode of the folder in question:

ls -lia
  • -l for long listing (optional)
  • -i to get inode values
  • -a to view all files, even hidden files (optional)

In the listing you'll see a long number in the first column, something like this:

2104697 drwx------ 2 user group 4096 date time WEIRD_DIRNAME

Now you can change to that directory using:

cd "$(find -inum ######)"

...but substituting ###### with the long number that you got in the directory listing.

Of course the command doesn't have to be cd. It could be rm, cp, mv, etc...


Since the characters are likely international characters, they likely are appearing as control codes in your en_US character set, which is why ls is showing them as question marks.

This may be due to your distribution's choice of alias'ing ls by default in the global profile. For example:

alias ls='ls -q'

You can see if your distribution has done so by typing into your shell:

alias ls

Which will print out something like the following:

$ alias ls
alias ls='ls --color=auto'

The page linked below suggests that if you do "ls -b", you will see the octal control codes so you know at least what characters are in use.

http://www.arsc.edu/arsc/support/howtos/nonprintingchars/

Assuming that doesn't work you have another option also mentioned there nearer to the bottom:

A tool I was unaware of until now called OctalDump (on Debian -- /usr/bin/od)

$ ls | od -b

Will show you the octal form of the control codes.

$ ls | od -c

Will show you the characters including any hidden additional whitespace that might be at the end (note the extra spaces on the end in their example)

Once you know what control characters are in play and whether there is additional hidden whitespace, you may be able to finagle a way to cd to the directory by escaping the control characters. Failing that, you have a couple of options for renaming the directory.

The page above also, conveniently, has a quick and dirty method for removing the control characters via the mv command, which I think will work, so long as you DO escape the space, but DONT escape the question marks (thusly making them single character wildcards), and so long as nothing else exists in the directory where this rogue directory is. The reason for these explicit requirements is because you're using only wildcard characters and the single space, and so you don't want the wildcard to inadvertently match any other file or directory.

If the above method does not work, you can almost assuredly use the find method mentioned by other responses, in a different way. Rather than trying to cd to the directory using the inode number, which is what you said you have tried, you can rename it using the inode number. Just be sure to copy the entire set of 3 lines for that one command from the page above (replace the "desired-name" part with the actual name you want to rename it to however).

Once the directory is renamed, then you should be able to access it. I can only hope that it doesn't also contain files with control codes in the names, or you will have to rinse and repeat for every file (and/or subdirectory) in the directory you just renamed, at which point a for loop would probably be wise to just name every file as a number, by incrementing from 1. :-)