LFCS Admin Exam preparation guide series, main page can be found here.
This post is part of Essential Commands from the domain competency list for the exam. The full list can be found in the link above paragraph or the Linux Foundation page here.
We can use commands like find and locate to search for files in the Linux file system.
find – powerful command to search for files and directories. It can be used to search by name, file type, timestamp, owner, and many other attributes. Supports use of expressions, perform a search in real-time therefore can be slow on large systems.
When running find command without specifying the directory, files will be the only search in the current user home directory, we can use the full name of the file we are looking for or use wild-case if we only know part of the name. Command parameter – name is used to specify the file name.
To search files throughout all file system we need to add sudo in front of the find command, find / will search everywhere. We can use – iname to search for files case insensitive, and we can find all files even not name properly.
When selecting command parameter – type we can search for “d” directories, “l” links, “f” files.
By using the – user parameters we can search for files of which that user is the owner.
locate – is faster than the find command, but have limited search options and cannot search by attributes or metadata. By default, the search is to the full system and relies on a database that has to be refresh regularly.
When we try to run locate command after the file was created, it is possible that the command will return nothing. We need to run sudo updatedb to refresh the database and be able to find the file we are searching for.
locate command has much fewer parameters than find command, but as well allows us to search for files case insensitive when add -i to the command.
There are other commands which will be helpful for looking for information about commands.
which – returns the location of the command base on the PATH settings
whereis – returns the location of the binary, source file, and man page, it can return multiple versions of command if they exist.
type – returns information about the command type and details based on how the command is related to the shell configuration.
Thank you for reading.