There are so many files and directories that are located in your system by default, but most times, we limit ourselves to the few we can work with and feel not to bother ourselves with other system files. The truth is if you are running a big data center running Linux servers, you will need to search deeper into the system to get some files or directories to keep your work going. Most of the files are systems files, while others could be files you have stored in the system, which might be needed at a particular time. This calls for looking for a command to search through the system to locate your files. Unlike the Windows operating system, where you can quickly click on the Windows icon to search for your files, the Linux command line works differently; it requires commands to carry out its search operation.
There are several commands available for Linux administrators for searching files that match certain criteria on the file system. In this article, we will be discussing a few of them in details
HOW TO SEARCH FILES USING THE FIND COMMAND
The find command provides a real-time search on all files in the local file system to search for files that match the specified arguments. The find command without any argument specified will search for files in the current directory as well as all sub-directories in the current directory. For a find command to be able to return all search command successfully on a specified directory as an argument, the user who executes the search command must have the read permission on such a file else the command will skip those files which the user has no read permission.
The search command can accept the name, ownership, time, date, etc. as an argument to perform the search criteria. The first argument must be the directory to search, and if the directory is omitted, the system assumes the search to be on the current directory.
The first simple step is to run the search command without any argument to search the entire current directory
To search the entire directory tree and its subdirectories, you run the find command followed by the forward-slash, which signifies the roots directory.
You can also specify the directory you want the find command to perform its operation on as seen below
They are lots of options available while using the find command to search for any specific file. Searches can be based on the filename, file size, timestamp, and many more. We will be practicalizing on a few of the options available. As we might not be able to cover all the options, it is best you look up the manual page of the find command to get the list of all available options.
We can kick-off by knowing how to search for files based on the filenames. Please note that you can combined options together to carry out the search operations.
To search for files by name, you use the –name option followed by the name of the file to search as an argument. The find command will search for files that match the file name specified and return the result.
Now let's tell the find command to search the entire directory tree for a file called NFS and return the result to the screen
Using the –name option can only search for all files with the specified name. What if I want to search for files that have a certain word at the end or beginning or even in the middle. That’s where the wild cards come into play. The wild card will search for a file name and return all results that are a partial match.
Please note that while using the wild cards to search for files, it is essential that the file names are quoted to prevent the terminal from interpreting the wild cards differently.
The instruction is to make the find command look up the entire directory tree and return all values that have “messages” at the end.
Another way with the wild card character is to specify it to returns all value with “messages” anywhere in their names
Another option while searching for files using the name is the –iname option. The –iname option performs a case-sensitive search on the specified directory.
Let’s use our file “messages” to run a find command search criteria to display case-sensitive values on the terminal
Let's look at one more option, which I think could be complicated for newbies, and then you can choose to practice other options in your leisure time. The next is to search for a file based on permission.
To search for files based on permission, you need the –perm option. The permissions are described in the octal values 4,2,1 with the corresponding values as read, write, and execute, respectively. There can be proceeded with either the – or / sign.
The permission followed by the forward-slash means to search for files with a least a bit of permission for the user, group, or others. That’s if I search for a file with /444 permission, the file with –wx-wx-wx won't be displayed.
When the – sign is preceded with the permission, it means to find all files with exact permissions set in all three bits. That’s to say; if I try finding a file with -222, I should be expecting the result of a file with w- -w- - w- - or rw-rw-rw-. In any case, it means there should be a write permission set in all. So I shouldn’t be expecting file with rw-r-x-wx
Let’s give one a try by searching a file from the entire directory tree with permission 645 by proceeding it with a forward-slash.
Specifying the permission without specifying either the – sign or the forward-slash sign will display all files with the specified permissions.
First, let's print out files with the exact permission of 754 starting from the directory tree
No such file found
Next, let's print out file using the forward slash
To print out using the – sign
There are many of these options available on the man page of the find. You can search for a file based on the time or based on ownership and many more. Go through the manual page to get the option that fits your need
HOW TO SEARCH FOR FILES USING LOCATE COMMAND
The locate command is used to search for files based on file name or path from a database. It is quite faster than the find command because its searches are based on a previously built database. The locate command searches the database for its file path or file names and return the result to the user. The database is updated automatically by the cron service, but you can as well choose to update it by running updatedb command on the command line
For any user to be able to execute a search command on a directory tree, the user must be able to have the read permissions on the directory. If no read permissions are set for the user who executes the command, the command will only display contents which the user has read permission.
The basic form of locate command is just typing the locate command on the terminal, but there are various other options to use with the locate command. Unlike the find command, the locate command requires a path or filename to search as an argument. A simple path could be a dot that signifies the current directory.
Alternatively, you can search for a file as follows
Now lets kick-off learning a few options you can use with the locate command. The – i option will perform a case-sensitive search on the argument provided
You can also limit the number of output when using the locate command by specifying the –n option followed by a number
HOW TO USE WHICH COMMAND TO SEARCH FOR FILES
The which command is used to search for the path of a file or an executable. It is useful when creating shortcuts for a program on the system.
Let's try searching for the full path of an executable program on the system such as firefox
HOW TO SEARCH FOR FILES USING THE WHEREIS COMMAND
The whereis command is used to display the binary, source, and the manual page file of command is located. By simply typing the whereis command of any command it shows the following information
If you want the path to the executable to display, you can use the –b option. It is rare for someone to search for a source and manual page of a program. They are all cool running the man command on the terminal rather than looking for the source of the file. If such is the case, run the command to just a few the path to the executable.
Linux is not rocket science; if you can follow up with all these articles, you will get acquainted with using Linux. We have been able to talk about four commands which will be useful to you while searching files through your file system, the rest of the work will be determined how much you practice.