DEV Community

Cover image for Find files and directories in CLI
Baransel
Baransel

Posted on • Originally published at baransel.dev

Find files and directories in CLI

Sign up to my newsletter!.

Although the popular use of the find command is to search within the folder path with its option, it can also meet much more specific needs when customized.

The basic usage is as follows:

find [search-to-directory] [option] [search-org]
Enter fullscreen mode Exit fullscreen mode

Let's start with examples. Our first example is to find the error.log files on the server.

find / -name "error.log"
Enter fullscreen mode Exit fullscreen mode

Or using type;

find / -type f -name "error.log"
Enter fullscreen mode Exit fullscreen mode

When this command is applied, the error.log files found in other services in the system will also be listed. If we want to find all *.log files on the server, the following command will be useful.

find / -name "*.log"
Enter fullscreen mode Exit fullscreen mode

Let's search multiple files at once. The files we want to find are access.logand error.log.

find / -type f ( -name "access.log" -o -name "error.log" ) -print
Enter fullscreen mode Exit fullscreen mode

we are not limited to searching only filenames. Let's take a look at -type expressions before folder searches.

  • b: block device
  • c: character device
  • d: directory
  • f: regular file
  • l: symbolic link
  • P: FIFO
  • s: socket

Now let's perform a folder search.

find / -type d -name [folder-name]
Enter fullscreen mode Exit fullscreen mode

Let me remind you that all operations are case-sensitive. We can use -iname to perform search operations without distinction of upper and lower case.

find / -type d -iname [folder-name]
Enter fullscreen mode Exit fullscreen mode

Sign up to my newsletter!.

Top comments (0)