Find Files or Directories by Name in Linux

Find Files or Directories by Name in Linux

When working with files in the Linux systems, there may need to find files or directories by name. This tutorial demonstrates how to do that.

Create few directories and files for testing:

mkdir -p docs/dir_1 docs/dir_2 docs/test
echo "Hello world" > docs/dir_1/test
echo "Hello world" > docs/dir_2/test
echo "Hello world" > docs/test/data.txt

Find files and directories

To find files and directories by name, use find command with -name option. For example, the following command finds files and directories where the name is test in docs directory:

find docs -name test

Output:

docs/test
docs/dir_1/test
docs/dir_2/test

To perform a case-insensitive search, use the -iname option instead of -name.

find docs -iname test

Find only files

To find only files, use the -type option with f value.

find docs -type f -name test

Output:

docs/dir_1/test
docs/dir_2/test

Find only directories

To find only directories, use the -type option with d value.

find docs -type d -name test

Output:

docs/test

Leave a Comment

Cancel reply

Your email address will not be published.