1. Overview

Navigating a clutter of files to look for specific file types can be time-consuming when we manually search for them. Automating the process of looking for specific file types with powerful Linux commands can improve our workflow and allow us to stay focused on the task at hand.

In this tutorial, we’ll learn different ways to list files with multiple extensions on Linux.

All instructions below have been tested on Ubuntu 20.04.6 LTS.

2. Using ls Command With Brace Expansions

The ls command lists all files in a directory. However, combining it with braces and wildcards, we can use it to list files ending in different extensions saving the time and effort to look for them singly.

Let’s use this command to list files ending in .txt or .csv:

$ ls *.{txt,csv}
checksums.txt  example2.csv  example3.txt  example5.csv
example1.csv   example2.txt  example4.csv  example5.txt
example1.txt   example3.csv  example4.txt

Thus, by using braces and wildcards with the ls command we can list files with multiple extensions simultaneously.

3. Using find Command With -iname and -or Options

The find utility in Linux locates files based on their names.

Using the find command with the -iname option ensures that the utility performs a case-insensitive name match. Particularly, the uppercase and lowercase form of a character in a filename would be considered equivalent for matching purposes.

The -or option is a logical operator that tells the find command to consider file extensions matching either the previous condition or the following one.

In the example below, we use the find command with -iname and -or options to list files ending in .txt or .csv in the current directory:

$ find -iname '*.txt' -or -iname '*.csv'
./example3.txt
./example1.csv
./case1.TXT
./example2.txt
./example5.csv
./example1.txt
./example3.csv
./example2.csv
./case1.CSV
./example5.txt
./checksums.txt
./case2.CSV
./example4.csv
./example4.txt
./case2.TXT

As seen in the example above, the additional options ensure a case-insensitive search for filenames with mixed case extensions is covered.

4. Using find Command With Regular Expressions

The find utility combined with regular expressions (regex) to match specific file patterns.

Let’s use the find command with regex to list files ending in .txt or .csv in the current directory:

$ find . -iregex '.*\.\(csv\|txt\)' -printf '%f\n'
example3.txt
example1.csv
case1.TXT
example2.txt
example5.csv
example1.txt
example3.csv
example2.csv
case1.CSV
example5.txt
checksums.txt
case2.CSV
example4.csv
example4.txt
case2.TXT

Here’s a breakdown of this command. First, we use the find command to locate files in the current directory:

find .

Then, we instruct the find command to use an extended regular expression to target files ending in .txt or .csv regardless of their case:

-iregex '.*\.\(csv\|txt\)'

Finally, we customize the output to display only the filename without paths, making it easy to read:

-printf '%f\n'

Therefore, using regular expressions with the find command is very efficient and precise for listing multiple files with different extensions.

5. Conclusion

In this article, we’ve learned how to list files with multiple extensions using the ls and find commands.

By using these commands, we can navigate through the Linux filesystem and find the files we need by searching for them by their file extensions.

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments