116

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?

183

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.

| improve this answer | |
  • 1
    It works. Are there any alternatives for this command? – Adrian George Aug 17 '13 at 10:56
  • 1
    You can take a look at Locate. – Mitch Aug 17 '13 at 11:07
  • 1
    @AdrianGeorge find is so widely used, so well documented, and has so many StackOverflow/Exchange posts about it that it'd be harder to find something easier to work with. Just about anything you'd want to do has been written up online somewhere. – BallpointBen May 9 '18 at 14:59
  • fd is faster and simpler to use.github.com/sharkdp/fd – Anton Oct 1 at 11:10
9

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'
| improve this answer | |
  • Why isn't locate reliable? – Giraldi Sep 6 '17 at 2:30
  • 1
    @Giraldi locate relies on the mlocate database to have been updated recently to be accurate. By default that is only scheduled to update once a day (though you can force it manually with updatedb). – Oli Sep 6 '17 at 8:42
  • @Oli not true on macs though, right? think mac os keeps mlocate updated – d8aninja Feb 21 '19 at 20:34
6

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"
| improve this answer | |
2

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

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.