Searching files in CentOS

Searching is vital for so many things in the Linux world. Let’s say that we want to search for a log in a specific period of time, or we want to search for a file in shared folder hosted on our server by extension, by date created or by last modification. All the above can be done and I’ll show you how.

Let’s get started!

Searching in Linux it’s possible using two commands “Locate” and “Find” one is more limited than the other but both are powerful in some way.

Locate

One of the biggest differences between the two commands it’s the speed. Locate it’s faster than find because it searches from an internal database, hence the results are showed quicker. This database it’s updated once per day, meaning if you are looking for a record created the same day it won’t appear on the results. This will make “find” more reliable and powerful.

On the other hand, another big difference it’s the lack of filtering for our searches, locate works better to spot existing files on the system and it’s definitively poorer than find in terms of searching.

As most of the things in Linux, we can force the update the locate database using the terminal, after executing this command we will be able to find files created the same day without waiting for the database to refresh.

$updatedb

Here are few examples how Locate will help you to find your files:

First of all, I am testing all the commands before bring them into the blog, so I have ran few commands that I haven’t covered on the blog yet and I’ll them explain below:

ls -l = List the files detailed in the selected path in this case -> /var/log

Pipe (|)=  It’s able to combine two or more commands in the same line.

grep = Searches after an standart input or a result given by a command like “ls -l”

messages = Grep it’s looking for the word messages in the result of the command “ls -l”

Make sense? let me know in the comments below and also try it at home.

usinggrep

Syntaxys: $locate (option) (location) pattern

So, after that we run locate together with -A (For all results) and a pattern “message” giving the results below:

LocateSearch.JPG

Locate as I said before, doesn’t have many options available for searching if you read the man page they aren’t specific commands to search by date or by size, which can be done using find.

Find

Find instead it’s so much powerful because it has more filters to play with and it allows us to be very specific in our searches.

Find below some find examples:

Syntax: $find (option) (location)

Here are few examples create by me to show you real world examples.

In the following example, I looked for records modified 5 minutes ago:

find-por-minutos

Quick note: In order to test my results I ran the command “tail /var/log/cron” which will sent us to the bottom of the log file to check if indeed the file was modified 5 minutes ago, I didn’t take the screenshot of the time but I can ensure it worked.

In the next example I searched for pattern or name using the option “-name”, please note the “*” which means it will searching all the records in that folder or all the records starting in “messa”

pornombre

In this example I searched by extension using “*.tar”, and the results were available again.

tar

The are a tons of commands we can try using find as this is a very powerful tool, however we won’t we able to cover them all in this post, If you want o search more information about find and locate or you want to run a specific task, feel free to use the man or info page as follows:

$man find/locate

$info find/locate

I hope you like it!!

Ops4life!

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s