How to change configuration of detox (dash instead underscore)

command linefilenamesrenamescripting

I found, best to this moment, a solution when changing all those annoying filenames in large quantities, making them readable and easier to manipulate with in command-line.

So I found, among many commands, a little piece of software called detox. By default, it replaces spaces with _. Reading through manpages didn't gave me an anwser how to make detox rename files replacing spaces with -, instead.

[/] cd test
one five/  one four/  one one/  one three/  one two/
[test] detox *
[test] l
one_five/  one_four/  one_one/  one_three/  one_two/
[test] 

How to do that?

I can't find .detoxrc file (or any of files related to this program) and if I create it, I don't know what to put in it.

P.S. Is there an alternative to detox?

Best Answer

It doesn't seem that detox has an option for that. It should be fairly simple to modify the source code to add a filter with your desired output (a small modification of the safe filter; don't forget to ensure that any leading - gets removed).

You could postprocess the result of detox, or use other tools altogether. There are many file renaming tools that are more flexible.

The Perl rename command (not to be confused with the util-linux rename command) supports transforming file names with arbitrary Perl code. This command is available as rename on Debian and derivatives (Ubuntu, Mint, …). It's available on Arch as perl-rename. If you just want to change _ into - and strip leading -, you can use:

rename 's/_/-/g; s/\A-*//' *

This only affects files (except dot files) in the current directory. To act on a directory recursively, combine this with the find command.

find . -depth -exec rename 's/_/-/g; s/\A-*//' {} +

Other features of detox can be expressed in Perl, most of them with the s/PATTERN/REPLACEMENT/ operator. For example, to retain only letters and digits and replace any sequence of other characters with -, you can use

rename 's/[^[:alnum:]]+/-/g; s/\A-//' …

If you want to approximate Unicode characters with ASCII, you can use Text::Unidecode:

perl -C -MText::Unidecode /usr/bin/rename '$_ = unidecode($_)' …

Another powerful renaming tool is the zmv command from the zsh shell. First run this (put it in your ~/.zshrc for interactive use):

autoload -U zmv

To change _ into - and strip leading -, you can use:

zmv '**/*' '$f:h/${${f##*/-#}//_/-}'

The pattern **/* makes this command act in subdirectories of the current directory recursively.

Related Question