Menu Close

Searching Files in Ubuntu Linux

This article talks about how to search for files in Ubuntu Linux.

For more on this subject, please refer to the SOC Table of Contents.


Using the find command

The main purpose of find is to search for files under a directory. The formatting is as follows:

find [path] [option] [key word]

Where path is the directory path that you wish to search. You may opt to leave it blank to search under the current directory. key word is typically a part of the name of the file that you’re looking for.

The main options are as follows:

-name<filename>                  Searches and finds all files that match with the provided filename. Asterisks can be used.

-depth                                      Start searching from the deepest subdirectory under the given directory.

-gid<Group ID>                     Find directories or files that matches the specified group ID.

-group<Group Name>        Find the directories or files that matches the specified group name.

-size<file size>                     Find files that match with the specified file size.

-type<file type>                   Find files that match with the specified file type.

-user<owner name>           Find the directories or files that matches the specified owner’s name.

We will be searching all files that start with “vim” under the /etc directory as an example.



Using the grep command

While the find command searches file names for matches, the grep command searches the content of the file for matches. If grep finds any matches, it will list the entire line of which the key word was located. Note that it can only search for one key word at a time. The formatting is as follows:

grep [option] [keyword] 

Where [keyword] is the word or character that you want to find. The main options are as follows:

-b                    Prefix each matching line with a block number.

-c                    Only output the number of lines that match.

-i                    Disregard upper and lowercase.

-v                    Reverse search, thus only show the lines that don’t match.

-r                    Recursively searches under the given directory.

As an example, we will be searching files that include the term “Ubuntu” under the /usr directory.



File types

Note that this is not the same as saying if a file is a music file or a document file. To better understand what sort of “file types” we are dealing with, we can simply type in the command ls -l to show all of the details of the current user’s root files.

The first character of the leftmost column is the file type. For example, the d in drwxr-xr-x for Desktop is the file type. The meaning behind these characters is as follows:

           Regular files, which includes things such as documents, images, and music, that is created by applications.

d          Directory files.

c          Character device files: stores character device drivers, includes connection devices and audio devices.

b          Block device files, which stores device drivers such as those for hard drive and USB.

l           Link files, which is similar to the shortcuts in Windows.

s          Socket files.

p          Pipeline files. Mainly referring to FIFO files.

Posted in Textbook and Training Project

Related Articles

Leave a Reply

Your email address will not be published.

Leave the field below empty!