How to Find Directories - Folders in Linux From Terminal - Many Examples With Different Options
1- Introduction
In before example, we examined the find command to find the files in the Linux system.
In this article, We will examine the find
unix command that we can easily use in the terminal for searching directories in the Linux system.
2- Usage Template
find [start_directory] [options] [expression]
- [start_directory]: This parameter is a directory where the search will start to look at from. We can specify any directory as the starting point.
- [options]: Additional parameters that change the behavior of the "find" command. They can be size, file type, ownership, etc.
- [expression]: This is a pattern or condition that specifies what we are looking for.
2.1 - Usage Example To Find Only Directories
find / -type d -name "folder"
This find command searches for directories from the root directory ("/"), for the named by "folder".
- The
/
directory is where the search will begin. - Using
-type d
parameter to search only directories/folders. - Using
-name
parameter to search pattern. (The match is case sensitive)
Note: name parameter is case sensitivity. Use-iname
parameter to ignore case sensitivity Instead of-name
parameter.
Note 2 : We can use asterisks if we do not know the exact name of the file. and we will see in the examples.
3 - Filtering Types
We can specify the type for searching.
Using the -type
option;
f
: To search any type of file (including hidden files)d
: To Search directories. (including hidden directories)s
: socketc
: Character Devices
: Socketp
: Named pipel
Symbolic link
Not: We can use multiple types for searching.
find / -type f,d -name "something"
For example, we will use f
and d
type options for the examples of directories and files searching.
4- Before The Examples
Let me create some directories and files in my local machine.
On my local machine;
- /home/volkan/find-directory-example
- "text-file.txt" (10 byte) - /home/volkan/find-directory-example/log-files
- Sub-Log-Files - as a Directory without any files (empty)
- 5mb-log-file.log - as a File (Appx. 5.3 MB)
- 10mb-log-file.log - as a File (Appx. 10.3 MB) - /home/volkan/find-directory-example/dump-files
- Sub-Dump-Files - as a Directory without any files (empty)
- 5mb-dump-file.log (Appx. 5.3 MB) as File - .hidden-folder
- .hidden-file.log
5- How to find Directories in the Linux System
5. 1 - Find Only Directories With Exact Name Match
Let's find;
- Directory to start searching "/home/volkan/find-directory-example"
- Using
-type d
just for searching the types of directories. - Using
-name
parameter with the value "log-file" for searching file names with exact matches.
find /home/volkan/find-directory-example/ -type d -name "log-files"
This find command is looking for "log-files" directories from the starting point of "/home/volkan" directory and search only for directories with "-type d" parameter.
Output:
find /home/volkan/find-directory-example/ -type d -name "log-files"
/home/volkan/find-directory-example/log-files
The first line is our find search command and the second line is about to show the found directory where located.
5.2 - Find Only Directories With Containing Name Value (Wildcards)
5.2.1 - Find Directories with Case Sensitivity
Let us find all directories which contain "file" naming starting from "/home/volkan/find-directory-example/" directory
find /home/volkan/find-directory-example/ -type d -name "*file*"
- Directory to start searching "/home/volkan/find-directory-example/"
- Using
-type d
just for searching the types of directories. - Using
-name
parameter with the value "file" to search directory names containing the specified value.
Output:
find /home/volkan/find-directory-example/ -type d -name "*file*"
/home/volkan/find-directory-example/log-files
/home/volkan/find-directory-example/dump-files
5.2.2 - Ignore Case Sensitivity
Let's change the -name
parameter to iname
which is case insensitive.
find /home/volkan/find-directory-example/ -type d -iname "*file*"
Output:
/home/volkan/find-directory-example/log-files
/home/volkan/find-directory-example/log-files/Sub-Log-Files
/home/volkan/find-directory-example/dump-files
/home/volkan/find-directory-example/dump-files/Sub-Dump-Files
All directories are found by containing the "file" value and ignoring case sensitivity.
5.3 - Find Only Directories With Starting/Ending Name Value (Wildcards)
5.3.1 - Find Only Directories Starting With
Let us look for all directories starting from "/home/volkan/find-directory-example" directory and starting with "log"
find /home/volkan/find-directory-example/ -type d -name "log*"
Or
find /home/volkan/find-directory-example/ -type d -iname "log*"
to ignore case sensitivity.
Output:
/home/volkan/find-directory-example/log-files
5.3.2 - Find Only Directories Ending With
Let us look for all directories starting to search from "/home/volkan/find-directory-example" directory and ending with a case-insensitive "files" value using the -iname
parameter.
find /home/volkan/find-directory-example/ -type d -iname "*files"
Output:
/home/volkan/find-directory-example/log-files
/home/volkan/find-directory-example/log-files/Sub-Log-Files
/home/volkan/find-directory-example/dump-files
/home/volkan/find-directory-example/dump-files/Sub-Dump-Files
5.3.3 - Find Only Directories Starting and Ending With
Let us look for all directories starting to search from the "/home/volkan/find-directory-example" directory and starting with "sub" value and ending with "files" value with a case-insensitive using the -iname
parameter
find /home/volkan/find-directory-example/ -type d -iname "sub*files"
Output
/home/volkan/find-directory-example/log-files/Sub-Log-Files
/home/volkan/find-directory-example/dump-files/Sub-Dump-Files
5.3.4 - Find Only Directories Containing and Ending With
Let us look for all directories starting to search from the "/home/volkan/find-directory-example" directory and containing "lo" naming value and ending with "files" value with a case-insensitive using the -iname
parameter
find /home/volkan/find-directory-example/ -type d -iname "*lo*files"
Output:
/home/volkan/find-directory-example/log-files
/home/volkan/find-directory-example/log-files/Sub-Log-Files
5.3.5 - Find Only Directories With Any Single Character (?)
Let us look for all directories starting to search from the "/home/volkan/find-directory-example" directory and containing "l (any-value)
g" naming value using the ?
wildcard expression.
find /home/volkan/find-directory-example/ -type d -iname "*l?g*"
Output:
/home/volkan/find-directory-example/log-files
/home/volkan/find-directory-example/log-files/Sub-Log-Files
5.4 - Find Only Directories With Created Time
5.4.1 - ctime option
Let's find the all directories that were created in 1 day using the -ctime
parameter.
find /home/volkan/find-directory-example/ -type d -ctime -1
Output
/home/volkan/find-directory-example/
/home/volkan/find-directory-example/log-files
/home/volkan/find-directory-example/log-files/Sub-Log-Files
/home/volkan/find-directory-example/empty
/home/volkan/find-directory-example/.hidden-folder
/home/volkan/find-directory-example/dump-files
/home/volkan/find-directory-example/dump-files/Sub-Dump-Files
5.5.2 - cmin option
Let's create a new directory with the name "test-directory-for-time-examples. Then, look at the directories that were created in 10 minutes and start to search from "/home/volkan/find-directory-example" directory
find /home/volkan/find-directory-example/ -type d -cmin -10
Output
/home/volkan/find-directory-example/
/home/volkan/find-directory-example/test-directory-for-time-examples
5.5.3 - Example -newerct option
Let's look at the directories with the created time greater than using the -newerct
and starting to search from "/home/volkan/find-directory-example" directory
find /home/volkan/find-directory-example/ -type d -newerct "2023-08-13 17:00:00"
Output:
/home/volkan/find-directory-example/
/home/volkan/find-directory-example/test-directory-for-time-examples
5.5.4 - Example of -neverct option - Between Dates
Let's find all directories with created time between two dates using the -newerct
option and starting to search from "/home/volkan/find-directory-example" directory
find /home/volkan/find-directory-example/ -type d -newerct "2023-08-13 17:00:00" ! -newerct "2023-08-13 17:35:00"
Output:
/home/volkan/find-directory-example/
/home/volkan/find-directory-example/test-directory-for-time-examples
5.5 - Find Only Directories With Modification Time
5.5.1 - Find Directories with mtime parameter option
Let's find all directories that were modified in 1 day and start to search from "home/volkan/find-directory-example" directory.
find /home/volkan/find-directory-example/ -type d -mtime -1
Output
/home/volkan/find-directory-example/
/home/volkan/find-directory-example/test-directory-for-time-examples
/home/volkan/find-directory-example/log-files
/home/volkan/find-directory-example/log-files/Sub-Log-Files
/home/volkan/find-directory-example/empty
/home/volkan/find-directory-example/.hidden-folder
/home/volkan/find-directory-example/dump-files
/home/volkan/find-directory-example/dump-files/Sub-Dump-Files
5.5.2 - mmin option
We created a new directory with the name of "test-directory-for-time-examples" in the before example. Then, look at the directories that were modified in 1 minute and starting search from "/home/volkan/find-directory-example" directory
Firstly
find /home/volkan/find-directory-example/ -type d -mmin -1
And Output: nothing to see.
Then let's modify the "test-directory-for-time-examples" directory (Create a new file inside it etc.) then again execute the same find command;
find /home/volkan/find-directory-example/ -type d -mmin -1
and output:
/home/volkan/find-directory-example/test-directory-for-time-examples
5.5.3 - Example -newermt option
Let's look at the directories with created time greater than using the -newermt
option, and start to search from "/home/volkan/find-directory-example" directory
find /home/volkan/find-directory-example/ -type d -newermt "2023-08-13 17:00:00"
Output:
/home/volkan/find-directory-example/
/home/volkan/find-directory-example/test-directory-for-time-examples
5.5.4 - Example of -neverct option - Modification Date Between Two Dates
Let's look at the directories with created time between two dates using the -newermt
option and starting to search from "/home/volkan/find-directory-example" directory.
find /home/volkan/find-directory-example/ -type d -newerct "2023-08-13 17:00:00" ! -newerct "2023-08-13 17:50:00"
Output:
/home/volkan/find-directory-example/
/home/volkan/find-directory-example/test-directory-for-time-examples
5.6- Find Only Files With Access Time
5.6.1 - find atime parameter option
Let's find all directories that were accessed in 1 day using the -atime
parameter and start to search from "home/volkan/find-directory-example" directory.
find /home/volkan/find-directory-example/ -type d -atime -1
Output:
/home/volkan/find-directory-example/
/home/volkan/find-directory-example/test-directory-for-time-examples
/home/volkan/find-directory-example/log-files
/home/volkan/find-directory-example/log-files/Sub-Log-Files
/home/volkan/find-directory-example/empty
/home/volkan/find-directory-example/.hidden-folder
/home/volkan/find-directory-example/dump-files
/home/volkan/find-directory-example/dump-files/Sub-Dump-Files
-atime
parameter for access date in days.-amin
parameter for access date in minutes.
5.6.2 - amin option
We created a new directory with the name "test-directory-for-time-examples" in the before example. Then, look at the directories that were accessed in 1 minute and start the search from "/home/volkan/find-directory-example" directory.
find /home/volkan/find-directory-example/ -type d -amin -1
Output:
/home/volkan/find-directory-example/test-directory-for-time-examples
5.6.3 - Example -newerat option
Let's find all directories with accessed time greater than using the -newerat
and start the search from "/home/volkan/find-directory-example" directory
find /home/volkan/find-directory-example/ -type d -newerat "2023-08-13 17:00:00"
Output:
/home/volkan/find-directory-example/
/home/volkan/find-directory-example/test-directory-for-time-examples
5.6.4 - Example of -neverat option - Between Dates
Let's look at the directories with the accessed time option between two dates using the -newerat
option and starting to search from "/home/volkan/find-directory-example" directory
find /home/volkan/find-directory-example/ -type d -newerat "2023-08-13 17:00:00" ! -newerat "2023-08-13 17:30:00"
Output:
/home/volkan/find-directory-example/
/home/volkan/find-directory-example/test-directory-for-time-examples
5.7 - Find All Directories using with -user Option
Let's find all directories with "user volkan" and start to search from "/home/volkan/find-directory-example" directory
find /home/volkan/find-directory-example/ -type d -user volkan
Output:
/home/volkan/find-directory-example/
/home/volkan/find-directory-example/test-directory-for-time-examples
/home/volkan/find-directory-example/log-files
/home/volkan/find-directory-example/log-files/Sub-Log-Files
/home/volkan/find-directory-example/empty
/home/volkan/find-directory-example/.hidden-folder
/home/volkan/find-directory-example/dump-files
/home/volkan/find-directory-example/dump-files/Sub-Dump-Files
5.8 - Find All Directories With Linux Permission ( 777 e.g)
Let's find all directories with "774" permission and start to search from "/home/volkan/find-directory-example" directory
find /home/volkan/find-directory-example/ -type d -perm 777
Output: Nothing to see
Note: In this example directories' permissions are 775 and directories' permissions are 664
Now, let's find all directories with "664" permission and start to search from "/home/volkan/find-directory-example" directory
find /home/volkan/find-directory-example/ -type d -perm 775
Output
/home/volkan/find-directory-example/
/home/volkan/find-directory-example/test-directory-for-time-examples
/home/volkan/find-directory-example/log-files
/home/volkan/find-directory-example/log-files/Sub-Log-Files
/home/volkan/find-directory-example/empty
/home/volkan/find-directory-example/.hidden-folder
/home/volkan/find-directory-example/dump-files
/home/volkan/find-directory-example/dump-files/Sub-Dump-Files
5.9 - Find All Directories With Linux Group
Let's find all directories using the -group
with a value of "volkan" and start to search from "/home/volkan/find-directory-example" directory.
find /home/volkan/find-directory-example/ -type d -group volkan
Output:
/home/volkan/find-directory-example/
/home/volkan/find-directory-example/test-directory-for-time-examples
/home/volkan/find-directory-example/log-files
/home/volkan/find-directory-example/log-files/Sub-Log-Files
/home/volkan/find-directory-example/empty
/home/volkan/find-directory-example/.hidden-folder
/home/volkan/find-directory-example/dump-files
/home/volkan/find-directory-example/dump-files/Sub-Dump-Files
5.1 Find All Directories With Boolean Operators
We can use -not
, -and
, -or
, etc. operators
5.10.1 - Find command with -not
Let's find all directories using;
- using
-iname
to ignore case sensitive with containing "file" value - using
-iname
to ignore case sensitive with-not
for containing "sub" - Starting the search from the "/home/volkan/find-directory-example" directory.
find /home/volkan/find-directory-example/ -type d -iname "*file*" -not -iname "*sub*"
/home/volkan/find-directory-example/log-files
/home/volkan/find-directory-example/dump-files
All directories are found with by containing the case-insensitive "file" value and do not contain with "sub" value from starting the "/home/volkan/find-directory-example" directory.
5.10.2 - Find command with -or
Let's find all directories using;
- using
-iname
to ignore case sensitive with containing "file" - using
-iname
to ignore case sensitive with-or
containing for "folder" value. - Starting the search from "/home/volkan/find-directory-example" directory.
find /home/volkan/find-directory-example/ -type d -iname "*file*" -or -iname "*folder*"
Output:
/home/volkan/find-directory-example/log-files
/home/volkan/find-directory-example/log-files/Sub-Log-Files
/home/volkan/find-directory-example/.hidden-folder
/home/volkan/find-directory-example/dump-files
/home/volkan/find-directory-example/dump-files/Sub-Dump-Files
All directories are found by containing the case-insensitive "file" value or containing the case-insensitive "folder" value from starting the "/home/volkan/find-directory-example" directory.
5.10.3 - Find command with -and
Let's find all directories using;
- using
-iname
to ignore case sensitive with containing "file" value - using
-iname
to ignore case sensitive with containing "log" using the-and
condition. - Starting the search from "/home/volkan/find-directory-example" directory.
find /home/volkan/find-directory-example/ -type d -iname "*file*" -and -iname "*log*"
Output:
/home/volkan/find-directory-example/log-files
/home/volkan/find-directory-example/log-files/Sub-Log-Files
All directories are found by containing the case-insensitive "file" value and containing the case-insensitive "log" value from starting the "/home/volkan/find-directory-example" directory.
5.11 - Find Only Directories in Hidden Folder
In this example, we just make sure that find command can find for the hidden directories in the hidden folders.
For this example, Let's create a new folder under the "/home/volkan/find-directory-example/.hidden-folder/" directory with ".hidden-sub-folder".
find /home/volkan/find-directory-example/ -type d -iname "*hidden*"
Output:
/home/volkan/find-directory-example/.hidden-folder
/home/volkan/find-directory-example/.hidden-folder/.hidden-sub-folder
The hidden ".hidden-folder" and its sub folder ".hidden-sub-folder" as hidden are found.
5.12 - Find Only Empty Directories
Let's find all empty directories using the -empty
and start to search from "/home/volkan/find-directory-example" directory.
find /home/volkan/find-directory-example/ -type d -empty
Output:
/home/volkan/find-directory-example/log-files/Sub-Log-Files
/home/volkan/find-directory-example/empty
/home/volkan/find-directory-example/.hidden-folder/.hidden-sub-folder
/home/volkan/find-directory-example/dump-files/Sub-Dump-Files
All empty folders are found. "hidden-sub-folder" was created in previous example.
6 - Combining Find Command Example
Let's search for;
- Starting directory of "/home/volkan/find-directory-example/"
- Only for directories
-type d
- User volkan
-user volkan
- Permission 664
-perm 775
- Created in one day.
ctime -1
- Ignore case sensitivity
-iname
and Contains "SUB" value and ends with "FILES" value
find /home/volkan/find-directory-example/ -type d -perm 775 -user volkan -ctime -1 -iname "*SUB*FILES"
Or
find /home/volkan/find-directory-example/ -type d -perm 775 -user volkan -ctime -1 -iname "*SUB*" -and -iname "*FILES"
Output
/home/volkan/find-directory-example/log-files/Sub-Log-Files
/home/volkan/find-directory-example/dump-files/Sub-Dump-Files
Conclusion
In this article, we examined and explain basically the find command and its some parameters to find the directories in the Linux system. Also, we tried some examples for them. More examples could be made and the document can be viewed for more information. (In the reference)
Reference