[PLUG] Mass Removal of White Space in File Names

mhewan1 at comcast.net mhewan1 at comcast.net
Mon Apr 18 02:12:10 UTC 2011


----- 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'

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.




More information about the PLUG mailing list