Skip to main content
added 537 characters in body
Source Link
Hauke Laging
  • 94.6k
  • 21
  • 132
  • 185
for file in *; do
  echo "$file"
done

This usually does not match "hidden" files (names beginning with a dot) though.

For matching the hidden files, too, you need this before:

shopt -s dotglob

If you want to skip directories (and probably anything "strange") then you have to detect them in the loop; pattern matching (the *) doesn't care about the type of object:

for file in *; do
  test -f "$file" || continue
  echo "$file"
done

Symbolic links are a special case. They are considered if they link to a file but the file may be in another directory. To ignore symlinks:

for file in *; do
  test -f "$file" || continue
  test -L "$file" && continue
  echo "$file"
done
for file in *; do
  echo "$file"
done

This usually does not match "hidden" files (names beginning with a dot) though.

For matching the hidden files, too, you need this before:

shopt -s dotglob
for file in *; do
  echo "$file"
done

This usually does not match "hidden" files (names beginning with a dot) though.

For matching the hidden files, too, you need this before:

shopt -s dotglob

If you want to skip directories (and probably anything "strange") then you have to detect them in the loop; pattern matching (the *) doesn't care about the type of object:

for file in *; do
  test -f "$file" || continue
  echo "$file"
done

Symbolic links are a special case. They are considered if they link to a file but the file may be in another directory. To ignore symlinks:

for file in *; do
  test -f "$file" || continue
  test -L "$file" && continue
  echo "$file"
done
Source Link
Hauke Laging
  • 94.6k
  • 21
  • 132
  • 185

for file in *; do
  echo "$file"
done

This usually does not match "hidden" files (names beginning with a dot) though.

For matching the hidden files, too, you need this before:

shopt -s dotglob