Re: Bash globbing files only?

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Tue, 30 Jan 2007, Jacques B. wrote:

>   files=
>   for f in *
>   do [ -f "$f" ] && files="$files $f"
>   done
>   ... do stuff with $files ...
>
> If you've got to deal with filenames with whitespace in them you need
> to be trickier.

I like how this works.  But you are right, it does cause a problem for
filenames with spaces.  As far as cpu time (preceding the command with
"time" to get same), we are only talking a few 1/100th of a second
difference with the find command.  The find command is more robust as
it will properly deal with filenames with spaces.  Not to mention find
will also yield hidden files (i.e. .file) whereas the above won't.
All depends on your needs as to which will serve you better.


Note, that find also solves another problem: namely, that if the globbing generates a line length beyond the max for bash (in either the for or the concatenation), then the script will fail. This won't happen with find.

Steve Friedman


[Index of Archives]     [Current Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Yosemite News]     [Yosemite Photos]     [KDE Users]     [Fedora Tools]     [Fedora Docs]

  Powered by Linux