Wildcard Scripting Demo

The course is part of this learning path

Bash Scripting and Shell Programming
11
5
Start course
Overview
Difficulty
Intermediate
Duration
20m
Students
82
Ratings
5/5
starstarstarstarstar
Description

This course will introduce you to wildcards and show you how to use them on the command line. Since shell scripts are really nothing more than a collection of command lines, this should prove to be really helpful to you. After that, we'll get into specifically how to use wildcards and shell scripts, and you'll watch a demo to give you a practical understanding of wildcard scripting.

Learning Objectives

  • Obtain a foundational understanding of wildcards in Linux
  • Understand how to use wildcards and shell scripts

Intended Audience

This course is intended for anyone looking to learn more about bash scripting and shell programming in Linux.

Prerequisites

To get the most out of this course, you should have some basic knowledge of the command line, but it's not essential.

Transcript

Let's take a look at this relatively simple script here. It's called a copy-www.sh. And you can see that it changes directory into /var/www and then copies all of the files that end in html to /var/www-just-html. Let's go ahead and look at what's in /var/www now. And let's also see what's in this other directory. So there's nothing in there, just html directory. So when we execute this script, we'll expect everything in the /var/ww directory, except for style.css, to end up in the /var/www-just-html directory. Let's go ahead and run the script and let's see if that happens. Okay, it says it's copied about.html, contact.html, and index.html. Let's just look in this directory and we can see that indeed those files were copied over and style.css was not copied because it didn't match the wildcard expression, star.html. Let's remove the files in this directory. Make sure they're gone and let's change the script just slightly. So instead of changing the directory to /var/www, let's go ahead and and place that path in the expression itself. Okay, again, we'll just look at what's in this directory. There's nothing there at the moment. And those are the files that are in /var/www. We'll execute this script. And this time you can see that the path was included in the output here, /var/www/about.html, for example. And you can see again that those files did indeed get copied over there. I remove those files in that directory. Let's do one other thing here. Let's edit the script. And this time we're going to remove the path. And we're not going to include a CD directive here or a CD command. So this is going to copy all the html files from the present working directory or the current working directory into that /var/www-just-html directory. Okay, we can see that there are no html files in this directory, our present working directory. Let's just go ahead and create one. We'll use the touch command to create an empty file. And we'll just name this file test.html. And we'll go ahead and run this newly modified script. And you can see that it says, copying test.html. Now that's okay if you that's what you wanted. But if you really wanted the files to be copied from /var/www, what you would have to do would be to CD to that first and then run that script. So we'll go ahead and do that now. Well, before we do that, let me just show you what's in var/www-just-html at the moment. And you can see that that test.html file is there. So go ahead and run this script again from the /var/www directory. And this time we'll just give the full path to the script. And then you can see that it acts as the first couple of times where we ran the script, where it copied the contents of this directory into the /var/www-just-html directory. So this is just a minor thing to keep in mind. So be there explicitly change directory before you use a wildcard, or include the path in the wild card. If you don't do either one of these things, then the wildcard will operate on whatever present working directory you're currently in.

About the Author
Avatar
Jason Cannon
Founder, Linux Training Academy
Students
3289
Courses
60
Learning Paths
7

Jason is the founder of the Linux Training Academy as well as the author of "Linux for Beginners" and "Command Line Kung Fu." He has over 20 years of professional Linux experience, having worked for industry leaders such as Hewlett-Packard, Xerox, UPS, FireEye, and Amazon.com. Nothing gives him more satisfaction than knowing he has helped thousands of IT professionals level up their careers through his many books and courses.

Covered Topics