From: dalgoda@ix.netcom.com (Mike Castle)
To: linux-newbie@vger.kernel.org
Subject: Re: File names with spaces
Date: Fri, 28 Feb 2003 15:46:53 -0800 [thread overview]
Message-ID: <d095jxhei.ln2@thune.mrc-home.org> (raw)
In-Reply-To: 20030218093623.1FB578D8FE@basicbox3.server-home.net
In article <20030218093623.1FB578D8FE@basicbox3.server-home.net>,
<robin@robind.de> wrote:
>for file in `ls -1`; do
> newfile=`echo "$file" | sed 's/ /_/'`
> echo "File is named ${file}"
> echo "The new file is named ${newfile}"
>
> mv "$file" "$newfile"
>done
>
>IHMO in the main-loop it is better to choose "ls -1", so the field
>separator is \n and there's only one filename in each line.
ls should detect that stdout is not a terminal and fall back to -1
automatically. Granted, it can't hurt, but should be unnecessary.
However, a few comments.
First, why use ls at all? Might as well use:
for file in *; do
Of course, since you are only interested in files with spaces in them,
while not limit to that in the first place?
mcastle@dl-mcastle[03:37pm]~/foo(776) for file in *; do echo $file; done
a b
bar
mcastle@dl-mcastle[03:37pm]~/foo(777) for file in *\ *; do echo $file; done
a b
Of course, if you have a lot of files, this technique simply doesn't work
as you'll overflow your max command line length.
You could go back to:
ls | while read; do
But you're processing every file again.
Another alternative might be something like:
find -name '* *' -maxdepth 1
I think maxdepth is gnu find specific, so keep that in mind (I don't have
access to any non-gnu systems to test).
>The next <big> thing is to put the filename into quotations. Now a
>filename, even with spaces, will be interpreted as one word.
Not just quotes, but prepend ./ too, in case any of your file names look
like "- -"
mcastle@dl-mcastle[03:45pm]~/foo(793) find
.
./bar
./a b
./blah
./blah/c d
./ns
./- -
mcastle@dl-mcastle[03:45pm]~/foo(794) ./ns
`././a b' -> `././a_b'
`././- -' -> `././-_-'
mcastle@dl-mcastle[03:45pm]~/foo(795) cat ns
#!/bin/bash
find -name '* *' -maxdepth 1 | while read name; do
newname=$(echo $name | tr ' ' '_')
mv -iv "./$name" ./$newname
done
Heh... forgot that find will already add in the ./ ... clean up as
appropriate.
mrc
--
Mike Castle dalgoda@ix.netcom.com www.netcom.com/~dalgoda/
We are all of us living in the shadow of Manhattan. -- Watchmen
fatal ("You are in a maze of twisty compiler features, all different"); -- gcc
-
To unsubscribe from this list: send the line "unsubscribe linux-newbie" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.linux-learn.org/faqs
next prev parent reply other threads:[~2003-02-28 23:46 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2003-02-18 9:36 File names with spaces robin
2003-02-18 13:59 ` Jim Reimer
2003-02-28 23:46 ` Mike Castle [this message]
-- strict thread matches above, loose matches on Subject: below --
2003-02-17 19:11 Theo. Sean Schulze
2003-02-17 20:34 ` Brian Jackson
2003-02-18 21:37 ` Theo. Sean Schulze
2003-02-20 10:22 ` J.
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=d095jxhei.ln2@thune.mrc-home.org \
--to=dalgoda@ix.netcom.com \
--cc=linux-newbie@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox