DEV Community

Cover image for Find files and directories in CLI
Baransel
Baransel

Posted on • Originally published at baransel.dev

2 3

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!.

AWS Q Developer image

Your AI Code Assistant

Automate your code reviews. Catch bugs before your coworkers. Fix security issues in your code. Built to handle large projects, Amazon Q Developer works alongside you from idea to production code.

Get started free in your IDE

Top comments (0)

Billboard image

Create up to 10 Postgres Databases on Neon's free plan.

If you're starting a new project, Neon has got your databases covered. No credit cards. No trials. No getting in your way.

Try Neon for Free →

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay