Example about pathname expansion in Linux

by Daniel Pham
Published: Updated:

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                     Webcam

Now try expansion with the printing of files and folders with names beginning with mac.

$ echo mac*
macOS-Sierra-Wallpaper-Macbook-Wallpaper.jpg mac.png

Or we will try expansion with printing the files that have the png extension.

$ echo *png
apple_icon.png mac.png

Or we want to print out the file names with the first capital letters.

Recommended Reading: What is the expansion in Linux?

$ echo [[:upper:]]*
Webcam
example about pathname expansion in Linux
Example about pathname expansion in Linux.

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.lock

Do 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.lock

The 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 -A

Conclusion

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).

0 0 votes
Article Rating
Series Navigation«« Previous part: What is the expansion in Linux?Next part: Meaning of tilde expansion in Linux »»

You may also like

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments

DevOps Lite is a personal blog specializing in technology with main topics about DevOps, DevSecOps, SRE and System Administrator. Articles are shared for free and contributed to the community.

SUPPORT US

FOLLOW US

Subscribe my Newsletter for new blog posts. Stay updated from your inbox!

© 2021-2024 DevOpsLite.com – All rights reserved.

Please write sources “DevOpsLite.com” when using articles from this website.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More

2
0
Would love your thoughts, please comment.x
()
x

Adblock Detected

Please support us by disabling your AdBlocker extension from your browsers for our website.