239

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?

Adrian George
  • 3,671
  • 8
  • 25
  • 31

5 Answers5

368

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.

Zanna
  • 72,471
Mitch
  • 109,937
  • 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
  • 2
    @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 09 '18 at 14:59
  • fd is faster and simpler to use.https://github.com/sharkdp/fd – Anton Oct 01 '20 at 11:10
  • I wonder why the -type f command is necessary? I checked the manual and I'm not sure what the advantage of using it is. Is it to omit symbolic links? I'd think folder's names usually don't have extensions (and if they have, like .git, I'd want to finde them). I looks to me, in this case, that adding -type f is overly specific. – Turtle10000 Dec 08 '20 at 11:27
  • 1
    @Turtle10000 -type f Search for files. – Mitch Dec 08 '20 at 17:56
  • 1
    @Turtle10000 looking into the man page of find with the -type the possible file types are: b block special | c character special | d directory | f regular file | l symbolic link | p FIFO | s socket – Honey Dec 18 '20 at 14:50
  • @AdrianGeorge you could sort of use tree | grep .nix but you won't get the full paths – s3c Aug 22 '24 at 20:30
12

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"
Zanna
  • 72,471
user184890
  • 121
  • 2
10

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'
Oli
  • 299,936
  • 2
    Why isn't locate reliable? – Giraldi Sep 06 '17 at 02:30
  • 2
    @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 06 '17 at 08:42
  • @Oli not true on macs though, right? think mac os keeps mlocate updated – d8aninja Feb 21 '19 at 20:34
4

How about using a simple ls command:

ls /etc/*.conf

This uses glob pattern (File name expansion), so it only finds files in immediate directory and not subfolders

hiru007
  • 45
Ivan
  • 141
0

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'
ggorlen
  • 115
  • 1
  • 8
nibble
  • 11