Re: How do I get a list of filenames with spaces on bash?
- From: Ed Morton <morton@xxxxxxxxxxxxxx>
- Date: Thu, 24 Jan 2008 11:04:29 -0600
On 1/24/2008 10:32 AM, Stephane Chazelas wrote:
On Thu, 24 Jan 2008 09:00:27 -0600, Ed Morton wrote:
[...]
for f in *\ *
do
n="${f// /_}"
[ ! -f "$n" ] &&
[ -f ... ] test whether the file exists *and* is a regular file
(or symlink to regular file). What if it exists and is a
directory?
The OP said "I was trying to rename all the filenames with spaces in them." and
I took him literally. If you look at the list of dirs he got with his echo
command earlier in the thread you'll see that while some have very interesting
names, none contain spaces or underscores so they won't clash with the new file
names all of which will contain underscores.
cp -- "$f" "$n" &&
rm -- "$f"
done
[...]
Why not mv? cp won't work great for non-regular files.
To ensure that in the event of a failure the original file is unaffected.
A better way to avoid overwriting files is to use the -i option
of cp/mv. See also (set -C; cat < in > out)
"cp -i" can still fail if the target file is unwritable and any use of "-i"
means you have to sit around while the scripts running which may not be desirable.
Ed.
.
- Follow-Ups:
- Re: How do I get a list of filenames with spaces on bash?
- From: Stephane Chazelas
- Re: How do I get a list of filenames with spaces on bash?
- References:
- Re: How do I get a list of filenames with spaces on bash?
- From: jellybean stonerfish
- Re: How do I get a list of filenames with spaces on bash?
- From: Chad
- Re: How do I get a list of filenames with spaces on bash?
- From: Ed Morton
- Re: How do I get a list of filenames with spaces on bash?
- From: Stephane Chazelas
- Re: How do I get a list of filenames with spaces on bash?
- Prev by Date: Re: How do I get a list of filenames with spaces on bash?
- Next by Date: Re: How do I get a list of filenames with spaces on bash?
- Previous by thread: Re: How do I get a list of filenames with spaces on bash?
- Next by thread: Re: How do I get a list of filenames with spaces on bash?
- Index(es):
Relevant Pages
|