0%

Commands, Configuration, and Errors · practice

Preview Glob Matches Before You Act

A filename pattern saves typing, but it can also select more files than you intended. The safe habit is simple: predict the matches, preview them with a listing command, and only then use the same pattern with a command that changes state.

A pattern describes names

In Bash and zsh, the expands a glob pattern into matching paths before the program runs. PowerShell commands also accept wildcard paths, although the exact behavior belongs to the PowerShell command processing the path. Do not assume that every edge case transfers between shells.

These are the useful beginner cases:

PatternBash and zsh meaningSafe cross-platform advice
*Zero or more charactersExpect a broad match and preview it.
?Exactly one characterPreview because PowerShell wildcard behavior can depend on the command and provider.
[abc]One listed characterTreat bracket patterns as shell-specific until a preview confirms them.

The pattern data/*.txt selects text files directly inside data. It does not mean “search every subdirectory,” and it does not inspect file contents. The pattern data/Ju??.txt has two one-character positions after Ju, so it matches both data/June.txt and data/July.txt in this chapter’s workspace.

Practice inside the disposable workspace

Start from a reset Chapter 7 workspace. Your working directory is /workspace, and these relevant paths already exist:

/workspace
├── data
│   ├── January.txt
│   ├── June.txt
│   ├── July.txt
│   └── summary.csv
└── destination
    └── README.txt

First predict the result of data/Ju??.txt. Then use the route shown for your to preview, copy, and verify the same pattern.

ls data/Ju??.txt
cp data/Ju??.txt destination/
ls destination

Bash expands the pattern before cp runs. The preview and the copy use the same characters, so you can compare the intended match with the result.

Success has three observable parts:

  1. destination contains README.txt, June.txt, and July.txt.

  2. data still contains the original June.txt and July.txt files.

  3. Neither January.txt nor summary.csv was copied.

If the result differs, reset the chapter workspace and preview again. Do not repair a broad match by deleting several files. A reset is the known recovery path for this disposable exercise.

You want to copy every .txt file directly inside data to destination. Which first action best transfers the safety habit from this lesson?

You used a pattern as a question before using it as an action. That same order matters when a command names a program file: establish the working directory and target first, then run it.