Shell wildcards
The patterns *, ?, [abc], [a-z], [!abc] and braces, the hidden files that * skips, the fact that the shell expands patterns before running the command, and the role of quotes.
- 23 cards
- 5 topics
- Free
The outline, topic by topic
Each topic is weighted the way the exam weighs it.
- Concepts
- Questions
- Sort and link
- Scenarios
- Understand
A few cards from the deck
Wildcard
*: what is it?Show the answer
A pattern replaced by all the existing file names that match, with any run of characters (even empty) in its place.
*.txtmatchesa.txtandfinal report.txt, but not.cache.txt: a name that starts with a dot must be written with its dot (.*).A directory contains
photo1.jpg,photo2.jpg,photo9.jpg,photo10.jpgandphoto1.png. What doesphoto[1-9].jpgexpand to?photo1.jpg photo2.jpg photo9.jpgphoto1.jpg photo2.jpg photo9.jpg photo10.jpgphoto1.jpg photo2.jpg photo9.jpg photo1.pngphoto1.jpg photo10.jpg
Show the answer
photo1.jpg photo2.jpg photo9.jpgThe class
[1-9]matches a single character:photo10.jpghas two betweenphotoand.jpg, andphoto1.pnghas the wrong extension.Wildcards are expanded by the …, before the command is run:
ls *.txtalready receives the ….Show the answer
Wildcards are expanded by the shell, before the command is run:
ls *.txtalready receives the list of names.echo *.txtshows what any command would see. The command cannot tell whether the user typed a wildcard or the names.Wildcard
?: what is it?Show the answer
A pattern that matches exactly one character, whatever it is.
photo?.jpgmatchesphoto1.jpgandphotoA.jpg, but notphoto10.jpg(two characters) norphoto.jpg(none).In a directory with no .zip file,
rm *.zipanswers “rm: cannot remove '*.zip': No such file or directory”. What happened?- No name matched: bash left
*.zipas is andrmlooked for that literal name - The shell is not allowed to expand wildcards in this directory
- The .zip files are hidden, so
*never matches them rmdoes not understand wildcards: you needfind -nameto delete
Show the answer
No name matched: bash left
*.zipas is andrmlooked for that literal nameA pattern that matches nothing stays as is (bash's default behaviour). So
rmsees the name*.zipand complains it does not exist: a file name can legitimately (but very rarely) contain a star.- No name matched: bash left