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
You need to update the database in order to search for files completely, like this
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.