In my personal opinion (which is much like a butt, everyone has one), piping
ls -l through
grep, for this case, is kinda like buying a full nice suit just to get the tie.
To get files/dirs separately:
Code:
$searchdir="."
find "$searchdir" -type f -maxdepth 1 # files
find "$searchdir" -type d -maxdepth 1 # dirs
The beauty of
find is that it provides a full path, relative to the search path. If that's not your bag, and all you need is the filename, then you can strip it using
basename:
Code:
$searchdir="."
find "$searchdir" -type f -maxdepth 1 # files -exec basename '{}' ';'
find "$searchdir" -type d -maxdepth 1 # dirs -exec basename '{}' ';'
If any of the files or dirs will have spaces in the name, you'll need to adjust the value of IFS, to remove the space as a field separator.
Here is another thread with information about using IFS