Example about pathname expansion in Linux

Share this post to your friends !

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

linuxmint Pictures # 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.

linuxmint Pictures # echo mac*
macOS-Sierra-Wallpaper-Macbook-Wallpaper.jpg mac.png

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

linuxmint Pictures # echo *png
apple_icon.png mac.png

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

linuxmint Pictures # echo [[:upper:]]*
example-about-pathname-expansion-in-linux 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:

linuxmint etc # 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.

linuxmint etc # 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


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.

Continue reading the series«« Previous part: What is the expansion in Linux?Next part: Meaning of tilde expansion in Linux »»
If you liked this article, then please subscribe to our YouTube Channel for more video tutorials. You can also find us on Twitter and Facebook.

Share this post to your friends !
If you appreciate what we share in this blog, you can support us by:
  1. Stay connected to: Facebook | Twitter | Google Plus | YouTube
  2. Subscribe email to recieve new posts from us: Sign up now.
  3. Start your own blog with SSD VPS - Free Let's Encrypt SSL ($2.5/month).
  4. Become a Supporter - Make a contribution via PayPal.
  5. Support us by purchasing Ribbon Lite Child theme being using on this website.

We are thankful for your support.

single post bottom banner
«« »»

Got something to say? Join the discussion

Please keep in mind that all comments are subject to our Comment Policy. Your email address will not be published.
This site uses Akismet to reduce spam. Learn how your comment data is processed.