Home Linux Commands How To Find A Directory In Linux From Command Line

How To Find A Directory In Linux From Command Line

Mastering Directory Search in Linux with the find Command

By sk
428 views

Ever misplaced a folder on your Linux computer? Worry not! Linux offers powerful command line tools to locate those missing directories. One of the most versatile is the find command, which allows you to search for directories based on various criteria. In this guide, we'll cover several ways to find a directory on your Linux system.

First, we'll show you how to search for directories by name. Then, we'll demonstrate how to find recently modified directories and directories modified in the last N days. You'll also learn how to identify older directories - those modified more than N days ago.

Next, we'll cover searching for directories within a specified depth range using the maxdepth and mindepth options. You'll see how to find directories based on their permissions, as well as directories containing a specific number of files.

Finally, we'll explain how to search for empty directories, which can be useful for identifying unused or unnecessary parts of your file system.

Throughout the guide, we'll provide practical examples to help you get the most out of the find command when working with directories in Linux.

Search by Directory Name

If you know the name of the directory and where to start searching, use this 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 searches for directories with the name "Documents" starting from the user's home directory (~/).

Here's a breakdown of the command:

  1. find ~/: This starts the search from the user's home directory (~/).
  2. -type d: This tells find to search for directories (-type d) instead of regular files.
  3. -name "Documents": This option tells find to look for directories with the name "Documents". The quotes around "Documents" ensure that the search looks for the exact name, not just directories containing the word "Documents".

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

Search Directories in the Current Directory

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

$ find . -type d -name "Documents"

Here's the breakdown of what each option does:

  1. find .: The . represents the current directory, so the search will start from the current working directory.
  2. -type d: This tells find to search for directories.
  3. -name "Documents": This option tells find to look for directories with the name "Documents".

The command will search the current directory and all its subdirectories for any directories named "Documents" and print the full paths to those directories.

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

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

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

Using -iname to Ignore Case

This is a quite useful flag in the find command.

We can use the -iname option instead of -name to make the search case-insensitive.

Here's the example using -iname instead of -name:

$ find . -type d -iname "Documents"

The -iname "Documents" option will match directories with names like "Documents", "documents", "DOCUMENTS", etc. This can be useful if you're not sure about the exact capitalization of the directory name.

The main differences between -name and -iname are:

  • -name performs a case-sensitive search, so it will only match directories with the exact capitalization of the provided pattern.
  • -iname performs a case-insensitive search, so it will match directories regardless of their capitalization.

You can also include the search term inside the wildcard like this:

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

This command will list all directories within the current directory (and its subdirectories) that have "documents" in their names.

Using -iname can be more convenient in situations where you don't know or care about the exact casing of the directory name you're looking for.

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

Search for Both Directories and Files

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

The command would look like this:

$ find . -iname "Documents"

Here's what would happen:

  1. The find command will search the current directory (.) and all its subdirectories for any items (both files and directories) with the name "Documents" (case-insensitive).
  2. It will include both directories named "Documents" and any regular files named "Documents" in the results.

So, the output of this command would include:

  • Any directories named "Documents", "documents", "DOCUMENTS", etc.
  • Any regular files named "Documents", "documents", "DOCUMENTS", etc.

Removing the -type d part makes the search more broad, as it will include both directories and files that match the specified name pattern.

This can be useful if you're not sure whether the "Documents" entry you're looking for is a directory or a file, and you want to find all occurrences of it.

However, if you're specifically looking for directories named "Documents", it's better to keep the -type d option to narrow down the search.

Find Recently Modified Directories

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

For example, to search for directories that were created or modified between March 15, 2024 and April 15, 2024 (exclusive), you can use:

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

Here's a breakdown of the above command:

  1. find /path/to/search: This tells the find command to start the search from the /path/to/search directory.
  2. -type d: This option ensures that the find command only searches for directories, not regular files.
  3. -newermt "2024-03-15": This option tells find to only include directories that are newer (created or modified) than March 15, 2024.
  4. ! -newermt "2024-04-15": The ! symbol negates the condition, so this part tells find to exclude directories that are newer than April 15, 2024.

The combination of these options results in find searching for directories that were created or modified on or after March 15, 2024, but before April 15, 2024.

This can be useful, for example, if you want to find all the directories that were created or modified during a specific time period, such as for backup or auditing purposes.

Locate Directories Modified in the Last N Days

To find directories modified in the last n days, use -mtime.

The following command will search for directories under the ~/Projects/ directory that have been modified within the last 7 days.

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

Here's what each part does in the command:

  1. find ~/Projects/: This tells the find command to start the search from the ~/Projects/ directory.
  2. -type d: This option ensures that the find command only searches for directories, not regular files.
  3. -mtime -7: This option tells find to only include directories that have been modified (the "m" in "-mtime" stands for "modification time") within the last 7 days.

The -mtime -7 part works as follows:

  • The - sign indicates "less than" or "within the last".
  • The number 7 represents the number of days.

This can be useful, for example, if you want to find all the new directories that have been created or updated recently within your ~/Projects/ directory, perhaps for backup or tracking purposes.

Identify Older Directories

To find directories modified more than N days ago (i.e. (find directories older than N days), use -mtime with a plus sign.

For instance, the following command will search for directories under the ~/Projects/ directory that have been modified more than 7 days ago.

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

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

The -mtime +7 option tells find to include only directories that have a modification time ("m" in "-mtime") greater than 7 days ago.

So this command is useful for finding directories that haven't been touched or modified for a certain period of time, which could be helpful for tasks like identifying unused or outdated directories, or performing maintenance/cleanup on your project directory structure.

How do we know if the directories are really modified more than X days ago? Simple.

To verify if the directories found by the find command are modified more than 7 days ago, you can use the stat command to display the modification time of each directory.

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

Here is the explanation for the above command:

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

When you run this command, it will display the path and last modification time of each directory that is older than 7 days. This allows you to verify that the directories found are indeed modified more than 7 days ago.

Here's an example of the output:

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

The modification time displayed in the output will help you confirm that the directories are indeed older than 7 days, as required by the original find command.

Related: How To Find And Delete Files Older Than X Days In Linux

Search Directories within Specified Range

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

Here is the syntax:

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

Let me explain what each option does in this command:

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

Please note that the -mindepth option should be specified before other arguments, including -type.

For example, to search for directories that are at least 2 levels deep but no more than 4 levels deep within the /Projects directory, you would use the following command:

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

This command will search for directories that are between 2 and 4 levels deep (inclusive) relative to the /Projects directory.

You can adjust the values of X and Y to suit your specific needs. For instance, if you want to search for directories that are exactly 3 levels deep, you can use:

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

This will only include directories that are 3 levels deep in the search results.

Using maxdepth and mindepth together can be helpful when you want to narrow down your search to a specific range of directory depths, which can be useful for organizing and managing your file system.

More details on maxdepth and mindepth can be found in the following link:

How To Use maxdepth and mindepth Options With Linux find command

Find a Directory Based on its Permission

To find directories based on their permissions, you can use the find command with the -perm option.

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

Replace /path/to/search with the directory where you want to start the search, and <permission_specification> with the desired permission. You can specify the permission using octal notation.

Allow me to show you some examples.

To find all directories in the current directory with permission 755 (i.e. rwx for the owner and rx for group and others):

$ find . -type d -perm 755

To find directories with permission rwx for the owner, r-x for group, and no permissions for others:

$ find . -type d -perm 750

To find all directories within ~/Projects with any permission for the owner and r-x for group and others:

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

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

Find Directories that Contains Specific Number of Files

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

Example:

The following command finds all directories in the current directory and its subdirectories that contain exactly 4 files, and prints the full path of 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: This starts the search for directories (not files) from the current directory (.).
  2. -exec bash -c '...' _ {} \;: This part uses the exec option of find to execute a custom Bash script for each directory found.
  3. if [ $(find "$1" -maxdepth 1 -type f | wc -l) -eq 4 ]; then echo "$1"; fi: This is the custom Bash script that is executed for each directory.
  • $(find "$1" -maxdepth 1 -type f | wc -l): This part counts the number of files (not directories) in the current directory ("$1"). The -maxdepth 1 option ensures that it only counts the files in the current directory, not in subdirectories.
  • [ ... -eq 4 ]: This checks if the number of files in the current directory is exactly 4.
  • then echo "$1"; fi: If the condition is true (the directory has exactly 4 files), the full path of the directory is printed.

This can be useful, for example, 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 don't meet that criteria.

Search for Empty Directories

To locate empty directories, use the -empty flag:

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

For example, to search for empty directories within the “Work” directory, run:

$ find ~/Work/ -type d -empty

Remember to adjust paths as needed.

Conclusion

In this guide, you've learned several techniques for using the find command to search for directories in your Linux system.

You can search by name, how recently it was changed, or how many files are inside. You can even find empty folders or ones with specific permissions.

By understanding how to use options like maxdepth, mindepth, and size, you can refine your searches to focus on the specific directories you need.

So next time a folder goes missing, don't panic! Use these find commands to track it down and keep your files organized.

Related Read:

You May Also Like

Leave a Comment

* By using this form you agree with the storage and handling of your data by this website.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

This website uses cookies to improve your experience. By using this site, we will assume that you're OK with it. Accept Read More