Table of Contents
Working mechanism of wildcard characters, we call it pathname expansion. So how does the this expansion in Linux work?
Example of pathname expansion
Let’s try a simple example with the ls command, simply listing the current directory.
$ ls
36845609-dragon-picture.jpg knight.jpg
481821.jpg macOS-Sierra-Wallpaper-Macbook-Wallpaper.jpg
apple_icon.png mac.png
bear.jpg WebcamNow try expansion with the printing of files and folders with names beginning with mac.
$ echo mac*
macOS-Sierra-Wallpaper-Macbook-Wallpaper.jpg mac.pngOr we will try expansion with printing the files that have the png extension.
$ echo *png
apple_icon.png mac.pngOr we want to print out the file names with the first capital letters.
Recommended Reading: What is the expansion in Linux?
$ echo [[:upper:]]*
Webcam
Pathname expansion of hidden files
Hidden files in Linux, names start with a dot character. So what about pathname expansion with hidden files?
If you type echo * as above, expansion does not work and hidden files are not displayed on the screen.
So we will use expansion with the start character of the hidden file, this should work.
$ echo .*For example, we print hidden files in the /etc directory:
$ echo .*
. .. .java .pwd.lockDo you see the names . and .. appears at the beginning of the result, it represents the current directory and its parent directory. This results in inaccurate results.
Now try the following command to see if the result is as an example.
echo .[!.]*
.java .pwd.lockThe result is more accurate right? Explain here, the [!.] character part is also expansion and it handles the exclusion of filenames named . or ...
Also, the ls command with the -A option will give you the same result.
$ ls -AConclusion
You can understand more about wildcards in Linux, what is it and how does it work. This is not entirely useless, as it can help you faster in filtering information on the system.
(This is an article from my old blog that has been inactive for a long time, I don’t want to throw it away so I will keep it and hope it helps someone).