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
These are the useful beginner cases:
| Pattern | Bash and zsh meaning | Safe cross-platform advice |
|---|---|---|
* | Zero or more characters | Expect a broad match and preview it. |
? | Exactly one character | Preview because PowerShell wildcard behavior can depend on the command and provider. |
[abc] | One listed character | Treat 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
Get-ChildItem -Path ./data/Ju??.txt
Copy-Item -Path ./data/Ju??.txt -Destination ./destination
Get-ChildItem -LiteralPath ./destination
PowerShell passes wildcard paths to the cmdlet. The important evidence is still the preview: continue only when it names June.txt and July.txt, and neither of the other two files.
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.
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:
destinationcontainsREADME.txt,June.txt, andJuly.txt.datastill contains the originalJune.txtandJuly.txtfiles.Neither
January.txtnorsummary.csvwas 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.