searching files with locate, find and grep

September 18th, 2006 mysurface Posted in Common, find, grep, locate, Text Manipulation | Hits: 38970 | 2 Comments »

I am here to show you 3 ways to search for files in linux.

locate is the most easilest way to search for files.

To search for a file with filename hello.c

locate hello.c

You need to update the database in order to search for files completely, like this

updatedb

For security purpose, sometimes, it is not recommended to updatedb using root privilege, but without root privilege sometimes is useless to search for certain binaries and configuration files which own by root.

Therefore, we use find. Find is a very powerful tools, I am here to give you a simple example. Let say you want to search for a file call smb.conf and you know its around under sub folder of /etc, you can do this.

find /etc -name smb.conf

If you are not sure where is the file located, you can do this

find / -name smb.conf

Again, this takes a bit longer time, because it scan through every folder and subfolder.

By the way -name is not case sensitive, in case you want to specified the files with case sensitive name, you can change -name to -iname.

find /etc -iname smb.conf

You can specified the file type too, to make your search faster. With using -type, d for directory and l for symlinks.

find /etc -type d -name samba

Check man and search for -type for more info.

Some times, you do not sure the file name, but you somehow know the contents of the file you wants to search, then you can use grep for that.

Let say you want to search for a file that have keyword “PermitRoot” somewhere in /etc, you can do this

grep -r "PermitRoot" /etc

Specified -r to recursively scan for files in sub folders of /etc if any exist.

By default, grep search for keywords with case sensitive, specified -i to ignore case.

grep -ri "permitroot" /etc

Bare in mind, ignore case tooks more time to process.

2 Responses to “searching files with locate, find and grep”

  1. Good post just thought I would let you know that find -name is case sensitive and if you change it to find -iname it makes it insensitive. In other words you got it around the wrong way.

  2. Thanks, that was informative. I learned some nice new tricks.

    FYI you should write ‘bear in mind’ (to keep, retain), and not ‘bare’ (to reveal).

Leave a Reply