Globbling files in the direct subdirectory of the current directory

I want to list files that end with .c in the direct subdirectory of the current directory. I have tried the following command:

find ./ -mindepth 2 -maxdepth 2 -name "*.c"

Is that right? Or is there any easier way to handle that problem?

Another problem is that I want to grep in a file to find how many lines that don't contain the pattern 'abc'

egrep -c -v "abc" myfile.txt

Is that right?

Thanks in advance.

Both approaches are right.

You can also use ls command to list .c files in all sub-directories:

ls -1 */*.c
1 Like

The shell can also do this directly without calling find or ls:

for FILE in */*.c
do
   [ -f "$FILE" ] || continue
   echo "$FILE"
   : process the file(s)
done

Also, if you are only looking for fixed strings fgrep will be faster than egrep.

1 Like

Can I understand the code in the way that the first star () in */.c refers to all the direct subdirectories in the current directory?
Thanks!

Yes, but beware if you have a large number of files that match.
The ls solution will fail with an "argument list is to long" error. The for solution is immune to this issue.

1 Like

Excellent! Thank you so much!