> ## Documentation Index
> Fetch the complete documentation index at: https://notes.kodekloud.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Demo Specifying Files by Name

> How to select files in Unix shells using paths, dot directories, and filename globbing patterns like * ? [] POSIX classes and brace expansion including hidden file behavior

In this lesson you'll learn how to select files in a Unix-like shell using absolute and relative paths, the special directory references `.` and `..`, and filename globbing (wildcards). Filename globbing lets the shell expand simple patterns into matching filenames so you don't need to memorize exact names.

Key terms: globbing, wildcards, shell patterns, Bash, filename expansion.

<Callout icon="lightbulb" color="#1CB2FE">
  This guide covers the most common glob patterns supported by the shell: exact matches, wildcards (`*`, `?`), character sets (`[]`), POSIX character classes, and brace expansion. Remember: by default, glob patterns do not match names that begin with a dot (`.`) unless the pattern itself begins with a dot.
</Callout>

## Pattern reference

| Pattern | What it matches | Examples |
| -: | - | - |
| Exact filename | A specific single file | `ls file1` |
| `*` (asterisk) | Zero or more characters | `ls *` — all non-hidden files<br />`ls *.txt` — files ending with `.txt` |
| `?` (question mark) | Exactly one character | `ls file?` — `file1`, `file2`<br />`ls ????` — any 4-character filename |
| `[]` (character set) | Any one character from the set or range | `ls file[12]` — `file1` or `file2`<br />`ls file[1-4]` — `file1`..`file4` |
| Negated set `[!...]` | Any one character not in the set | `ls [!a]*` — names not starting with `a` |
| POSIX classes in `[]` | Character categories (digits, upper, lower, etc.) | `ls [[:digit:]]*` — names starting with a digit |
| Brace expansion | Generates multiple patterns before globbing | `ls *.{log,txt}` — expands to `ls *.log *.txt` |

## Exact match

To list a specific file by name:

```bash theme={null}
ls file1
```

## Wildcard: `*`

* `*` matches zero or more characters.
* Examples:
  * `ls *` — lists all non-hidden files in the current directory.
  * `ls *.txt` — lists files ending with `.txt`.
  * `ls log*` — lists files starting with `log`.
  * `ls *X*` — lists files containing the letter `X` anywhere in the name.

## Wildcard: `?`

* `?` matches exactly one character.
* Examples:
  * `ls file?` — matches names starting with `file` followed by one character (e.g., `file1`).
  * `ls ????` — matches any filename exactly four characters long.

## Character sets: `[]`

Square brackets match any single character listed inside (OR relationship).

* `ls file[12]` — matches `file1` or `file2`.
* `ls file[1-4]` — matches `file1`..`file4`.
* `ls [abc]*` — matches filenames beginning with `a`, `b`, or `c`.

Negation inside brackets: put `!` (or `^` in some shells) as the first character to invert the set.

* `ls [!a]*` — matches files that do not start with `a`. Note: dotfiles are excluded unless the pattern begins with a dot.
* Ranges also work for digits and letters: `[0-9]`, `[a-z]`, etc.

## POSIX character classes inside bracket expressions

These are useful for matching character categories portably across locales:

* `[[:digit:]]` — any digit (0–9).
* `[[:upper:]]` — any uppercase letter.
* `[[:lower:]]` — any lowercase letter.

Negate a class using `!` inside the outer brackets:

* `ls [![:upper:]]*` — files that do not start with an uppercase letter (dotfiles still excluded unless the pattern begins with a dot).

## Brace expansion

Brace expansion is performed by the shell before globbing. It lets you generate multiple patterns concisely:

* `ls *.{log,txt}` — expands to `ls *.log *.txt` and lists files ending with `.log` or `.txt`.

## Hidden files

Files that begin with a dot (`.`) are hidden by default.

* `ls -a` — shows all files including `.` and `..`.
* `ls -A` — shows all entries except `.` and `..`.
* `ls .*` — matches names beginning with a dot, but may expand to `.` and `..`.
  * To list only dotfiles and avoid `.` and `..`, use a safer pattern such as:
    * `ls -d .[!.]* ..?*`

### Hidden file commands quick reference

| Command | Description |
| - | - |
| `ls -a` | Show all files, including `.` and `..` |
| `ls -A` | Show all files except `.` and `..` |
| `ls -d .[!.]* ..?*` | Safer pattern to list dotfiles only (avoids `.` and `..`) |

## Example session

Below is a consolidated example that demonstrates the patterns and their outputs in a sample directory:

```bash theme={null}
student@control:~/files$ ls
1  1.txt  2.txt  a.txt  b.txt  c.txt  data.log  file1  file2  file3  file4  log  log1  notes.conf
student@control:~/files$ ls file1
file1
student@control:~/files$ ls *
1  1.txt  2.txt  a.txt  b.txt  c.txt  data.log  file1  file2  file3  file4  log  log1  notes.conf
student@control:~/files$ ls *.txt
1.txt  2.txt  a.txt  b.txt  c.txt
student@control:~/files$ ls log*
log  log1
student@control:~/files$ ls *X*
# no matches in this directory (no filenames contain an uppercase X)
student@control:~/files$ ls file?
file1  file2  file3  file4
student@control:~/files$ ls ???? 
log1
student@control:~/files$ ls file[12]
file1  file2
student@control:~/files$ ls file[1-4]
file1  file2  file3  file4
student@control:~/files$ ls file[1234]
file1  file2  file3  file4
student@control:~/files$ ls [abc]*
a.txt  b.txt  c.txt
student@control:~/files$ ls [!a]*
1  1.txt  2.txt  b.txt  c.txt  data.log  file1  file2  file3  file4  log  log1  notes.conf
student@control:~/files$ ls [!abcd]*
1  1.txt  2.txt  file1  file2  file3  file4  log  log1  notes.conf
student@control:~/files$ ls [0-9]*
1  1.txt  2.txt
student@control:~/files$ ls [a-z]*
a.txt  b.txt  c.txt  data.log  file1  file2  file3  file4  log  log1  notes.conf
student@control:~/files$ ls [[:digit:]]*
1  1.txt  2.txt
student@control:~/files$ ls [[:upper:]]*
# no matches in this directory (no filenames start with uppercase)
student@control:~/files$ ls [![:upper:]]*
1  1.txt  2.txt  a.txt  b.txt  c.txt  data.log  file1  file2  file3  file4  log  log1  notes.conf
student@control:~/files$ ls *.{log,txt}
1.txt  2.txt  a.txt  b.txt  c.txt  data.log
student@control:~/files$ ls -a
.  ..  1  1.txt  2.txt  a.txt  b.txt  c.txt  data.log  file1  file2  file3  file4  log  log1  notes.conf  .hidden
student@control:~/files$ ls .*
.  ..  .hidden
student@control:~/files$
```

<Callout icon="lightbulb" color="#1CB2FE">
  Hidden files begin with a dot (`.`). Use `ls -a` to show all files including hidden ones. If you want to list only dotfiles without seeing `.` and `..`, use `ls -d .[!.]* ..?*` or `ls -A`.
</Callout>

## Summary

* Use `*`, `?`, `[]`, POSIX character classes, and brace expansion to specify filename patterns.
* Globbing allows you to describe sets of files concisely without remembering exact filenames.
* Remember: dotfiles are matched only when the pattern begins with a `.`.

<CardGroup>
  <Card title="Watch Video" icon="video" cta="Learn more" href="https://learn.kodekloud.com/user/courses/red-hat-certified-system-administrator-rhcsa/module/bec5b574-0ba0-45f8-9ddf-03f7cc4f0dd6/lesson/031c8add-65d2-4c39-b615-c0da43eb7665" />
</CardGroup>


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.