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

From NovaOrdis Knowledge Base
Jump to: navigation, search
(Created page with "=Internal= * bash =Overview= To list files: local dir=... for f in ${dir}/*; do [[ -f ${f} ]] && echo -n "$(basename ${f}) " done To...")
 
Line 7: Line 7:
 
To list files:
 
To list files:
  
local dir=...
+
<syntaxhighlight lang='bash'>
 +
local dir=...
  
for f in ${dir}/*; do
+
for f in ${dir}/*; do
  [[ -f ${f} ]] && echo -n "$(basename ${f}) "
+
  [[ -f ${f} ]] && echo -n "$(basename ${f}) "
  done
+
done
 +
</syntaxhighlight>
  
 
To list directories, replace -f with -d.
 
To list directories, replace -f with -d.

Revision as of 20:16, 20 September 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.