How To List Files In Linux : Filename Pattern Matching Examples

The `ls` command with no arguments lists every non-hidden item in the specified directory. If you’re new to Linux, understanding **how to list files in linux** is one of the first skills you’ll need. This guide covers everything from basic commands to advanced filtering, sorting, and permissions viewing.

Listing files in Linux is straightforward once you know the right commands. The terminal gives you full control over what you see and how you see it.

How To List Files In Linux

This section covers the core `ls` command and its most useful options. You’ll learn to list files in the current directory, show hidden items, and view details like file size and permissions.

Basic File Listing With Ls

The simplest way to list files is typing `ls` and pressing Enter. This shows all non-hidden files and folders in your current directory.

  • Type `ls` and press Enter to see files in the current folder
  • Use `ls /path/to/directory` to list files in a specific location
  • Add `-a` to show all files, including hidden ones (those starting with a dot)
  • Use `-l` for a long listing format with permissions, owner, size, and date

For example, `ls -la` combines both flags to show every file with full details. This is one of the most common commands for inspecting directories.

Listing Files With Human-Readable Sizes

When you use `ls -l`, file sizes appear in bytes by default. That can be hard to read for large files. Add the `-h` flag to get sizes in KB, MB, or GB.

Run `ls -lh` to see file sizes in a format that’s easy to understand. The output shows 1.2K, 45M, or 2.3G instead of raw byte numbers.

Sorting Files In The List

You can sort the output in several ways. The `-t` flag sorts by modification time, newest first. Use `-S` to sort by file size, largest first.

  • `ls -lt` — sort by time, newest first
  • `ls -lS` — sort by size, largest first
  • `ls -ltr` — sort by time in reverse order (oldest first)
  • `ls -lX` — sort alphabetically by file extension

Reverse sorting with `-r` is helpful when you want to see the oldest files or smallest files first. Combine flags like `ls -ltrh` for a detailed, reverse time-sorted list with human-readable sizes.

Listing Files Recursively

Sometimes you need to see files inside subdirectories too. The `-R` flag tells `ls` to list files recursively, going into every folder below the current one.

Type `ls -R` to see a tree-like output. This can be overwhelming in large directories, so consider piping the output to `less` for easier viewing: `ls -R | less`.

Filtering By File Type Or Pattern

You can list only certain types of files using wildcards. The asterisk `*` matches any characters, while `?` matches a single character.

  • `ls *.txt` — list all text files
  • `ls file?.log` — list files like file1.log, fileA.log, but not file10.log
  • `ls [abc]*` — list files starting with a, b, or c

For more advanced filtering, use the `find` command. But for simple pattern matching, `ls` with wildcards works great.

Viewing Hidden Files

Hidden files in Linux start with a dot. They’re not shown by default. To see them, add the `-a` flag.

Run `ls -a` to list all files including `.bashrc`, `.profile`, and other configuration files. The special entries `.` (current directory) and `..` (parent directory) also appear.

If you want to exclude `.` and `..` from the list, use `ls -A` instead. This shows all hidden files except those two directory references.

Using Ls With Color Output

Most modern Linux distributions enable colored output by default. If yours doesn’t, add `–color=auto` to your `ls` command.

Colors help you quickly identify file types: blue for directories, green for executables, red for archives, and so on. You can set this permanently by adding an alias to your `.bashrc` file.

Add this line to make `ls` always use colors: `alias ls=’ls –color=auto’`

Listing Files With Full Path

The `-d` flag combined with `*/` shows only directories. But to get the full path of each file, use the `find` command or combine `ls` with `pwd`.

A simpler method: use `ls -d $PWD/*` to list files with their absolute paths. This is useful when you need to copy paths for scripts or commands.

Common Ls Flags Reference

Here’s a quick reference of the most useful `ls` flags:

  • `-a` — show all files including hidden
  • `-A` — show all except `.` and `..`
  • `-l` — long format with details
  • `-h` — human-readable sizes (use with -l)
  • `-t` — sort by modification time
  • `-S` — sort by file size
  • `-r` — reverse order
  • `-R` — recursive listing
  • `-d` — list directories themselves, not their contents
  • `-1` — list one file per line
  • `-m` — comma-separated output
  • `-Q` — quote file names

Using The Tree Command For Better Visuals

While `ls -R` shows recursive listings, the `tree` command gives a much nicer visual hierarchy. It’s not installed by default on all systems.

Install it with `sudo apt install tree` (Debian/Ubuntu) or `sudo yum install tree` (RHEL/CentOS). Then run `tree` to see a graphical representation of your directory structure.

Use `tree -L 2` to limit the depth to two levels. Add `-d` to show only directories. The `-h` flag adds human-readable file sizes.

Listing Files With Permissions And Ownership

The `ls -l` output includes permission strings like `-rw-r–r–`. The first character indicates the file type: `-` for regular file, `d` for directory, `l` for symbolic link.

The next nine characters show permissions for owner, group, and others. Each group has three positions: read (r), write (w), and execute (x).

For example, `-rwxr-xr–` means the owner can read, write, and execute; group can read and execute; others can only read.

Listing Files By Date And Time

To see when files were last modified, use `ls -l`. The date and time appear in the sixth column. You can sort by this column using `-t`.

For more precise timestamps, add `–full-time` to show the complete date including seconds and timezone. This is useful for scripts that need exact modification times.

Using Ls With Grep For Advanced Filtering

Combine `ls` with `grep` to filter files by name patterns that wildcards can’t handle. For example, list all files containing “report” in their name:

`ls -la | grep report`

This pipes the output of `ls` into `grep`, which searches for lines containing the word “report”. You can use regular expressions for more complex patterns.

Listing Files With Inode Numbers

Every file in Linux has an inode number that identifies it in the filesystem. Use `ls -i` to display inode numbers in the first column.

Inode numbers are useful for identifying hard links and for working with files that have special characters in their names. You can remove such files using their inode number with the `find` command.

Common Mistakes And Troubleshooting

New users often forget that `ls` doesn’t show hidden files by default. If you can’t find a file you know exists, try `ls -a`.

Another common issue: using `ls` in a directory with thousands of files can produce a wall of text. Use `ls | less` to scroll through the output page by page.

If you get “permission denied” errors, you might not have read access to the directory. Use `sudo ls` if you have sudo privileges, or check with your system administrator.

Using Ls In Scripts

When writing shell scripts, avoid parsing `ls` output directly. Instead, use globbing or the `find` command for reliable file handling.

For example, to loop through all text files in a script, use:

“`bash
for file in *.txt; do
echo “Processing $file”
done
“`

This is safer than parsing `ls` output, which can break with filenames containing spaces or special characters.

Listing Files With Alternative Commands

While `ls` is the standard tool, other commands can list files too:

  • `find . -maxdepth 1` — lists files in current directory
  • `echo *` — simple listing using shell expansion
  • `stat *` — shows detailed metadata for each file
  • `dir` — an older command similar to `ls`
  • `vdir` — like `ls -l` but with more details

The `find` command is especially powerful for complex searches. Use `find . -name “*.pdf”` to locate all PDF files recursively.

Customizing Ls Output With Aliases

Save time by creating aliases for your most-used `ls` commands. Add these to your `.bashrc` or `.zshrc` file:

  • `alias ll=’ls -lah’` — detailed list with hidden files
  • `alias la=’ls -A’` — all files except . and ..
  • `alias l=’ls -CF’` — columnated output with file type indicators
  • `alias lt=’ls -ltr’` — sorted by time, oldest first

After editing the file, run `source ~/.bashrc` to apply the changes immediately.

Listing Files With Color Coding

The `dircolors` command lets you customize the colors used by `ls`. Run `dircolors -p` to see the default color scheme.

You can create a custom `.dircolors` file in your home directory. For example, to make directories appear in bold blue, add:

`DIR 01;34`

Then source the file with `eval $(dircolors ~/.dircolors)`.

Using Ls With The -D Flag

The `-D` flag (not available in all versions) lists directory entries without sorting. This can be faster on very large directories.

Check your `ls` man page with `man ls` to see all available options for your specific system. Different Unix-like systems may have slight variations.

Listing Files In A Single Column

By default, `ls` arranges output in columns to fit your terminal width. Use `-1` (the number one) to force one file per line.

This is useful when piping output to other commands like `wc -l` to count files, or when you need each filename on its own line for processing.

Using Ls With The -Q Flag

The `-Q` flag encloses filenames in double quotes. This is helpful when filenames contain spaces or special characters that might be misinterpreted.

For example, `ls -Q` shows `”my file.txt”` instead of `my file.txt`. This makes it clear where filenames begin and end.

Listing Files With The -M Flag

The `-m` flag lists files separated by commas, with no column alignment. This produces a compact output suitable for pasting into other documents.

Run `ls -m` to see something like: `file1.txt, file2.txt, file3.txt`

Common Ls Pitfalls

One mistake is assuming `ls` shows all files in a directory. It doesn’t show hidden files unless you use `-a`.

Another pitfall: using `ls` with wildcards can match unexpected files if you’re not careful. Always test your pattern with `echo` first.

Also, remember that `ls` output is not guaranteed to be sorted in any particular order unless you specify a sort option. The default order varies by system.

Using Ls With The -B Flag

The `-b` flag escapes non-printable characters in filenames. This is useful when dealing with files that have unusual characters in their names.

For example, a file named `file^I.txt` (with a tab character) would appear as `file\t.txt` with `-b`.

Listing Files With The -K Flag

The `-k` flag forces file sizes to be displayed in kilobytes, even when using `-h`. This can be useful for consistency in scripts.

Use `ls -lk` to see sizes in KB regardless of the actual file size.

Using Ls With The -N Flag

The `-n` flag displays numeric user and group IDs instead of names. This is helpful when user names are not available, such as in chroot environments.

Run `ls -ln` to see UID and GID numbers instead of usernames and group names.

Listing Files With The -O Flag

The `-o` flag is like `-l` but omits the group information. This produces a shorter listing that still shows permissions, owner, size, and date.

Use `ls -o` when you don’t need group details.

Using Ls With The -G Flag

The `-g` flag is like `-l` but omits the owner information. This is the opposite of `-o`.

Run `ls -g` to see permissions, group, size, and date without the owner column.

Listing Files With The -F Flag

The `-F` flag appends a character to each filename indicating its type: `/` for directories, `*` for executables, `@` for symbolic links, `|` for FIFOs, and `=` for sockets.

This is useful for quickly identifying file types without looking at permissions.

Using Ls With The -P Flag

The `-p` flag appends a slash to directory names, similar to `-F` but without the other type indicators. This is simpler and less cluttered.

Run `ls -p` to see directories with a trailing `/`.

Listing Files With The -S Flag

The `-s` flag shows the allocated file size in blocks, not the actual file size. This can differ from the size shown by `-l` due to filesystem block sizes.

Use `ls -s` to see how much disk space each file actually uses.

Using Ls With The -V Flag

The `-v` flag sorts files by version number, which is useful for files with numbers in their names like `file1`, `file2`, `file10`.

Without `-v`, `ls` would sort `file10` before `file2` because it compares character by character. With `-v`, it recognizes numeric values and sorts correctly.

Listing Files With The -X Flag

The `-X` flag sorts files alphabetically by extension. This groups all `.txt` files together, all `.pdf` files together, and so on.

Run `ls -lX` to see files grouped by type.

Using Ls With The –Group-directories-first Flag

This flag lists directories before files, regardless of the sort order. It’s a GNU extension and may not be available on all systems.

Use `ls –group-directories-first` to see all folders listed before regular files.

Listing Files With The –Hide Flag

The `–hide` flag allows you to exclude certain patterns from the listing. For example, `ls –hide