Skip to Content

How to Find Directory on Linux Using Find Command

Finding a directory on Linux can be challenging, especially if you have a large and complex file system. Fortunately, Linux provides a powerful tool called the find command that can help you locate any directory based on various criteria. In this article, you will learn how to use the find command to search for directories by name, type, date, size, and more.

What is the Find Command?

The find command is one of the most versatile and useful tools in the Linux system administrator’s arsenal. It allows you to search for files and directories in a directory hierarchy based on a user-given expression and perform actions on each matched file or directory. You can use the find command to search for directories based on their permissions, ownership, modification time, size, and more. You can also combine the find command with other tools such as grep or sed to perform more complex tasks.

The general syntax for the find command is as follows:

find [options] [path...] [expression]

The options control the treatment of symbolic links, debugging options, and optimization methods. The path specifies the starting directory or directories where the find command will search for files and directories. The expression is made up of options, search patterns, and actions separated by operators.

To search for directories in a directory, you need to have read permissions on that directory. Otherwise, the find command will not be able to access the directory and its contents.

How to Find a Directory by Name

One of the most common ways to search for a directory is by its name. To do this, you need to use the -name option followed by the name of the directory you are looking for. For example, to search for a directory named Documents in the home directory, you can use the following command:

find ~ -type d -name "Documents"

The ~ symbol represents the home directory of the current user. The -type d option tells the find command to look for directories only, not files. The -name “Documents” option specifies the name of the directory to search for. The name is enclosed in double quotes to prevent the shell from interpreting any special characters in the name.

If you want to perform a case-insensitive search, you can use the -iname option instead of -name. For example, to search for a directory named documentsDocuments, or DOCUMENTS, you can use the following command:

find ~ -type d -iname "documents"

How to Find a Directory by Type

In Linux, everything is a file, including directories, symbolic links, devices, and pipes. To search for directories based on their type, you need to use the -type option followed by one of the following descriptors:

  • f: a regular file
  • d: a directory
  • l: a symbolic link
  • c: a character device
  • b: a block device
  • p: a named pipe (FIFO)

For example, to search for all symbolic links in the current directory, you can use the following command:

find . -type l

The . symbol represents the current directory. The -type l option tells the find command to look for symbolic links only.

How to Find a Directory by Date

Another way to search for a directory is by its modification date. To do this, you need to use the -newermt option followed by a date expression. The date expression can be in various formats, such as YYYY-MM-DDMM/DD/YYYY, or todayyesterday, or tomorrow. For example, to search for directories that were modified on March 1, 2024, you can use the following command:

find ~ -type d -newermt "2024-03-01"

The -newermt “2024-03-01” option tells the find command to look for directories that were modified on or after March 1, 2024. To specify a date range, you can use the ! operator to negate the second date expression. For example, to search for directories that were modified between March 1 and March 15, 2024, you can use the following command:

find ~ -type d -newermt "2024-03-01" ! -newermt "2024-03-16"

The ! -newermt “2024-03-16” option tells the find command to exclude directories that were modified on or after March 16, 2024.

You can also use the -mtime option to search for directories based on their modification time in days. For example, to search for directories that were modified in the last 7 days, you can use the following command:

find ~ -type d -mtime -7

The -mtime -7 option tells the find command to look for directories that were modified less than 7 days ago. To search for directories that were modified more than 7 days ago, you can use the following command:

find ~ -type d -mtime +7

The -mtime +7 option tells the find command to look for directories that were modified more than 7 days ago.

How to Find a Directory by Size

You can also search for directories based on their size. To do this, you need to use the -size option followed by a size expression. The size expression consists of a number and a unit. The unit can be one of the following:

  • b: 512-byte blocks
  • c: bytes
  • w: two-byte words
  • k: kilobytes
  • M: megabytes
  • G: gigabytes

For example, to search for directories that are larger than 10 megabytes, you can use the following command:

find ~ -type d -size +10M

The -size +10M option tells the find command to look for directories that are larger than 10 megabytes. To search for directories that are smaller than 10 megabytes, you can use the following command:

find ~ -type d -size -10M

The -size -10M option tells the find command to look for directories that are smaller than 10 megabytes.

How to Find Empty Directories

Sometimes, you may want to find empty directories that do not contain any files or subdirectories. To do this, you can use the -empty option. For example, to search for empty directories in the home directory, you can use the following command:

find ~ -type d -empty

The -empty option tells the find command to look for directories that are empty.

Conclusion

The find command is a powerful tool that can help you locate any directory on Linux based on various criteria. You can use the find command to search for directories by name, type, date, size, and more. You can also combine the find command with other tools to perform more complex tasks.

This article has shown you some of the most common and useful ways to use the find command to find directories on Linux.