[nSLUG] Removing spaces from file names - note, long lines

Oliver Doepner odoepner at gmail.com
Wed Nov 12 22:22:42 AST 2014


I guess it sounds like I am tooting my own horn here, but ...

My script recursively cleans up directory and file names, byreplacing all
characters with an underscore that are not alphanumeric, underscore, dot or
dash. It also works for weird stuff like newlines, wildcards etc in
filenames. It only requires bash, find and mv.

And it never overwrites any files that happen to have the same "sanitized"
filename (using --backup=numbered).

http://odoepner.wordpress.com/2011/10/13/bash-script-to-recursively-sanitize-folder-and-file-names/

Please let me know if/how the same thing can be done in a simpler way.

Vlado's script requires perl (acceptable) but has lots of unecessarily(?)
complicated regexes.
Eugene's one-liner only removes spaces and only for files in the current
folder.
Jack's script only works for single regular files and is quite verbose,
compared to Eugene's one-liner.

Thanks
Oliver


On Wed, Nov 12, 2014 at 9:54 PM, Stephen Gregory <nslug at kernelpanic.ca>
wrote:

> On Wed, Nov 12, 2014 at 8:24 PM, Vlado Keselj <vlado at dnlp.ca> wrote:
> >
> > #!/usr/bin/perl
> > # Author: Vlado Keselj 2009-14 last update 2014-04-25
> > # Change file names to safe names (e.g., space to _)
> >
> > for my $fnold (@ARGV) {
> >     my $fnnew = $fnold;
> >
> >     local $_ = $fnnew; s/ +- +/-/g;
> >     s/''+/--/g; s/'/-/g; s/[[(<{]/_-/g; s/[])>}]/-_/g;
> >     s/[,:;]\s*/--/g; s/&/and/g; s/ /_/g;
> >     s/__+/_/g; s/---+/--/g;
> >     s/\xE2\x80\x99/-/g; # Single right quote
> >     s/(=|[^\w.-])/"=".uc unpack("H2",$1)/ge;
> >     $fnnew=$_;
>
>
> <insert joke about email corrupted by line noise here>
>
> That seems to handle most of the annoyances in filenames, but does it
> handle files that start with a [-$*] or similar special?
>
> --
> sg
> _______________________________________________
> nSLUG mailing list
> nSLUG at nslug.ns.ca
> http://nslug.ns.ca/mailman/listinfo/nslug
>



-- 
Oliver Doepner
Software Engineer
http://oliver.doepner.net/
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://nslug.ns.ca/pipermail/nslug/attachments/20141112/5ce7036d/attachment.html>


More information about the nSLUG mailing list