Re: OT: shell scripts and spaces in file/folder names
On Thu, 2003-04-17 at 14:16, Joris Huizer wrote:
> Hello everybody,
>
> I'm trying to learn to write shell scripts... but I
> have a problem: I've got some files which have spaces
> in their names (yeah I know it's ugly... but it'll be
> some searching to find those)
>
> Those files kill many of the shell scripts - some of
> them go through all files in the given directory,
> like:
>
> for i in `ls .`
> do
> #...
> done
>
> Can anybody tell me how I can make this work even
> though I have filenames with spaces ?
Yet another method:
$ touch "foo bar"
$ touch "foo snafu"
$ for f in "`ls -1 | grep ' '`";
> do
> echo "$f"
> done
foo bar
foo snafu
Note the *2* pair of double quotes, and the pair of "back quotes".
However, you should learn perl or python. I really like python, but
perl is ubiquitous...
Here's a script I use to change files with spaces to dashes.
#!/usr/bin/perl
use warnings;
use strict;
my $fn;
my $nn;
chdir ("/dir/with/spaces");
opendir(DIRHANDLE,".");
while ($fn = readdir(DIRHANDLE))
{
if ($fn =~ / /)
{
$nn = $fn;
$nn =~ s/ /-/g;
rename("$fn", $nn);
}
}
closedir(DIRHANDLE);
Gurus can do it in far fewer, but more cryptic lines, but I like
this way.
--
+-----------------------------------------------------------+
| Ron Johnson, Jr. Home: ron.l.johnson@cox.net |
| Jefferson, LA USA http://members.cox.net/ron.l.johnson |
| |
| An ad currently being run by the NEA (the US's biggest |
| public school TEACHERS UNION) asks a teenager if he can |
| find sodium and *chloride* in the periodic table of the |
| elements. |
| And they wonder why people think public schools suck... |
+-----------------------------------------------------------+
Reply to: