Re: BASH scripting question

From: pbs (pnews_at_lomarline.freeserve.co.uk)
Date: 10/09/03


Date: Thu, 09 Oct 2003 23:04:28 +1300

NS wrote:
> I have a very simple task that eludes me.
> I have been in the process of administrating a huge amount of data on a
> pre-existing file server. The problem is that a lot of the directories and
> files have been copied from various sources and as a result have
> non-sequitor file names. eg. containing one or more spaces and ampersands.
> This obviously causes some problems.
>
> Stop me here if there is an easier way.
>
> I need to compile a list of all these files and especially directories.
>
> So in a shell script I do:
>
> directories=`find . -type d -print`
>
> The problem is that the escape characters for the spaces etc. get
> interpereted as newlines, so that when I try to reference the names from
> within the variable it breaks them.
[snip]

Spaces in names and speed came up last month on the 6th of September in
a long thread in uk.comp.os.linux called "Effective searching on *nix?".
If you use google groups look at the subthread from enty 49 {Tim Haynes}
onwards: http://short.uigui.com/?u=290

find . -type d -print0 | xargs -0 *command*

 From man find(1L):
-iname case insensitive [at some point you will need it].
-print0
               True; print the full file name on the standard out­
               put, followed by a null character. This allows
               file names that contain newlines to be correctly
               interpreted by programs that process the find out­
               put.

from man xargs(1L)
        --null, -0
               Input filenames are terminated by a null character
               instead of by whitespace, and the quotes and back­
               slash are not special (every character is taken
               literally). Disables the end of file string, which
               is treated like any other argument. Useful when
               arguments might contain white space, quote marks,
               or backslashes. The GNU find -print0 option pro­
               duces input suitable for this mode.