How To Find Files, Folders and Directories In Linux From Command Line? – POFTUT

How To Find Files, Folders and Directories In Linux From Command Line?

Linux provides different ways to find and locate files and folders. We can use GUI tools like GNOME and KDE file managers or other 3’rd party applications designed for finding files. In this tutorial, we will look at how to find files, folders, and directories from the command line.

Tools To Find Files and Folder In Linux

As stated previously there are a lot of tools that can be used to find files and folders. We will look in detail all of them. Here list of these tools.

  • find
  • locate
  • grep
  • which
  • whereis

Find Command

findcommand is very featureful command used with a lot of different options. More details about find command can be found from the following tutorial.

Linux Find Command With Examples

Find Only Files

We can search only files by providing file type as -type f. We will search files those named conf in this example. We will use the glob start and end of the search term in order to accept any prefix or postfix for the search term. So this will match conffff, myconf, myconfffff, myconfiguration.txt etc.

$ find . -type f -name "*conf*"
Find Only Files
Find Only Files

Alternatively, we can specify the path we want to search for the given file name. We will provide the path according to .. In this example, we will search in the /etc path.

$ find /etc -type f -name "*conf*"
Find Only Files
Find Only Files

Find Only Folders

We may need only to find the folder. We will specify the type like below a directory.

$ find . -type d -name "*conf*"
Find Only Folders
Find Only Folders

Locate Command

locate command can be used as an offline database of all files and folders. locate will search a database which is created with updatedb command. More detailed information can get from the following tutorial.

locate Command Tutorial With Examples For Linux To Find Files

LEARN MORE  What Are Inodes In Linux?

As locate database only holds file and folder names we can not search in detail. But this database provides us very fast search option then find command because it works offline.

Update Database

As stated previously locate uses a database to search files and folders. Updating this database is important before a search. The update will take very little time.

$ updatedb

Search For File or Folders

We will use locate command and the file and folder name to search.

$ locate /home/ismail/*back*
Search For File or Folders
Search For File or Folders

Grep Command

grep command mainly filters given text and files contents but we can use it to find files and folders. For more detail

Introduction to Linux Grep Command With Examples

We can use ls command recursively and grep the files and folder we want to find. In this example, we will search for files and folders whose names contain backup .

$ ls -R -l | grep backup
Grep Command
Grep Command

Which Command

whichcommand is not an actual file and folder search. which command simply search current environment executable files. This is generally useful if we are looking for a command which is not included in PATH variable and can not use automatically.

$ which ls

Whereis Command

whereis command is used to list given search term related binary, source, or man page files. In this example, we will search for ls binary and related man page files.

$ whereis ls

Leave a Comment