Globbing for only files in Bash

Viewed 13169

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.

4 Answers

My go to in this scenario is to use the find command. I just had to use it, to find/replace dozens of instances in a given directory. I'm sure there are many other ways of skinning this cat, but the pure for example above, isn't recursive.

for file in $( find path/to/dir -type f -name '*.js' );
    do sed -ie 's#FIND#REPLACEMENT#g' "$file";
    done

Related