How to find all files with the same extension within a directory

I want to search for all .conf files in /etc/.

I tried grep -r *.conf /etc, but the result isn’t right.

What am I doing wrong?

Here is Solutions:

We have many solutions to this problem, But we recommend you to use the first solution because it is tested & true solution that will 100% work for you.

Solution 1

Just press Ctrl+Alt+T on your keyboard to open Terminal. When it opens, run the command below:

find . -type f -name "*.txt"

This will list all files with the extension .txt.

The . at the start denotes the current directory. find searches recursively in all the directories below the given path. If you want the search to start somewhere other than the current working directory, specify the path, for example:

find /etc -type f -name "*.conf"

This searches the /etc directory and all its subdirectories for regular files with the .conf extension.

Solution 2

I’d personally use find, but you can glob for these things too:

shopt -s globstar
ls /etc/{,**/}*.conf

And you can use locate and it’s fast but not reliable.

locate '/etc/**.conf'

Solution 3

grep searches the contents of files, not the file names.

To find all .conf files in /etc/ you’ll want find:

find /etc -name "*.conf"

Solution 4

The find command is slow, use this command will give you result immediately:

locate "/etc/*.conf"

More info about locate command (in mlocate package) can be found here: https://medium.com/@thucnc/the-fastest-way-to-find-files-by-filename-mlocate-locate-commands-55bf40b297ab

Solution 5

The following command will return all files and folders ending in .conf:

ls -lR /etc | grep ".conf$"

To find only files, run:

find /etc -type f -name '*.conf'

Note: Use and implement solution 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply