Find a Directory in Linux
In Linux, you create multiple files and directories and frequently navigate in between but there are times when the number of directories is so huge that finding it is next to impossible.
So what options do you have in that case?
You can use the find command with multiple parameters to look for that specific directory hidden in the heavens. Use -type d
to indicate that you are specifically looking for directories, not files.
Let me show you how.
1. Find a directory using its name
By far, this is the most common and simple way to search for a directory where you know the name of the directory and where to find it and that's it!
To find directories using their names, you can use the find command in the following manner:
find /path/to/search -type d -name directory_name
Here,
/path/to/search
is where you will give the path of the directory where you want to initiate the search.type -d
indicates you want to search for the directories only.-name
indicates you want to indicate the name search.directory_name
is where you enter the name of the directory which you want to search.
Let's suppose you want to search for a directory named Downloads
in the home directory, then, I will be using the following:
find ~/ -type d -name "Downloads"
2. Find a directory modified on a specific date
If you want to list the directories that were modified on a specific date, then, you can use the find command in the following manner:
find /path/to/search -type d -newermt "YYYY-MM-DD" ! -newermt "YYYY-MM-DD"
Here, the -newermt
flag was used two times. One to specify the lower limit and another one was used for the upper limit indicating on that date or before that.
By doing so, you create a time frame of two dates.
For example, if I want to list all the directories modified between the 16th and 18th of Jul 2023, then, I will be using the following:
3. Find directories modified in the last n days
To look for directories that were modified in the last n days, you can use the -mtime
flag with the find command:
find /path/to/search -type d -mtime -days
So if I want to find directories modified in the last 7 days, then, I will be using the following:
find ~/Test/ -type d -mtime -7
4. Find a directory modified more than N days ago
Similar to the above, if you want to find directories that were modified in more than N days ago, then you'd have to use the -mtime
command in the following manner:
find /path/to/search -type d -mtime +days
For example, if I want to find directories that were modified more than 7 days ago, then, I will be using the following:
find ~/Test/ -type d -mtime +7
5. Find empty directories
To find empty directories, all you have to do is use the -empty
flag as shown:
find /path/to/search -type d -empty
Let's say I want to find empty directories inside the Test
directory, then, I will be using the following:
find ~/Test/ -type d -empty
Here's how to check the size of a directory
If you're curious to know how to check the size of the directory, then, you can follow our detailed guide on how to check the size of the directory:
I hope you will find this guide helpful.