List Files by Extension: Methods for Every OS
In this article
To list files in a directory that match a specific filename extension pattern, you use a "wildcard" character (usually the asterisk *) combined with the extension in a command-line interface or a programming script. For example, in a terminal, typing ls *.jpg on Linux or dir *.jpg on Windows will return all files ending in .jpg.
Understanding Globbing and Wildcards
The process of matching filenames using patterns is technically known as "globbing." A glob is a string of characters that represents a set of filenames. The most common wildcard is the asterisk (*), which acts as a placeholder for zero or more characters of any type. When you search for *.txt, the system interprets this as: "find any sequence of characters that ends specifically with the dot and the letters 'txt'."
Beyond the basic asterisk, some systems support more advanced patterns:
- Question Mark (
?): Matches exactly one single character. For example,file?.txtwould matchfile1.txtbut notfile10.txt. - Square Brackets (
[ ]): Matches any one character contained within the brackets. For example,image[123].pngmatchesimage1.png,image2.png, orimage3.png. - Curly Braces (
{ }): Used in some shells (like Bash) to match a comma-separated list of patterns. For example,*.{jpg,png,gif}matches any of those three image formats.
Listing Files in Windows Command Prompt and PowerShell
Windows provides two primary command-line environments, each with its own syntax for filtering files by extension.
In the traditional Command Prompt (cmd.exe), the dir command is used. To list files of a specific type, use the following syntax:
- Open the Command Prompt.
- Navigate to the target directory using
cd path/to/folder. - Type
dir *.extension(e.g.,dir *.pdf) and press Enter. - To search subdirectories as well, add the
/sflag:dir *.pdf /s.
In PowerShell, the Get-ChildItem cmdlet (aliased as ls or dir) is more powerful. To filter by extension, use the -Filter parameter:
- Basic filter:
Get-ChildItem -Filter *.docx - Recursive search:
Get-ChildItem -Filter *.docx -Recurse - Filtering multiple extensions:
Get-ChildItem -Include *.jpg, *.png -Recurse(Note:-Includerequires the path to end in a wildcard like*to function correctly in some versions).
Listing Files in Linux and macOS (Unix-like)
Unix-based systems use the ls command for listing files and the find command for more complex searches. Because the shell (Bash or Zsh) handles the wildcard expansion before passing the list to the command, it is highly efficient.
Using ls for current directory matches:
ls *.sh: Lists all shell scripts in the current folder.ls -l *.sh: Lists the files in long format, showing permissions and file size.
Using find for deep directory searches:
The find command is preferred when you need to search through nested folders or perform actions on the results. The syntax is find [path] -name "[pattern]".
- To find all .log files in the current directory and all subdirectories:
find . -name "*.log". - To perform a case-insensitive search (matching .JPG and .jpg):
find . -iname "*.jpg". - To find files that do not match an extension:
find . -not -name "*.txt".
Programmatic Methods: Python Implementation
When building software, you often need to list files by extension automatically. Python provides two primary libraries for this: glob and os.
The glob module is the most direct way to implement shell-style wildcards. A simple implementation looks like this:
import globfiles = glob.glob('*.csv'): This returns a list of all CSV files in the current working directory.files = glob.glob('**/ *.csv', recursive=True): This finds all CSV files in the current directory and all sub-folders.
Alternatively, the os.listdir() method combined with str.endswith() provides more control but requires more code:
- Iterate through
os.listdir('.'). - Use an
if file.endswith('.csv'):block to filter the results.
Common Problems and Misconceptions
One common mistake is forgetting that file extensions can be case-sensitive depending on the Operating System. On Linux, *.jpg will not find IMAGE.JPG. Users must use the -iname flag in find or convert the string to lowercase in a script to ensure all files are captured.
Another point of confusion is the difference between a filter and a search. A filter (like ls *.txt) asks the system to show only items that match a pattern. A search (like find) actively crawls the file system tree to locate items. Using ls on a directory with millions of files can sometimes lead to an "Argument list too long" error because the shell expands the wildcard into a massive list of individual filenames before executing the command.
Frequently Asked Questions
How do I list files with multiple different extensions at once?
In Linux/macOS Bash, use curly braces: ls *.{jpg,png,gif}. In Windows PowerShell, use the -Include parameter with a comma-separated list: Get-ChildItem -Include *.jpg, *.png. In Python, you can use a list comprehension to filter for multiple extensions.
Can I list files that don't have any extension?
Yes. In Linux, you can use the find command with a regex or by searching for files that do not contain a dot in the filename: find . -type f ! -name "*.*". In Windows, you can use dir /b | findstr /v "\." to filter out any filename containing a period.
Does the asterisk match hidden files?
Generally, no. In Unix-like systems, files starting with a dot (e.g., .htaccess) are considered hidden and are ignored by the * wildcard. To include hidden files, you must explicitly include the dot in your pattern, such as ls .*, or use the -a flag with ls.
Is there a way to list files by extension in a GUI?
Yes. In Windows File Explorer, type ext:.jpg or simply *.jpg into the search bar at the top right. In macOS Finder, use the search bar and select "Kind" as the filter, or type kind:image to see all image extensions.