in reply to Unix shell ls vs readdir

my @lines = grep {/->\s+tomcat\s+/} qx{ls -l /etc/rc.d/init.d}; my @inits = map {(split)[-3]} @lines;

If /etc/rc.d/init.d contains any subdirectories then ls will also display the files from them.    Is that what you want?

If any of the file names contains spaces or tabs or newlines (or other whitespace characters) then (split)[-3] will not return the correct file name.

Replies are listed 'Best First'.
Re^2: Unix shell ls vs readdir
by jffry (Hermit) on May 11, 2010 at 03:36 UTC

    Actually, ls -l /a_dir will not list the contents of a_dir's subdirectories on any Unix flavor that I've used. Maybe you are thinking of ls -l /a_dir/* which will do exactly what you described because the shell will expand the glob "/a_dir/*" and then hand that list of arguments to ls, and, of course, when ls is handed a directory name as an argument it lists the contents of that dir.

    But yes, a total forehead slap on the situation with spaces in file names messing up my array ordering. Yet another solid reason to keep it all Perl.