Search for a file in Linux (Find command)

Do not miss this exclusive book on Binary Tree Problems. Get it now for free.

In this article, we have explored the approach to Search for a file in Linux (Find command).

Table of contents:

  1. Introduction
  2. Linux OS
  3. The Find Command
  4. Various options used with the find command
  5. Conclusion

Introduction

The unix Operating system is a multi-user, multi-tasking operating system. It consists of a set of programs that act as a link between the computer and the user. It is sometimes referred to as the kernel. On UNIX operating systems users interact with the system through a shell or command line interface which is capable of accepting commands in the form of text, interpreting the command (i.e. if it is a valid command) and executing the command. There a various variants of unix, some examples include: Solaris unix, BSD, AIX, HP Unix and Linux. Among all these Linux is the most popular and its free to use.

Linux OS

Linux operating system is an open source operating system based on unix. It supports multi-tasking and multi user operations and is widely used for supercomputers and servers. A standard way of interacting with the linux os is through a program called the shell, terminal, console or command prompt. The shell has the ability to execute a host of commands (e.g opening a file or starting up another application) and return the result back to the user.
In order to interact with the OS through the shell the user will need to learn the commands which can be interpreted by the shell. There are a wide range of commands understood by the linux shell but some are used more frquently than others. One such powerfull command is the find command.

The Find Command

The find command is a command line utility which can be used to search through a folder/directory hierachy and return files and folders which meet a certain criteria which is specified along with the find command. Below is a general syntax of the find command:

$ find [location to begin search] 
  [an expression determining what to search for] [-options] [action]

After the find command the first parameter specified is the location you wish for the search to be started.

The second parameter is used to determine what is to be searched (A filter). For example to search by name you pass it the value -name.

The third parameter is used to provide options which can be used to perform additional actions on the files that are returned from the find command.
The fourth parameter is the action to be performed

The third and fourth parameters are optional and can be ommited. Let's look at some examples of how to use the find command below.

Example 1:

    $ find ./documents -name myfile.txt
    output: ./documents/myfile.txt

In the example above we use the find command with only two parameters. The first parameter is ./documents which means the search should be started in the directory called documents and its subdirectories. The second parameter is "-name myfile.txt" specifies what to search by which in this case we are searching by name and we are looking for a file named myfile.txt. The output shows that the file was found directly in the documents directory.

Example 2:

     $ find /main -type d -name myDirectory
     output: ./main/projects/myDirectory

In this example we are starting the search from a folder named main and we are searching for a directory (searching by type) with the name myDirectory. The output gives us the path to the file.

Example 3:

    find ./main -name sample.txt -exec rm -i {} \;
    rm: remove regular file './main/demo/sample.txt'? y

In this example we pass four parameters to the find function. The first is the location to begin the search (a folder called main), the second parameter is what we are searching by which is name. The third parameter (exec) is used to signify that we want to execute another command on each of the files returned from the find command, the last parameter is the command we want to execute (in this case its the rm command) on the returned files. The second line shows the response gotten by when the first line is executed, if the file is found a prompt is displayed for the user to confirm he/she wants to delete the file.

Various options used with the find command

Outlined below are some of the various options which can be used with the find command to find tune a search to meet certain criteria.

  • -name: This option is used to search by file or directory name.
  • -newer file: used to search for files that were created or modified after file (the second parameter)
  • -links N: used to search for the file having links N.
  • -perm octal: used to search for files whose permissions are octal
  • -print: used to print the path of the file listed.
  • -size +N/-N: used to search for files with size N
  • -user name: used to search for files owned by user "name".
  • -rm file: used to remove files that are found.
    These are just a few of the options available with the find command.

Conclusion

The find command is a very important and useful command to any linux user. The most useful aspect of it is it's flexibility, it allows users to search for files or directories using a wide range of parameters like searching by name, size of file, date created etc. Further more it does'nt just search for files or directories, it also gives the user the ability to perform certain actions on files which have been returned by the find command.

With this article at OpenGenus, you must have the complete idea of Search for a file in Linux (Find command).

Sign up for FREE 3 months of Amazon Music. YOU MUST NOT MISS.