The course is part of this learning path
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.
- Obtain a foundational understanding of wildcards in Linux.
- Understand how to use wildcards and shell scripts.
This Course is intended for anyone looking to learn more about bash scripting and shell programming in Linux.
To get the most out of this Course, you should have some basic knowledge of the command line, but it's not essential.
In this video, you'll learn how to use wildcards in your shell scripts. You'll run into situations where you'll want to work on a group of files or directories. To do that in a script, you can use wildcards. This is very similar to the process of using wildcards at the command line. You can use wildcards just as you did on the command line. This script, for example, changes to the /var/www directory, and copies all of the HTML files into /var/www-just-html.
Let's say you want to do something for each file that matches a wildcard. In that case, you can use a for loop. Let's use a for loop to perform an action on all the HTML files in a directory. This script simply echoes to the screen the name of the file that is about to be copied, and then copies that file from the /var/www directory, into the /var/www-just-html directory. Of course, you could do something more complex in this loop if you wanted to or needed to. Notice that the wildcard expression is being used where you would normally provide a list. This wildcard expression will expand to create a list of matching files and directories.
In the case that the wildcard is not matched, the for loop is just simply not executed. Also notice that we explicitly changed into a directory before we used the wildcard. The output of this script might look something like this, Copying about.html, Copying contact.html, and finally, Copying index.html. Let's modify the script just slightly. Instead of cding into the /var/www directory, let's include that path in the wildcard expression. You can see the wildcard expression is /var/www/*.html. The output of this script will look something like this. Copying /var/www/about.html, and then the same thing for /var/www/contact.html, and /var/www/index.html. Notice how the path is included in the output. This is because we included the path in the wildcard expression.
Here's an example of how not to use a wildcard. In this script, all the files in the current working directory that match start at HTML, will be copied into /var/www-just-html. The current working directory is the directory from which you started this script. If you wanted to get the same results as the previous scripts, you would have to cd into /var/www before you executed the script. Instead of having this dependency live outside of your shell script, you should instead explicitly cd into the desired directory with a cd command within your shell script, or include the path in the wildcard expression.
In this lesson, you learned how to use wildcards in your shell scripts, just like you can on the command line. We also looked at how you can use wildcards in conjunction with loops. Finally, you learned that if you did not supply a directory in the wildcard expression, or cd into a directory before the wildcard expression is used, the wildcard expression will operate on the contents of your current directory.
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.