Debug School

rakesh kumar
rakesh kumar

Posted on

What is difference between find and which in linux command

In the context of Linux commands, "find" and "which" have similar but distinct functions:

"Find" is a command used to search for files and directories within a specified location. It can be used to search for files based on various criteria, such as name, size, modification time, etc. For example: "find / -name myfile.txt" will search for the file named "myfile.txt" starting from the root directory "/".

"Which" is a command used to determine the location of a specified executable file in the system PATH. It returns the full path of the executable file if it is found, or an error message if it is not found. For example: "which ls" will return the full path of the "ls" executable, which is usually "/bin/ls".

In summary, "find" is used for searching for files and directories, while "which" is used for determining the location of executable files.

which ls
Enter fullscreen mode Exit fullscreen mode

This command will return the full path of the "ls" executable, such as "/bin/ls".

which mysql
Enter fullscreen mode Exit fullscreen mode

Image description

This will return the path of the mysql executable, typically something like /usr/bin/mysql

Find

Consider the following directory structure:

/
  home/
    user1/
      file1.txt
      file2.txt
    user2/
      file3.txt
Enter fullscreen mode Exit fullscreen mode
find /home -type f
Enter fullscreen mode Exit fullscreen mode

Image description

Image description

# find / -type d -name "etc"
Enter fullscreen mode Exit fullscreen mode

OR

$ sudo find / -type d -name "etc"
Enter fullscreen mode Exit fullscreen mode

OR

$ sudo find / -type d -iname "etc"
Enter fullscreen mode Exit fullscreen mode

Top comments (0)