User Agents

Linux List Files with Extensions: Commands That Work

Linux does not store file extensions as separate metadata; an extension is simply part of the filename. To list files with a given extension, match filename patterns using shell globs with ls, or use find when the search needs to be recursive or more precise.

For a quick current-directory listing, ls -1 -- *.txt is the usual pattern. For a recursive search, find . -type f -name '*.txt' lists matching regular files below the current directory. The details matter because Linux filename matching is case-sensitive by default, hidden files behave differently in shell globs, and a suffix is not proof of a file's content type.

How Linux Treats File Extensions

A Linux filesystem stores a filename, an inode number, ownership, permissions, timestamps, and other metadata. It does not store a separate “extension” field. The dot in report.txt is an ordinary character in the name, just like the letters. Consequently, report, report.txt, and report.tar.gz are three different names, and the system does not inherently know that one is a text file, one is a compressed archive, or one has no suffix.

Many programs use suffixes as a convention. Text editors may associate .txt with plain text, image viewers may expect .jpg, and archive tools may recognize .tar.gz. The kernel and core utilities do not require those conventions. An executable shell script can be named backup with no extension and still run if it has execute permission and a valid shebang line such as #!/bin/sh. Conversely, a file named notes.txt can contain binary data; the name alone does not guarantee the format.

  • An extension is part of the filename, not a filesystem attribute.
  • Case usually matters: *.TXT and *.txt are different patterns in common shells and in find.
  • A name can contain several dots, so *.tar.gz matches the full suffix while *.gz matches any name ending in .gz.
  • Hidden files begin with a dot, such as .profile; shell globs normally skip them unless the shell is configured to include them.

Listing Extensions in the Current Directory

The simplest method is a shell glob. Before ls runs, the shell expands *.txt into the matching filenames in the current directory. The command

ls -1 -- *.txt

prints one name per line; the -- stops option parsing so a filename beginning with a hyphen is treated as a name rather than an option. If you want no ls formatting at all, printf '%s\n' *.txt is a useful alternative because it prints each expanded argument followed by a newline.

To match more than one suffix in a single glob, use multiple patterns or brace expansion where the shell supports it. In Bash and similar shells, ls -1 -- *.{jpg,png} expands to the .jpg and .png patterns. A more portable form is ls -1 -- *.jpg *.png. Remember that glob expansion happens before the command, so quoting the pattern prevents expansion: ls -1 -- '*.txt' asks ls for a literal file named *.txt, which is usually not what you want.

GNU ls also has a -X option that sorts entries by extension, but it does not filter by extension. ls -X still lists all non-hidden entries in the directory, merely changing the sort order. Filtering and sorting are separate jobs.

Recursive Searches with find

find is the right tool when you need to search below the current directory, limit by file type, combine patterns, or handle unusual filenames safely. The basic recursive search for .txt files is:

find . -type f -name '*.txt'

Here . is the starting directory, -type f restricts results to regular files, and -name '*.txt' matches the filename pattern. To search only the current directory without descending into subdirectories, add -maxdepth 1 before the tests:

find . -maxdepth 1 -type f -name '*.txt'

Multiple extensions need grouping so the file-type test applies to the whole expression:

find . -type f \( -name '*.jpg' -o -name '*.png' \)

The escaped parentheses group the two name tests; the shell must receive them as literal parentheses, so the backslashes are required in Bash. Case-insensitive matching is available with -iname on GNU find and many BSD versions:

find . -type f -iname '*.jpg'

For names that may contain spaces, tabs, or newlines, avoid piping plain find output into another command. Use NUL-separated output instead:

find . -type f -name '*.txt' -print0 | xargs -0 ls -l

GNU xargs users can add -r to avoid running the command when there are no matches. A while loop with read -d '' is another safe approach. These details are not necessary for a one-off look at a few files, but they prevent breakage in scripts.

Files Without Extensions and Mixed Patterns

Linux has no built-in concept of “a file with no extension,” so you have to describe the name pattern you mean. A common approximation is:

find . -maxdepth 1 -type f ! -name '*.*'

This lists regular files in the current directory whose names do not contain a dot. It excludes report.txt and also excludes hidden dotfiles such as .bashrc, because the pattern *.* matches those names in find. It also excludes a file named report. and names like version.2, even though a person might not consider those true extensions. That is why the command is an approximation rather than a precise extension test.

If you need to inspect the last suffix of each name, shell parameter expansion can help. In Bash, ${name##*.} removes everything up to the final dot, leaving the text after it. A small loop can compare that value, but such a loop must handle files with no dot, names beginning with a dot, and names ending with a dot. For most practical searches, a name pattern is simpler and less error-prone.

To list names that contain a dot anywhere, including hidden files, use find . -maxdepth 1 -type f -name '*.*'. To see only names ending in a specific suffix, prefer the specific pattern, such as -name '*.conf', rather than a broad “has a dot” test.

Case, Hidden Files, and Multiple Dots

Case sensitivity is a frequent source of confusion. In Bash, *.txt does not match REPORT.TXT. With find, use -iname for a case-insensitive name match. In Bash, shopt -s nocaseglob makes pathname expansion case-insensitive for the current shell; other shells have their own options. Do not assume that a pattern works the same way across all shells.

Hidden files are another difference. A shell glob such as *.txt normally does not match .notes.txt, because the leading dot is hidden from ordinary expansion. ls -a -- *.txt does not fix that: the shell still expands *.txt before ls sees it, and the hidden name was never in the expansion. To include hidden names in Bash, enable dotglob or use find, whose -name '*.txt' pattern can match a leading dot. Test the behavior in your shell before relying on it.

Multiple dots are matched literally. *.tar.gz matches names ending in .tar.gz, while *.gz matches any name ending in .gz, including backup.tar.gz and notes.gz. A pattern such as *.tar.* matches .tar.gz, .tar.bz2, and similar names, but it can also match unintended names. Exactness is usually better than cleverness when the goal is a reliable list.

Common Problems and Safer Habits

  • No matching files: In Bash, an unmatched glob is passed through literally by default, so ls *.txt can produce a “No such file or directory” error for the literal name *.txt. Use find or enable nullglob if you want an empty result instead.
  • Filenames with spaces: Quoting matters. for f in *.txt; do printf '%s\n' "$f"; done handles spaces because the variable is quoted. Avoid unquoted command substitution around filenames.
  • Leading hyphens: Use -- before filename arguments, as in ls -1 -- *.txt, so a name like -notes.txt is not read as an option.
  • Too many arguments: A glob that expands to a very large number of names can exceed the command-line argument limit. find ... -exec or NUL-separated pipelines avoid that limit.
  • Directories and symlinks: -type f excludes directories. If you need to follow symbolic links, check your find implementation's -L option and use it deliberately.
  • Parsing ls output: Human-readable ls output is not a stable data format. For scripts, prefer find, printf, or a small loop with quoted variables.

The file command answers a different question: what kind of data is inside a file? Running file -- *.txt inspects the named files and reports likely content types; it does not filter a directory by extension. This is useful when a suffix is missing or suspicious, but it is not a substitute for filename pattern matching.

For content search rather than filename search, tools such as grep -l can list files containing a pattern, but they do not select by extension unless you combine them with a filename list. Some systems provide indexed search tools such as locate; their databases may be stale and their pattern syntax is not identical to find, so verify before depending on them. Optional third-party tools can make extension searches shorter, but they are not guaranteed to be installed on a default Linux system. The portable answer remains shell globs for simple current-directory work and find for recursive or scripted work.

Frequently Asked Questions

How do I list only .txt files in the current directory?

Use ls -1 -- *.txt for a quick interactive listing. For a safer result when there may be no matches, use find . -maxdepth 1 -type f -name '*.txt'.

Can find search for a file extension recursively?

Yes. find . -type f -name '*.txt' searches the current directory and all subdirectories. Add -maxdepth 1 to keep the search in the current directory only.

Are Linux file extensions case-sensitive?

Filename matching is case-sensitive by default in common shells and in find -name. Use find ... -iname '*.txt' for case-insensitive matching, or configure the shell's glob behavior deliberately.

How do I match several extensions at once?

With find, group the name tests: find . -type f \( -name '*.jpg' -o -name '*.png' \). In Bash, brace expansion such as *.{jpg,png} can also produce multiple patterns for a simple current-directory glob.

How do I list files with no extension?

A common approximation is find . -maxdepth 1 -type f ! -name '*.*'. It excludes names containing any dot, including hidden dotfiles and names such as version.2, so inspect the result rather than treating it as a perfect extension test.

Does Linux require a file extension to run a program or script?

No. The filesystem does not require an extension. Execution depends on permission and, for scripts, a valid interpreter line; applications may still use suffixes by convention.