3

How can I change directory to directory that name consists of non ASCII default characters?

root@senior:/media/data/hudba/# ls . -l
total 3096
-rwxr-xr-x 2 user users 3160192 Jul 31  2006 Lucie BĂ­lĂĄ - esemes.mp3
drwxr-xr-x 1 user users    8192 Mar 28  2008 ĂplnÄahĂĄ

How to enter that directory (second entry)? Or (better) rename it without using mc or other tool or having to remount the partition?

  • how about either of these cd pln mv pln /newpath/newname – David Houde Apr 5 '13 at 11:46
  • 1
    Double quotes. cd "ĂplnÄahĂĄ" . Doesn't this work :( – Drt Apr 5 '13 at 12:01
3

Make sure it really isn't UTF-8.

echo $LANG; if the returned string didn't end in .UTF-8, your terminal isn't set to the right encoding.

Tab or wildcard completion, where possible.
If the first character or three is ASCII, try to use tab completion to do the job for you. The remote server will supply the raw bytes, at which point you can just hit return even if it doesn't look like it makes sense.

Obviously tab completion doesn't apply in this case, but as David pointed out you may be able to use a wildcard cd based on a substring.

Use abstraction.
If you can't transmit the right character codes, not even the first character, you pretty much have to use other tools to solve the problem for you, such as coaxing the find command to identify only the directory in question and perform a rename on it.

  1. Use ls -i in the parent directory to identify the associated inode.
  2. Execute find . -type d -inum inode#, replacing the inode number as appropriate. If this command works and only returns a single directory (the one you want to rename), then append this to the end of the find command: -okdir mv {} ILikeThisNameBetter \;

(-okdir is the variant of -execdir that prompts for whether or not you really want to do something, which is the right way to go in this case)

Credit to Aaron Bush for the inode approach, I was doing this by file size in a prior revision.

  • Original answer suffered from PEBKAC, UTF-8 was already ruled out. Fixed. – Andrew B Apr 4 '13 at 23:40
  • 2
    1. Use ls -i to list the directory or files inode number. 2. Next use the inode number from step #1 with finds -inum switch to find the exact directory or file in question. 3. Continue with the method that Andrew B proposed with -okdir mv ... – Aaron Bush Apr 5 '13 at 12:23
  • @Aaron That's a much better method. I've incorporated it into the answer, thank you! – Andrew B Apr 5 '13 at 13:11
  • -inode argument doesn't work to me, I find it's -inum in my documentation. So I can get only the directory by find . -type d -inum 1763 but it prints an error with -okdir find: The current directory is included in the PATH environment variable, which is insecure in combination with the -okdir action of find. Please remove the current directory from your $PATH (that is, remove "." or leading or trailing colons) – Buksy Apr 5 '13 at 15:23
  • 1
    @Buksy -inode was a mental slip, sorry about that. Since you're sure that this command is only returning the inode you want to rename, it should be safe in this particular instance to replace -okdir with -ok. The error you're getting pops up when using -okdir or -execdir if I'm not mistaken; you can read about this in the find manpage. If that still doesn't work, remove this directory from your PATH temporarily. – Andrew B Apr 5 '13 at 15:49
1

Double quotes for the directory name should work fine.

 cd "ĂplnÄahĂĄ" 

even for names with spaces

 cd "ĂplnÄahĂĄ hfjksf" 
  • Even double quotes doesn't work in certain situations, however prepending a relative path (ie "./") with double quotes almost always works. For the example given in the Question this Answer does work. If in the odd case that the incorrectly rendered text comes back as a dash, most shells would interpret it as a option instead of the directory argument. – Chris S Apr 5 '13 at 13:14
  • @Chris Huh. So even if you're configured for the wrong encoding and the bytes are being rendered incorrectly on your terminal, the remote end will still know what to do? The title did say it wasn't UTF-8 for sure, so I guess it's true the edge case of improperly ordered continuation bytes doesn't apply here... – Andrew B Apr 5 '13 at 13:18
  • cd ./"ĂplnÄnahĂĄ" returns -bash: cd: ./ĂplnÄnahĂĄ: Invalid or incomplete multibyte or wide character` – Buksy Apr 5 '13 at 15:07
  • It might work if you set your terminal to an encoding that supports the 8th bit without multibyte characters, like iso8859-1 (Latin-1). It's trying to keep you from generating a bogus UTF-8 string, but in this case the damage is already done. I know this answer is more appealing than mine due to its apparent simplicity, but byte encodings are not simple things and I was trying to avoid this for a reason. – Andrew B Apr 5 '13 at 16:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.