Difference between revisions of "Bash Listing Files in a Directory"

From NovaOrdis Knowledge Base
Jump to: navigation, search
(Overview)
(2 intermediate revisions by the same user not shown)
Line 16: Line 16:
  
 
To list directories, replace -f with -d.
 
To list directories, replace -f with -d.
 +
 +
<font color=darkgray>
 +
'''find''': To further research, it seems the following approach does not work because if there is more than one directory, the first iteration assigns a multi-line to d:
 +
<syntaxhighlight lang='bash'>
 +
for d in $(find ${dir} -name "*-something" -type d); do
 +
  debug "d: ${d}"
 +
  ...
 +
done
 +
</syntaxhighlight>
 +
</font>

Revision as of 00:26, 4 December 2019

Internal

Overview

To list files:

local dir=...

for f in ${dir}/*; do
  [[ -f ${f} ]] && echo -n "$(basename ${f}) "
done

To list directories, replace -f with -d.

find: To further research, it seems the following approach does not work because if there is more than one directory, the first iteration assigns a multi-line to d:

for d in $(find ${dir} -name "*-something" -type d); do
  debug "d: ${d}"
  ...
done