Re: Spaces in file names - watch for arg list too long with ls *



On Mon, 31 Dec 2007 03:31:16 -0900, Floyd L. Davidson wrote:

"Leslie Rhorer" <lrhorer@xxxxxxxxxxx> wrote:
Hey, that works! Thanks.

But it would be a lot easier to just do this:

#!/bin/bash

for file in * ; do
echo "$file"
done
exit 0

The trick is putting quotes around $file. Using `ls -1 *`
is unnecessary, and so is find.


"Dan Mills" <dmills@xxxxxxxxxxxxxxxxxxxx> wrote in message
news:477856c3$0$21094$da0feed9@xxxxxxxxxxxxxxxxx
On Sun, 30 Dec 2007 19:31:45 -0600, Leslie Rhorer wrote:

OK, here's the problem. I need to automatically manipulate the file
names
of a number of files whose filenames have spaces in them.
for file in `ls -1 *`
do
<do stuff>
done

How about something like (untested):

#!/bin/bash
IFS=$'\n'

#IFS is the internal field separator in bash and
#here we set it to be new line only.

for file in $(find . -type f)
do
<stuff with "$file">
done

HTH.
Regards, Dan.

--
Floyd L. Davidson <http://www.apaflo.com/floyd_davidson>
Ukpeagvik (Barrow, Alaska) floyd@xxxxxxxxxx

Another pitfall using the ls -1 *, if you have a large number of files in teh
output list the command may fail with an argument list too long. (but may be
OK without the astersk,) Depending on your default distribution's compilied
options this may be as few as 2000 files.

Regards,
Bart


.



Relevant Pages

  • Re: Shell Script to Remove Old Files
    ... Note it needs a TSM server: ... # Verify command line arguments ... echo "\nError: $STARTDIR does not exist.\n" ...
    (comp.unix.admin)
  • Re: Errors returned by System()function
    ... specified command succesfully and /that/ command ... For example ('echo $?' ... Seems that bash exits with a exit code of 1 when it can't find the ... php$ wget http://www.baddomain.net/index.html ...
    (comp.lang.php)
  • Re: bash (sh?) scripting question
    ... command and verify the exit code: ... contains the result of the echo command. ... for param in $* ...
    (comp.unix.shell)
  • Re: bash (sh?) scripting question
    ... I am trying to implement a type of "try" function, that will execute a command and verify the exit code: ... trap 'echo failed' ERR ... echo "Param: $param" ...
    (comp.unix.shell)
  • Re: Batch file
    ... You can use the "exit" command in a batch file if you ... @echo off ...
    (microsoft.public.win2000.general)