CLI challence: handle weird filenames without the use of copy-paste

These days, being in a lab abroad, one situation I commonly face is handling complex filenames in the terminal. For many european languages, one could in these cases just spend a few moments finding the right accent key on the keyboard, and remember it for later how to type this key. But with the filenames I face here it’s often not appearant how I input a certain letter, or even worse, the filename is a victim of mojibake .

So I ask the planet, what can I do about this? I’d rather not have to reach for the copypasta, and in some cases even that does not help (messed up filename triggers tab-completion/shell bugs). Worst case is having to pull up konq/dolphin and actually gui-rename the file.

Here’s an archive with three weird filenames for your enjoyment.

http://dl.getdropbox.com/u/133095/filenames.tar.gz

Solutions in the comments.

9 Responses to “CLI challence: handle weird filenames without the use of copy-paste”

  1. Kevin Kofler Says:

    Some of the mojibake can be fixed by convmv.

  2. Jesús Guerrero Says:

    I am not sure if these will work with all the possible names. However, they seem to handle just fine the names in your tarball. Some possible solutions:

    – Try another shell, autocompletion for zsh (or whas it ksh) works differently than the one in bash
    – mc
    – a simple oneliner that adds kind of an index in front of the file name, something like

    j=1; for i in *; do echo mv “$i” “$(printf %.2i $j)_$i”; j=$((j+1)); done

    Remove the “echo” if you like the way it works to do the real thing. You can alias it, and that way at least you don’t have to depend on a file manager to rename your files and be able to reference them. This is the solution I usually use.

  3. Anarky Says:

    I tend to use “*” or “?” in most case and come up with a pattern that matches only one file name but uses only ascii characters.

  4. Karl Says:

    I found this tip from http://www.shell-fu.org/lister.php?top , perhaps it’ll work for you as well?🙂

    Karl

    To delete a file who’s file name is a pain to define (eg. ^H^H^H) find it’s inode number with the command “ls -il”. Use the line below to find and delete the file.

    find . -inum 12345 | xargs rm

  5. Luke Plant Says:

    In Bash, use ‘menu-complete’ in combination with tab completion – stick this in your ~/.inputrc

    Control-o: menu-complete

    Now tab will still do normal completion as far as it can, control-o will cycle through remaining options. This allows to get to the file you want without having to type funny chars.

  6. metellius Says:

    @Luke!
    WOW, I’ve been googling for so long for a tip just like that, had given up looking for it.

  7. lendulado Says:

    You can also try detox: http://detox.sourceforge.net/

  8. Luke Plant Says:

    @metellius: a pleasure🙂

    I also find this in .inputrc to be a invaluable:

    “\e[A”: history-search-backward
    “\e[B”: history-search-forward

    It makes up and down keys do history searches e.g. type “sudo ” then [up] and it will search through everything in history that starts with “sudo ” — a lot more useful than the default ‘up key’ behaviour.

  9. hlovdal Says:

    For xargs you most certainly want to use 0-terminated strings:

    find . -inum 12345 -print0 | xargs -0 rm

    File names in these cases might accidentally contain a newline.

Comments are closed.


%d bloggers like this: