How To Use Find and Locate Command to Find a Folder in Linux
This post will guide you how to find files or folders using find or locate command from the command line in Your Linux operating systems. How do I find directories from the command line in Linux.
- Find Command
- Locate Command
- Find Directories using Find Command
- Find Directories Using Locate Command
Find Command
GNU find searches the directory tree rooted at each given starting-point by evaluating the given expression from left to right, according to the rules of precedence (see section OPERATORS), until the outcome is known (the left hand side is false for and operations, true for or), at which point find moves on to the next file name. If no starting-point is specified, `.’ is assumed.
The syntax of the Find command is as follows:
find [-H] [-L] [-P] [-D debugopts] [-Olevel] [starting-point...] [expression]
Locate Command
locate reads one or more databases prepared by updatedb(8) and writes file names matching at least one of the PATTERNs to standard output, one per line. If –regex is not specified, PATTERNs can contain globbing characters. If any PATTERN contains no globbing characters, locate behaves as if the pattern were *PATTERN*. By default, locate does not check whether files found in database still exist (but it does require all parent directories to exist if the database was built with –require-visibility no). locate can never report files created after the most recent update of the relevant database.
The syntax of the Locate command is as follows:
locate [OPTION]... PATTERN...
Find Directories using Find Command
If you only want to find directory in a given directory in your Linux system, and you can use the find command, type:
$ find /searchPath/ -name "folderPattern" -type d
For example, you want to find all directories called “cache” under / directory, and you can run the following find command:
$ find / -type d -name cache
Outputs:
devops@devops:~$ find / -type d -name cache
......
find: ‘/lost+found’: Permission denied
find: ‘/sys/kernel/debug’: Permission denied
/sys/devices/system/cpu/cpu0/cache
find: ‘/sys/fs/pstore’: Permission denied
find: ‘/sys/fs/fuse/connections/48’: Permission denied
/home/devops/myphp/storage/framework/cache
/home/devops/myphp/bootstrap/cache
find: ‘/home/devops/.config/Slack’: Permission denied
/home/devops/tmp/wildfly-17.0.1.Final/modules/system/layers/base/org/wildfly/clustering/ee/cache
/home/devops/tmp/wildfly-17.0.1.Final/modules/system/layers/base/org/wildfly/clustering/web/cache
/home/devops/.composer/cache
If you want to hide all “Permission denied” error message while using the above find command, and you can redirect the standard error output to /dev/null. type the following command:
$ find / -type d -name cache 2>/dev/null
Outputs:
devops@devops:~$ find / -type d -name cache 2>/dev/null
/snap/gnome-3-26-1604/90/var/cache
/snap/gnome-3-26-1604/92/var/cache
/snap/core/7396/etc/apparmor.d/cache
/snap/core/7396/var/cache
/snap/core/7396/var/lib/ucf/cache
/snap/core/7713/etc/apparmor.d/cache
/snap/core/7713/var/cache
/snap/core/7713/var/lib/ucf/cache
/snap/core18/1144/etc/apparmor.d/cache
/snap/core18/1144/var/cache
/snap/core18/1144/var/lib/snapd/cache
/snap/core18/1144/var/lib/ucf/cache
/snap/core18/1098/etc/apparmor.d/cache
/snap/core18/1098/var/cache
/snap/core18/1098/var/lib/snapd/cache
/snap/core18/1098/var/lib/ucf/cache
/snap/gnome-3-28-1804/71/var/cache
/snap/gnome-3-28-1804/67/var/cache
/var/spool/libreoffice/uno_packages/cache
......
Find Directories Using Locate Command
You can also use another command called “locate” to find only folder in your Linux system.
For example, you want to search for a folder named “cache” in / directory, just type:
$ locate -b "\cache"
Outputs:
devops@devops:~$ locate -b "\cache"
/etc/apparmor.d/cache
/home/devops/.composer/cache
/home/devops/myphp/bootstrap/cache
/home/devops/myphp/storage/framework/cache
/home/devops/tmp/wildfly-17.0.1.Final/modules/system/layers/base/org/wildfly/clustering/ee/cache
/home/devops/tmp/wildfly-17.0.1.Final/modules/system/layers/base/org/wildfly/clustering/web/cache
/opt/wildfly/modules/system/layers/base/org/wildfly/clustering/ee/cache
/opt/wildfly/modules/system/layers/base/org/wildfly/clustering/web/cache
/snap/core/7396/etc/apparmor.d/cache
/snap/core/7396/var/cache
/snap/core/7396/var/lib/ucf/cache
/snap/core/7713/etc/apparmor.d/cache
/snap/core/7713/var/cache
/snap/core/7713/var/lib/ucf/cache
/snap/core18/1098/etc/apparmor.d/cache
/snap/core18/1098/var/cache
/snap/core18/1098/var/lib/snapd/cache
/snap/core18/1098/var/lib/ucf/cache
/snap/core18/1144/etc/apparmor.d/cache
/snap/core18/1144/var/cache
/snap/core18/1144/var/lib/snapd/cache
/snap/core18/1144/var/lib/ucf/cache
/snap/gnome-3-26-1604/90/var/cache
/snap/gnome-3-26-1604/92/var/cache
/snap/gnome-3-28-1804/67/var/cache
.......
If you want to see more help about find and locate commands, you can run the following commands:
$ man find $ man locate
Conclusion
You should know that how to find a folder from a directory using find/locate commands in your Linux system.