1. Overview

Often, we need to perform complex file management tasks in a Linux environment. One such task is searching for all files that start with a specific string. Additionally, this operation is performed in different scenarios, including data organization and project management.

In this tutorial, we’ll check out the methods to search for all files starting with a specific string using the find, tree, ls, and locate commands.

2. Using the find Command

In Unix-like operating systems, the find command is used for searching and locating files in a directory hierarchy. In addition, we can also utilize it to specify search criteria such as when searching for all files starting with a specific string.

Let’s search for all files starting with the test pattern:

$ find . -type f -name "test*"
./Downloads/test_article2
./snap/test_article1
./Public/test_article4
./Documents/test_article3

In the above script:

  • find command searches for the files
  • . specifies the current directory as the starting directory of the search
  • -type with f flag filters the search to include only regular files, not directories
  • -name represents the search pattern
  • test represents the search pattern that uses the wildcard * to match any characters after it

As a result, the output shows all files starting with the specified pattern in the current directory.

3. Using the tree Command

tree is primarily used to display the directory structure in a tree-like format. Moreover, we can also use it to visualize the organization of the searched files that start with a specific string.

For this purpose, let’s use the tree command in our terminal:

$ tree -P 'test*'
.
├── Desktop
├── Documents
│   └── test_article3
├── Downloads
│   ├── firefox.tmp
│   │   └── firefox
│   └── test_article2
├── Music
├── Pictures
├── Public
│   └── test_article4
. . .

In the script above, we use the tree command with -P or pattern option to display the directory structure in a tree-like format. However, it only includes items whose names start with the test string.

Now, we’ll filter the output and display only lines containing the test string:

$ tree | grep test
│   └── test_article3
│   └── test_article2
│   └── test_article4
│   └── test_article1

Here, the pipe (|) operator redirects the output of the tree command to grep. Then, grep filters the output, searches for files containing test patterns, and displays them on the terminal.

4. Using the ls Command

The ls command is used for listing files and directories within a specified location. However, we can also customize it based on specific requirements such as searching and listing all files that start with a specific string.

First, we’ll navigate to our Documents directory with cd:

$ cd Documents

Next, let’s run the ls command for listing files in the current directory that start with a specific string:

$ ls test*
test_article3

The output displays that our Documents directory only contains one file that starts with the test string.

5. Using the locate Command

We can also use the locate command to find out the location of files with specific criteria in a pre-built database accordingly.

Therefore, in this case, we’ll use locate to search for the files having a specific pattern at the start:

$ locate /test_article
/home/author/Documents/test_article3
/home/author/Downloads/test_article2
/home/author/Public/test_article4
/home/author/snap/test_article1

Here, the locate command with / only looks for the files starting with the test_article pattern.

As a result, the output shows the complete path of the located files that start with the specified pattern.

6. Conclusion

In this article, we explored several methods to search for all files starting with a specific string. These methods included the find, tree, ls, and locate commands.

Specifically, we can use the find command to perform file-searching operations quickly and easily.

The tree command provides a visual representation of directories containing the specific files.

Moreover, we can also use the ls command to get detailed information about the matched files. Furthermore, the locate command can speedily search for the location of files based on the given pattern.

Ultimately, we can choose any of these methods depending on the details we want to get about the files that start with a specific string.