How to rename a directory with name made of non-printable characters only

directorysolaris

On SunOS I have directories with names containing only non-printable characters. The command

ls -ldb *

returns the following (some spaces deleted)

drwxr-xr-x 3 user1 group1 3 Feb 4 18:10 \0122011\0122016\0122016\0122016\0122011\0122011\0122011\0122011\0122011\0122011\0122016\0122011\0122012/
drwxr-xr-x 3 user1 group1 3 Feb 4 11:03 \0122016\0122016/

How can I rename these directories?

Best Answer

Similar text as ls -ldb * could be produced by (ksh, bash, zsh) $'...', as this:

echo $'\0122016\0122016'

Which is just a bunch of new-lines (Oct 012, Hex 0x0A) and years.

If limited on the shell you could use, then use printf :

printf '\0122016\0122016'

Note that the above code does not include the last /. Which in fact is not needed to give the name of the directory to the kernel.

So, to erase, use:

rmdir "$(printf '\0122016\0122016')"

To rename use:

mv "$(printf '\0122016\0122016')" newname

The other file will need this:

mv "$(printf '\0122011\0122016\0122016\0122016\0122011\0122011\0122011\0122011\0122011\0122011\0122016\0122011\0122012')" newname

Or, if there are not other files with a similar name, a shorter string:

mv "$(printf '\0122011\0122016\0122016\0122016')"* newname
Related Question