As soon as I touch a new file, my ls command with grep stops working

I’m trying to find all the files in my home directory with a specific extension. If I only have one instance of the file extension, the ls command works perfectly fine, but as soon as I touch a new file with the same extension the ls command returns nothing.

I’m really not sure what’s going on, and if someone could help point me to the right direction, that would be great.

enter image description here

Asked By: Luna


Always quote your regex in single quotes, because the shell expand the wildcard to files.


ls -1 | grep '.java$'

Removed the wild card that is not necessary.

But parsing ls output is not a good idea, I would recommend find:

find . -name '*.java'

to search recursively, or

find . ! -name . -prune -name '*.java'

Or with GNU find or compatible:

find . -name '*.java' -mindepth 1 -maxdepth 1

to search only the current directory.

In this particular case, since the only file at depth 0 is . (the current working directory where we instruct find to start looking for files) which doesn’t match the *.java pattern, you can get aways with omitting the -mindepth 1.

Answered By: Gilles Quénot
Categories: Answers Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.