Search for files and directories in Linux using the command line interface

You can use the graphical file management utility to find files in Linux like Nautilus in Gnome, Dolphin in KDE and Thunar in Xfce. However, there are several ways to use the command line to find files in any Linux desktop management utility.

Search for files and directories in Linux using the command line interface Picture 1

Use the Find command

The Find command allows users to find files if they know the file name roughly. This is the simplest type of command that searches for files in the current directory that match certain criteria. Users can search for files by name, owner, group, file type, permissions, date and year and many other criteria.

Type the following command to list all files found in the current directory:

tìm.

The dot indicates the current directory.

Search for files and directories in Linux using the command line interface Picture 2

To find files that match a particular pattern, use the ' -name ' parameter. Users can use metacharacters (such as '*' ) but must leave the escape symbol ' ' before each super-character.
For example, if we want to find all the files that start with ' pro ' in the Documents folder, use cd Documents / to move to the Documents folder, then type the following command:

tìm. –Name pro *

All files starting with ' pro ' in the directory will be listed.

Note: The default find statement is case sensitive. If you want to search for a word or phrase that is not case sensitive, use the ' -iname ' option with the find statement.

Search for files and directories in Linux using the command line interface Picture 3

If no file matches the criteria, no results will be displayed.

The find statement has many options for filtering searches. To find out more, type ' man find ' in the Terminal window and press Enter .

Use the locate command

The locate command is faster than the find command because it uses the previously built database while the find command searches the system in real time, from all directories and files. The locate command returns a list of all path names that contain a group of special characters.

The database is updated periodically from cron , but users can update themselves at any time so they can get results immediately. To do this, type the following statement:

sudo updatedb

Enter password.

Search for files and directories in Linux using the command line interface Picture 4

The original form of the locate command finds all files in the file system, starting from the root directory that contains all or any part of the search criteria.

locate mydata

For example, the following statement found two files containing ' mydata ' and a file containing ' data '.

Search for files and directories in Linux using the command line interface Picture 5

If you want to find all files or folders that contain exactly the search criteria, use the '-b' option with the locate command as follows:

locate -b'mydata '

Search for files and directories in Linux using the command line interface Picture 6

The command of mlocate is a variant of locate. Search results include only files that the current user has access to. When you update the mlocate database, it maintains the time stamp information in the database. This allows mlocate to know if the content in a directory is changed without re-reading the content and updating it to a faster database without relying on the hard drive.

When installing mlocate, the / usr / bin / locate binary file changes and points to mlocate. To install mlocate when the command is not in the Linux distribution, type the following command:

sudo apt-get install mlocate

Search for files and directories in Linux using the command line interface Picture 7

The mlocate command does not use the same database file as the standard locate command. Therefore, users can create the database manually by typing the following statement:

sudo /etc/cron.daily/mlocate

The mlocate command will not work until the database is created manually or when the script is run from cron .

Search for files and directories in Linux using the command line interface Picture 8

For detailed information about locate and mlocate statements, type man locate or man mlocate in the command window and press Enter . The help screen will appear.

Use the which command

The which command returns the absolute path of the executable file called when a command is issued. This is useful when you need to find the location of an executable file to create a shortcut to the program on the screen, on a panel or elsewhere in the desktop management utility. For example, typing the command which firefox will display the results as shown below.

Note: By default, the which command only displays the first match. To display the entire result, use the ' -a ' option with the command. The ' which firefox ' command will become ' which -a firefox '.

Search for files and directories in Linux using the command line interface Picture 9

Users can search multiple executable files at the same time. Only the paths to executable files will be displayed. In the example below, only the executable file ' ps ' is found.

Search for files and directories in Linux using the command line interface Picture 10

Note: The which command only finds the path to the executable file that the current user is allowed to use. If searching for an executable file that only the administrator is allowed to use, no results will be displayed.

To learn more about the which command, type man which at the command prompt and press Enter .

Use the whereis command

The whereis command is used to find the location of binary files, source files, or 'man' files for a program. For example, when typing whereis firefox 'we will get the results shown below.

Search for files and directories in Linux using the command line interface Picture 11

If you just want the path to the executable file to display and not display the path to the source file and the ' man ' file, use the ' -b ' option. For example, the 'whereis –b firefox' command will only display the result '/ usr / bin / firefox' . This is very convenient because users will often search for more executable files than search for source and ' man ' files of that program. Users can also search only for source files (' -s ') or 'man' (' -m ') files.

To find out the details of the command, type man whereis in the command prompt and press Enter .

The difference between whereis and which

The whereis command shows the location of binary files, source files, and ' man ' files with only one command, while the which command only shows the binary file location.

The whereis command finds from a special directory list for the binary file, the source file, and the ' man ' file while the which command searches for the directories listed in the authorized PATH environment of the current user. With the whereis command, the special directory list can be found in the FILES section of the ' man ' file.

By default, the whereis command displays all the results found while the which command only displays the first result found.

Because the whereis command only uses the hard-coded path to the command, you may not find the results you expect. If you are looking for a program that you think may have been installed in a directory that is not listed in the 'man' file of the whereis command, use the which command with the '-a' option to find all results from the system.

4 ★ | 2 Vote

May be interested

  • 5 reasons why people love the Linux command line5 reasons why people love the Linux command line
    many people are afraid of the command line. they see it as something for software developers or geeks. but the command line is merely another way of interacting with a pc, and there are some tasks that are very easy to do with the cli.
  • The Cat command in LinuxThe Cat command in Linux
    the cat command (short for 'concatenate') is one of the most frequently used commands in operating systems like linux / unix. the cat command allows users to create one or more files, view file contents, join files, and redirect output in a terminal or file.
  • Append command in WindowsAppend command in Windows
    the append command allows programs to open data files in specified directories. if used without parameters, the append command will display the list of additional directories.
  • The basics of Cisco Switch Management - Part 1The basics of Cisco Switch Management - Part 1
    in the command-line interface cli (command-line interface) you can perform any type of configuration. so you can use the ios command line interface.
  • 8 main differences between Windows and Linux8 main differences between Windows and Linux
    is linux and windows the same? linux is different from windows? that is the question that many people wonder when they first come into contact with the linux operating system. in this article we will help you answer that question.
  • How to Launch Programs from Command Line on LinuxHow to Launch Programs from Command Line on Linux
    most linux distributions have a graphical user interface that allows us to open programs just by clicking on the program icon in the apps menu. however, sometimes you may want to launch programs from terminal. terminal is a powerful tool that allows users to launch programs and manage linux systems through keyboard commands. today's tipsmake will show you how to launch programs from terminal on linux.
  • How to check whether a Linux PC is 64-bit or 32-bit by command lineHow to check whether a Linux PC is 64-bit or 32-bit by command line
    how to check if your linux system is 32-bit or 64-bit? in this article, tipsmake.com will show you how to define it using the gui and the command line.
  • How to Create ISO Files on LinuxHow to Create ISO Files on Linux
    today's tipsmake will show you how to turn multiple files into one iso file on a linux computer. you need to use the linux command line to do it.
  • How to print from the Linux command lineHow to print from the Linux command line
    printing documents from the linux command line is easy. you use the lp command to request printing and lpq to see which print jobs are in the queue.
  • How to move files between systems using scp and rsyncHow to move files between systems using scp and rsync
    there are many tools for moving files between two computers via the linux command line, but the scp and rsync commands are two of the most popular choices, covering almost every use case.