How To Find Directory In Linux? – POFTUT

How To Find Directory In Linux?


Linux provides different ways to find directories. Here we will look at how to find directories in a recursive way. In this tutorial, we will use commands like  find and locate.

Find Directories and Files with find Command

Find command is a popular command to used a lot of different purposes like find file and directory, take a backup, copy files. Actually find do not have these abilities it just runs commands over search results like copy and backup.

Syntax

find LOCATION SEARCH_TERM ACTION
  • `LOCATION` is the path or location we will search in
  • `SEARCH_TERM` is the term which is the file or directory name we want to search
  • `ACTION` is optional which can take actions like print, delete, rename in the search results

We will search for a directory bin in the root file system.

$ find / -name bin
Find Command
Find Command

Get Detailed Information about The Directories and Files

We can get detailed information about found directories by using -ls parameter for the find command.

$ find / -name bin -ls
Get Detailed Information
Get Detailed Information

Here results will list the size of the directory permission of the directory, owner, last change date, etc.

Only List Directories

Up to now, we have searched for all files and directories. We can search for only directories by providing a type parameter with a directory specifier.

$ find / -name bin -ls -type d
Only List Directories
Only List Directories

Locate

Locate command is a non-interactive alternative to find command. Also, locate have restricted capabilities. The advantage of the locate command is that it is fast because locate use database to search. Manually a database for file and directories is created. Manually this database is updated. The search is done directly in this database. Database is located at /var/lib/mlocate/mlocate.db.

We will update our database to search with locate command. To update the locate database we need root privileges.

$ sudo updatedb

We will search for files and directories ends with /bin. In this example, we will use the regex option of the locate to specify the end of the line.

$ locate --regex /bin$
Locate
Locate

LEARN MORE  What Are Inodes In Linux?

Leave a Comment