Re: How do I get a list of filenames with spaces on bash?





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.


.



Relevant Pages

  • Re: Spaces and underscores
    ... filenames with spaces between words in the filename. ... replace the spaces with underscores but find it will probably take me ... command to the beginning. ... used this approach many times to rename hundreds of files. ...
    (comp.os.os2.apps)
  • spaces to underscores
    ... I strongly dislike spaces in filenames and wish to have a script which ... will rename a file changing spaces to underscores. ... I'd like to ask for advice on how I could rename a file ...
    (comp.unix.shell)
  • Re: spaces to underscores
    ... I strongly dislike spaces in filenames and wish to have a script which ... will rename a file changing spaces to underscores. ... I'd like to ask for advice on how I could rename a file ...
    (comp.unix.shell)
  • Re: Gear Hobbing Attachment Pix
    ... I thought I'd post a direct link for the internet challenged. ... Also -- note that the spaces in the filenames got converted to ... underscores -- because spaces in filenames are a serious problem on ... command-line driven systems, while everyone with a semi-modern system ...
    (rec.crafts.metalworking)
  • Re: Spaces and underscores
    ... Willard Reese wrote: ... filenames with spaces between words in the filename. ... replace the spaces with underscores but find it will probably take me ... a JPG extension. ...
    (comp.os.os2.apps)