Linux Administration Bootcamp
Installing and Connecting to a Linux System
Intermediate Linux Skills
Linux Boot Process and System Logging
Disk Management in Linux
LVM - The Logical Volume Manager
User Management in Linux
Networking With Linux
Shell Scripting with Linux
Command Line Kung Fu
In this course, you will learn how to install a Linux system and connect to it, whether that be on Mac or Windows.
Let's do an ls to get our bearings in and see what files and directories we're working with. Let's use the asterisk or star and match all the files that end in .txt, ls *.txt. What just happened is the shell performed a globbing operation to expand *.txt and it passed those results to the ls command.
In this example, running ls *.txt is the same as running ls ab.txt, a.txt, b.txt and songs.txt. Of course you can clearly see it's easier to use a pattern like *.txt than it is to type out all the files by hand. I just wanted to be clear about what is happening when you use wildcards.
Now, let's look at all the files that start with the letter a. How would you do that? That's right, type in ls a*. Now, let's find all the files that start with an a and end in dot txt. We can do that by typing in ls a*.txt. Can you see a way that we can make the wildcard even simpler? How about this? Ls a*t and that gives us the same results. Let me clear the screen here and reset our bearings, look at what we're working with.
Now, let's use the question mark wildcard. Let's match all the files that are exactly one character in length. We'll do that by running ls ? If you want to display all the files that are two characters in length, then just use two question marks. Each question mark represents a single character. So we'll do ?? and we'll see that all the two character files and directories are returned. In this case they're just files.
Let's list all the files that start with an a then contain exactly one character and end in .txt. To do that, we'll run ls a followed by a question mark which represents exactly one character and then .txt. Let's check out the difference between the question mark and star. Let's run ls a*.txt The star matches zero or more characters so running ls a*.txt matches the file named a.txt.
Before we continue, I wanna point out that you can use the same flags and options as you normally do with a command while you're using wildcards. Let's get a long listing of all the files that start with the letter a. So, just like we normally would use with ls to get along listing we'll use -l and then we'll use a* to get every file and directory that begin with the letter a. Go ahead and clear the screen again here.
Here's an example of using a character class. Let's match all the files that start with the letter c then contain a vowel and end in t. To do this, we'll run ls c[ and all the characters we want to match with a, e, i, o, u and then t. And I wanna point out too that it doesn't matter what order the characters appear in in the bracket. So we can use e, i, o, a, u and we'll get the same results.
Let's do another one here, still get the same results. Let's use a range. This range will match all the files that start with an a, b, c, or d. To do that, we'll do ls [a-d] to represent a through d and then star. Here's an example of using a named character class. This command will return a list of files that end in a digit. I've simply been using the ls command to demonstrate how to match files using wildcards but let's do a little bit of actual work with wildcards. Let's clear our screen and run ls again to see what's in our directory.
Let's move all of the text files into the directory named notes. To do this, we can use the mv command, move, and then we'll use *.text to match all the text files and we'll end with the directory name, which is notes. And let's see if that worked. Okay, I'm not seeing any of the files that end in .txt, let's look in the notes directory and see if they're there. And indeed they are.
Let's move all of the mp3 files into the music directory. We can do this in a couple of different ways. One way would be to run mv*mp3 and specify the songs directory. We can also use *3 since that would match and we can even use a character class like star and then digit. I'll go ahead and just use mv *.mp3 to perform the match.
Let's look inside the music directory, and we can see that the two mp3 files that matched our search at mp3 expression were moved into the music directory. Let's use wildcards to delete some files. Here's a good pattern to follow. Test your wildcard pattern with the ls command before you use that pattern with the rm command. This way you can hopefully avoid deleting any files that you do not intend to remove.
Let's remove all the files that are two characters in length. We'll check our wildcard pattern here to make sure it returns what we want to do. And it looks like it does, so we'll go ahead and run rm using that same expression. And now the files that matched that pattern are gone.
Today, you learned how to use wildcards. You learned that the asterick matches zero or more characters. You also learned that the question mark matches exactly one of any character. You learned about character classes and how you can match any character that is included between the brackets. You were taught about ranges and how you can create your own custom ranges by starting with a character, following it by a hyphen and then ending it with another character.
Finally, you learned about the various named character classes and how they can be used to match common sets of characters such as digits, upper case letters and lower case letters.