I am trying to get a list of only directory names in order to store it in a variable for a foreach for processing later on.
However, just testing the ls
first I am getting weird results.
For example:
ls -1 /var/lib/mysql/ | grep -e '^d'
db_nagiosql
db_nagiosql_v32
discount-o-matic
drupal
drupal5
However, this does what it should do:
ls -l /var/lib/mysql | grep -e '^d' | awk '{print $9}'
alex
bugs
bugtracker
bugzilla
cacti
cerb5
db_nagiosql
db_nagiosql_v32
discount-o-matic
drupal
drupal5
earth
fft
final_function_test
firm_ware
flyspray
gallery2
graphics
jon
joomla
mysql
nconf
old_fft
opendocman
oreon
part-number
phpbb
phpbugtracker
phplist
postnuke
teldir
test
testing
vanilla
vision
wikidb
wordpress
zen
The issue is, I need this to be portable(ish) so the awk part I rather not have as it may not always be the ninth column. Why does the ls -1
not work while the ls -l /var/lib/mysql | grep -e '^d' | awk '{print $9}'
does work?
grep
in the first example, if you don't only want directories whose names start withd
?^d
does that? No files. No information. Just the name of the directory regardless of name listed one per line.ls -1
with a number one does not do a long listing of the entries, so the first character being ad
only means the file/directory name starts with ad
not that it is a directory. if you did a-l
(lower caseL
) instead of1
your grep would work, but as @inukshuk's answer shows there are better ways to get just the directories^d
was doing. I forgot it was from the d in the permissions rather than directory type, thus why the long listing worked and not thels -1
, thus why talking amongst others is a great learning experience for catching such silly mistakes.