bashglob

Globbing for only files in Bash


I'm having a bit of trouble with globs in Bash. For example:

echo *

This prints out all of the files and folders in the current directory. e.g. (file1 file2 folder1 folder2)

echo */

This prints out all of the folders with a / after the name. e.g. (folder1/ folder2/)

How can I glob for just the files? e.g. (file1 file2)

I know it could be done by parsing ls but also know that it is a bad idea. I tried using extended blobbing but couldn't get that to work either.


Solution

  • WIthout using any external utility you can try for loop with glob support:

    for i in *; do [ -f "$i" ] && echo "$i"; done