[PLUG] Mass Removal of White Space in File Names

Michael Rasmussen michael at jamhome.us
Mon Apr 18 02:27:36 UTC 2011


On Mon, Apr 18, 2011 at 02:12:10AM +0000, mhewan1 at comcast.net wrote:
> 
> ----- Original Message -----
> From: "Rogan Creswick" <creswick at gmail.com>
> To: "Rich Shepard" <rshepard at appl-ecosys.com>, "General Linux/UNIX discussion and help, civil and on-topic" <plug at lists.pdxlinux.org>
> Cc: plug at pdxlinux.org
> Sent: Sunday, April 17, 2011 5:00:52 PM
> Subject: Re: [PLUG] Mass Removal of White Space in File Names
> 
> On Sun, Apr 17, 2011 at 4:44 PM, Rich Shepard <rshepard at appl-ecosys.com> wrote:
> >   I have two directories of images where each file name has spaces between
> > the words. I'm not having success writing a shell script that takes each
> > name in sequence, removes the white spaces, and leaves the extension as-is.
> > I'm certain that this can be done in perl, python, ruby, scheme, awk, sed,
> > and so on. I believe that a shell script would be a one-liner.
> 
> There is as perl script called 'rename' that makes this very easy:
> 
> $ rename 's/ //g' Some\ File\ Name.ext
> 
> Will take the spaces out (destructively, so be careful.  I don't know
> how it handles filename conflicts that may arise, I suspect not
> gracefully.).  Combined with find and xargs, you can apply it to a
> whole directory tree (I haven't tested the following line though, so
> beware of typos/etc..):
> 
> $ find <path> -type f | xargs rename 's/ //g'

if you're going to go that route:
    it's safer to replace the spaces with underlines so 's/ /_/g' 
    the find/xargs will still choke on files with spaces in the names, 
    however the syntax to work with that is find ... -print0 | xargs -0 which then uses NULL as the file name delimiter
    so:  find <path> -type f -print0 | xargs -0 rename 's/ /_/g'

    add -v to rename so it will print out the names of the files it changes
    use -n to not actually rename, but to show what it would have done

> On debian my debian system, 'rename' is a symlink to
> /etc/alternatives/rename, which points to /usr/bin/prename, which
> apt-file says came with perl.  If you don't have 'rename', try
> 'prename'.
> 
> There are also probably other programs called rename.  This is what I
> get if I run 'rename' with no arguments:
> 
>  $ rename
> Usage: rename [-v] [-n] [-f] perlexpr [filenames]
> 
> --Rogan
> 
> 
> 
> Be careful, rename on Ubuntu is different that that on OpenSuSE.  The command line above with an expression is an OpenSuSE style command.  Also be aware of spaces in directories that are parents of the target file.  I'll post a perl script I wrote to deal with this as soon as I am in front of that machine.
> 
> _______________________________________________
> PLUG mailing list
> PLUG at lists.pdxlinux.org
> http://lists.pdxlinux.org/mailman/listinfo/plug

-- 
      Michael Rasmussen, Portland Oregon  
  Trading kilograms for kilometers since 2003
    Be appropriate && Follow your curiosity
          http://www.jamhome.us/
The Fortune Cookie Fortune today is:
The World Watch just released a report that showed that they 
thought animal agriculture was responsible for 18 percent of 
greenhouse gases, but it turns out it's 51 percent.
    ~ Jonathan Foer



More information about the PLUG mailing list