search
HomeSystem TutorialLINUXHow To Find A Directory In Linux From Command Line

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
How does hardware compatibility differ between Linux and Windows?How does hardware compatibility differ between Linux and Windows?Apr 23, 2025 am 12:15 AM

Linux and Windows differ in hardware compatibility: Windows has extensive driver support, and Linux depends on the community and vendors. To solve Linux compatibility problems, you can manually compile drivers, such as cloning RTL8188EU driver repository, compiling and installing; Windows users need to manage drivers to optimize performance.

What are the differences in virtualization support between Linux and Windows?What are the differences in virtualization support between Linux and Windows?Apr 22, 2025 pm 06:09 PM

The main differences between Linux and Windows in virtualization support are: 1) Linux provides KVM and Xen, with outstanding performance and flexibility, suitable for high customization environments; 2) Windows supports virtualization through Hyper-V, with a friendly interface, and is closely integrated with the Microsoft ecosystem, suitable for enterprises that rely on Microsoft software.

What are the main tasks of a Linux system administrator?What are the main tasks of a Linux system administrator?Apr 19, 2025 am 12:23 AM

The main tasks of Linux system administrators include system monitoring and performance tuning, user management, software package management, security management and backup, troubleshooting and resolution, performance optimization and best practices. 1. Use top, htop and other tools to monitor system performance and tune it. 2. Manage user accounts and permissions through useradd commands and other commands. 3. Use apt and yum to manage software packages to ensure system updates and security. 4. Configure a firewall, monitor logs, and perform data backup to ensure system security. 5. Troubleshoot and resolve through log analysis and tool use. 6. Optimize kernel parameters and application configuration, and follow best practices to improve system performance and stability.

Is it hard to learn Linux?Is it hard to learn Linux?Apr 18, 2025 am 12:23 AM

Learning Linux is not difficult. 1.Linux is an open source operating system based on Unix and is widely used in servers, embedded systems and personal computers. 2. Understanding file system and permission management is the key. The file system is hierarchical, and permissions include reading, writing and execution. 3. Package management systems such as apt and dnf make software management convenient. 4. Process management is implemented through ps and top commands. 5. Start learning from basic commands such as mkdir, cd, touch and nano, and then try advanced usage such as shell scripts and text processing. 6. Common errors such as permission problems can be solved through sudo and chmod. 7. Performance optimization suggestions include using htop to monitor resources, cleaning unnecessary files, and using sy

What is the salary of Linux administrator?What is the salary of Linux administrator?Apr 17, 2025 am 12:24 AM

The average annual salary of Linux administrators is $75,000 to $95,000 in the United States and €40,000 to €60,000 in Europe. To increase salary, you can: 1. Continuously learn new technologies, such as cloud computing and container technology; 2. Accumulate project experience and establish Portfolio; 3. Establish a professional network and expand your network.

What is the main purpose of Linux?What is the main purpose of Linux?Apr 16, 2025 am 12:19 AM

The main uses of Linux include: 1. Server operating system, 2. Embedded system, 3. Desktop operating system, 4. Development and testing environment. Linux excels in these areas, providing stability, security and efficient development tools.

Does the internet run on Linux?Does the internet run on Linux?Apr 14, 2025 am 12:03 AM

The Internet does not rely on a single operating system, but Linux plays an important role in it. Linux is widely used in servers and network devices and is popular for its stability, security and scalability.

What are Linux operations?What are Linux operations?Apr 13, 2025 am 12:20 AM

The core of the Linux operating system is its command line interface, which can perform various operations through the command line. 1. File and directory operations use ls, cd, mkdir, rm and other commands to manage files and directories. 2. User and permission management ensures system security and resource allocation through useradd, passwd, chmod and other commands. 3. Process management uses ps, kill and other commands to monitor and control system processes. 4. Network operations include ping, ifconfig, ssh and other commands to configure and manage network connections. 5. System monitoring and maintenance use commands such as top, df, du to understand the system's operating status and resource usage.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function