Home >System Tutorial >LINUX >How To Find A Directory In Linux From Command Line

How To Find A Directory In Linux From Command Line

Lisa Kudrow
Lisa KudrowOriginal
2025-03-18 09:07:11939browse

How To Find A Directory In Linux From Command Line

Can't find a folder in Linux system? don’t worry! Linux provides powerful command-line tools to find missing directories. The most common tool is the find command, which allows you to search directories based on various criteria. This guide will introduce several ways to find directories in Linux systems.

First, we will demonstrate how to search the directory by name. We will then demonstrate how to find the most recently modified directories and the ones modified in the past N days. You will also learn how to identify older directories—those modified N days ago.

Next, we will explain how to search for a directory within a specified depth range using the maxdepth and mindepth options. You will learn how to find directories based on permissions for directories and directories that contain a specific number of files.

Finally, we will explain how to search for empty directories, which is very useful for identifying unused or unnecessary parts of the file system.

Throughout the guide, we will provide practical examples to help you take advantage of the find command when using directories in Linux.

Table of contents

  • Search the directory in the current directory
  • Use -iname to ignore case
  • Search directories and files
  • Find the recently modified directory
  • Find directories modified in the past N days
  • Identify older directories
  • Search the directory within the specified range
  • Find directory based on directory permissions
  • Find directories containing a specific number of files
  • Search for empty directory
  • Summarize

Search by directory name

If you know the name of the directory and where the search starts, use the following command:

 find /path/to/search -type d -name "directory_name"

Replace /path/to/search with the actual path and "directory_name" with the target directory.

Example:

 $ find ~/ -type d -name "Documents"

This command starts searching for a directory named "Documents" from the user's home directory (~/).

Here is a breakdown of the commands:

  1. find ~/ : Start searching from the user's home directory (~/).
  2. -type d : Tell find to search for directories (-type d) instead of normal files.
  3. -name "Documents" : This option tells find to find a directory named "Documents". Quotes on both sides of "Documents" ensure that the search looks for exact names, not just directories containing the word "Documents".

Therefore, this command will search the user's home directory and all its subdirectories for any directory named "Documents". If any directories are found, it will print the full path to those directories.

Search the directory in the current directory

If you want to start your search from the current directory instead of the home directory (~/), you can simply omit the ~/ part of the command as follows:

 $ find . -type d -name "Documents"

Here is a breakdown of the features for each option:

  1. find . : . represents the current directory, so the search will start from the current working directory.
  2. -type d : Tell find to search the directory.
  3. -name "Documents" : This option tells find to find a directory named "Documents".

This command will search the current directory and all its subdirectories, find any directory named "Documents", and print the full path to those directories.

Alternatively, you can also use the $PWD variable, which represents the current working directory as follows:

 $ find $PWD -type d -name "Documents"

Both commands will achieve the same result, starting with the current directory.

Use -iname to ignore case

This is a very useful flag in the find command.

We can use the -iname option instead of -name to perform case-insensitive searches.

Here is an example of using -iname instead of -name :

 $ find . -type d -iname "Documents"

-iname "Documents" option will match directories with names such as "Documents", "documents", "DOCUMENTS", etc. This is useful if you are not sure about the exact case of the directory name.

The main difference between -name and -iname is:

  • -name performs case-sensitive searches, so it will only match directories that are exactly the same case as the provided pattern.
  • -iname performs case-insensitive searches, so it will match the directory regardless of its case.

You can also include search terms in wildcards like this:

 $ find . -type d -iname "*documents*"

This command lists all directories in the current directory (and its subdirectories) that contain "documents" in their names.

Using -iname may be more convenient if you don't know or care about the exact case of the directory name you are looking for.

Personally, when searching for something, I prefer to use the -iname option in the find command.

Search directories and files

If you remove -type d part from the find command, it will search for files and directories that match other criteria such as -name or -iname options.

The command is as follows:

 $ find . -iname "Documents"

Here are what happens:

  1. The find command will search the current directory (.) and all its subdirectories for any project (files and directories) named "Documents" (case insensitive).
  2. It will contain a directory named "Documents" and any normal file named "Documents" in the result.

Therefore, the output of this command will include:

  • Any directory named "Documents", "documents", "DOCUMENTS", etc.
  • Any normal file named "Documents", "documents", "DOCUMENTS", etc.

Deleting -type d section will make the search wider because it will contain directories and files that match the specified name pattern.

This is useful if you are not sure if the "Documents" entry you are looking for is a directory or a file, and you want to find all the happenings.

However, if you specifically look for directories named "Documents", it's better to keep -type d option to narrow your search.

Find the recently modified directory

To list directories modified within a specific date range, use -newermt twice.

For example, to search for directories created or modified between March 15, 2024 and April 15, 2024 (excluding these two days), you can use:

 $ find /path/to/search -type d -newermt "2024-03-15" ! -newermt "2024-04-15"

Here is a breakdown of the above commands:

  1. find /path/to/search : Tell the find command to start searching from /path/to/search directory.
  2. -type d : This option ensures that find command only searches directories, not ordinary files.
  3. -newermt "2024-03-15" : This option tells find to contain only directories that are updated (created or modified) than March 15, 2024.
  4. ! -newermt "2024-04-15" : ! Symbol Negative Conditions, so this part tells find to exclude directories updated than April 15, 2024.

The combination of these options causes find search directory to be created or modified on or after March 15, 2024, but before April 15, 2024.

For example, this can be useful if you want to find all directories created or modified in a specific time period (for example, for backup or audit).

Find directories modified in the past N days

To find directories that have been modified in the past n days, use -mtime .

The following command will search for the directory modified in the past 7 days under the ~/Projects/ directory.

 $ find ~/Projects/ -type d -mtime -7

Here is what each part of the command does:

  1. find ~/Projects/ : Tell the find command to start searching from the ~/Projects/ directory.
  2. -type d : This option ensures that find command only searches directories, not ordinary files.
  3. -mtime -7 : This option tells find to include only directories that have been modified in the past 7 days ("m" in "-mtime" means "modify time").

The working principle of -mtime -7 part is as follows:

  • The minus sign (-) means "less than" or "in the past".
  • The number 7 represents the number of days.

For example, this will be very useful if you want to find all new directories that have been recently created or updated in the ~/Projects/ directory (perhaps for backup or tracking).

Identify older directories

To find a directory that was modified N days ago (i.e. (Find a directory that was N days ago), use -mtime with a plus sign.

For example, the following command will search for the directory modified 7 days ago under the ~/Projects/ directory.

 $ find ~/Projects/ -type d -mtime 7

In other words, it will look for directories that have not been modified (created, updated, or accessed) in the past 7 days.

-mtime 7 option tells find to include only directories whose modification time ("m" in "-mtime") is greater than 7 days ago.

Therefore, this command can be used to find directories that have not been touched or modified for a period of time, which is helpful for identifying unused or outdated directories or performing tasks such as maintenance/cleaning of the project directory structure.

How do we know if the directory was actually modified X days ago? Very simple.

To verify that the directory found by the find command was modified 7 days ago, you can use the stat command to display the modification time for each directory.

 $ find ~/Projects/ -type d -mtime 7 -exec stat -c '%n %y' {} \;

The following is the explanation of the above command:

  1. find ~/Projects/ -type d -mtime 7 : This command part is the same as before, find the directory 7 days ago.
  2. -exec stat -c '%n %y' {} \; : This section uses the -exec option of find to run the stat command for each directory found.
  • stat -c '%n %y' : stat command displays the file name (%n) of the directory and the last modified time (%y).
  • {} : This is a placeholder, replaced by the current directory found by find .
  • \; : This terminates the -exec command.

When you run this command, it displays the path and last modified time of each directory 7 days ago. This allows you to verify that the directory found was indeed modified 7 days ago.

Here is an output example:

 <code>/home/ostechnix/Projects/Python 2024-03-22 14:18:14.958042173 0530 /home/ostechnix/Projects/Vim 2024-03-22 14:05:55.887041168 0530</code>

The modification time shown in the output will help you confirm that the directory is indeed longer than 7 days, as required by the original find command.

Search the directory within the specified range

We can search the directory within the specified depth range using the maxdepth and mindepth options of the find command.

The following is the syntax:

 find /path/to/search -mindepth X -maxdepth Y -type d

Let me explain what each option in this command does:

  1. find /path/to/search : Tell the find command to start searching from /path/to/search directory.
  2. -mindepth X : This option sets the minimum depth level of the directory to include in the search results. The value X represents the minimum number of directory levels starting from the starting point.
  3. -maxdepth Y : This option sets the maximum depth level of the directory to include in the search results. The value Y represents the maximum number of directory levels starting from the starting point.
  4. -type d : This option ensures that find command only searches directories, not ordinary files.

Note that the -mindepth option should be specified before other parameters, including -type .

For example, to search for a directory at least 2 layers deep but up to 4 layers deep in the /Projects directory, you can use the following command:

 $ find ~/Projects -mindepth 2 -maxdepth 4 -type d

This command searches directories (included) at least 2 layers deep and up to 4 layers deep relative to the /Projects directory.

You can adjust the values ​​of X and Y according to your specific needs. For example, if you want to search for a directory that is exactly 3 layers deep, you can use:

 $ find ~/Projects -mindepth 3 -maxdepth 3 -type d

This will only contain 3-layer deep directories in the search results.

Using maxdepth and mindepth in combination can help you narrow your search to a specific range of directory depth, which is very useful for organizing and managing file systems.

Find directory based on directory permissions

To find a directory based on permissions of the directory, you can use the find command with the -perm option.

 find /path/to/search -type d -perm<permission_specification></permission_specification>

Replace /path/to/search with the directory you want to start searching and<permission_specification></permission_specification> Replace with the required permissions. You can specify permissions using octal notation.

Let me show you some examples.

To find all directories with permissions 755 (i.e. owner is rwx, group and others are rx) in the current directory:

 $ find . -type d -perm 755

To find directories where the owner has rwx permissions, the group has rx permissions, and others do not have permissions:

 $ find . -type d -perm 750

To find all directories in ~/Projects where the owner has any permissions and groups and others have rx permissions:

 $ find ~/Projects~ -type d -perm -005

You can adjust according to your specific requirements<permission_specification></permission_specification> . Additionally, you can use -maxdepth and -mindepth options to control the depth of the search if needed.

Find directories containing a specific number of files

To find directories that contain a specific number of files, you can use find , wc and bash commands in combination.

Example:

The following command looks for directories that happen to contain 4 files in the current directory and its subdirectories, and prints the full path to those directories.

 $ find . -type d -exec bash -c 'if [ $(find "$1" -maxdepth 1 -type f | wc -l) -eq 4 ]; then echo "$1"; fi' _ {} \;

Let's break down the command step by step:

  1. find . -type d : Start searching for directories (not files) from the current directory (.).
  2. -exec bash -c '...' _ {} \; : This section uses the find exec option to execute custom Bash scripts for each directory found.
  3. if [ $(find "$1" -maxdepth 1 -type f | wc -l) -eq 4 ]; then echo "$1"; fi : This is a custom Bash script executed for each directory.
  • $(find "$1" -maxdepth 1 -type f | wc -l) : This section calculates the number of files (not directories) in the current directory ("$1"). -maxdepth 1 option ensures that it only calculates files in the current directory, not files in the subdirectory.
  • [ ... -eq 4 ] : This checks whether the number of files in the current directory is exactly 4.
  • then echo "$1"; fi : If the condition is true (the directory happens to have 4 files), print the full path to the directory.

For example, this would be useful if you have a specific set of directories that should contain a certain number of files and you want to quickly identify any directories that do not meet that criteria.

Search for empty directory

To find an empty directory, use the -empty flag:

 find /path/to/search -type d -empty

For example, to search for an empty directory in the "Work" directory, run:

 $ find ~/Work/ -type d -empty

Remember to adjust the path as needed.

Summarize

In this guide, you have learned several techniques for searching for directories in Linux systems using the find command.

You can search by name, last time of change, or by number of internal files. You can even find empty folders or folders with specific permissions.

By learning how to use options like maxdepth , mindepth , and size , you can improve your search to focus on the specific directories you need.

Therefore, don't panic next time the folder is lost! Use these find commands to track it and keep the file organized.

The above is the detailed content of How To Find A Directory In Linux From Command Line. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn