Check out a free preview of the full Complete Intro to Linux and the Command-Line course:
The "Wildcards & Replacements" Lesson is part of the full, Complete Intro to Linux and the Command-Line course featured in this preview video. Here's what you'd learn in this lesson:

Brian explores wildcards, expansions, and replacement, and explains how and why they fixtures in a developer's toolkit.

Get Unlimited Access Now

Transcript from the "Wildcards & Replacements" Lesson

[00:00:00]
>> Let's go ahead and get into wildcards and replacements. So I showed you before, if we go into folder1, right? That you can touch file1, file2, file3, file4. Right? Well, let's go back in here and let's make a folder2 go into folder2. Let's do this a quicker way.

[00:00:27] So I'm gonna do touch, file, folder. Wow, file. Then I'm gonna put a curly brace and I'll do 1,2,3,4 like that .txt. And now if you look in here, I made file1, file2, file3, and file4. So what this curly brace does is it's a replacement and I'm saying, for every option in here, repeat that, right?

[00:00:53] So this actually expands out to the exact same thing. Touch file1.txt, touch file2.txt, so on and so forth. But it allows me to write that in a shorter way. Now what's nice about this is it's actually bash that does this and it's not touch. So, the final command that touch receives is touch file1.txt, touch file2 or touch file1.txt, file2.

[00:01:19] So, it's expanded out before it gets into the program, which is good, because it means the program doesn't have to support it. Bash just does that for you automatically. And this doesn't just apply to numbers, right? So I could say touch. Let's do this. mn, wa, mt, ut, like that.

[00:01:48] And you can see there, it expanded all that stuff out as well with texts there as well. So that's called expansion. There's a lot of fanciness that you can do with this, and I'm not really gonna show you much more than that. I think the last thing maybe I'll show you here is let's say I was going to do like ca and ny.

[00:02:14] Let's say I wanted to have one that was just file dash and so it actually didn't have anything, if I just put a comma there with nothing after it, that's actually a third entry in there that's empty. So one of these files would be called file-.txt, right, which is this one.

[00:02:30] And that's because they have that extra comma there at the end which can be useful sometimes. Okay. So that's expansion or at least that's basic expansion for right now. Let's put another one in here. I'm gonna put file10.txt. Let's say I wanted to find all of the files in here that didn't have the dash in here, that were just the numbers.

[00:03:04] I could say Well hold on, let's do ls first, File-. And I wanted to find everything that had the file- in it. If I put that star right there that's called a wildcard, right? And it's only gonna match things that have thing before it plus stuff after it.

[00:03:27] Right? So you can see here, it found all the file dashes but didn't find anything with the numbers in it. So that's again an expansion. And that's something that bash is doing for us. So what the ls command finally ends up seeing is file-ca, file-mn, file-nt, and so it's actually doing all that expansion for you, that matching.

[00:03:52] So it's actually looking at its own file system to figure that out for you. What happens if I do file* like that, that's gonna find, Basically everything, right? Let's make another one. So if I say touchFile.txt. So if I put file* here, it can go in the middle as well, or maybe it can't.

[00:04:24] I'm doing touch that's why. So if I do ls file*.txt, notice it's still finding relatively everything. But notice here that it is find file.txt. What if I wanted to match at least one thing right? So something has to be there, star will match nothing and it will match anything that matches that, right?

[00:04:55] So if you put question mark there, so question mark actually is going to match exactly one character. So notice that it dropped all the -mn, the -nt, the 10, right, 10 didn't show up. And also file.txt didn't show up either. So things that have in there two characters match there.

[00:05:15] So by the same token, I could do And notice that it found only ones that have States after them. Frankly, I don't use the question mark a whole lot, but I use the star one all the time. So I'm going to go ahead and create another folder here, mkdir folder3.

[00:05:31] Going into folder3, and I wanna come in here and I wanna make file 1 through 30. I can actually do touch file1..30 using curly braces here and .txt. And if you look here, it actually goes out and expands 1 through 30. And so that can be useful as well.

[00:05:53] And it's not just useful for numbers, you can actually do it for letters as well. So if I do a..z like that, it actually went through and created all. You know, file A, file B, file C, file D, so on and so forth. Let's just do it with Echo, because I'm creating a lot of files right now.

[00:06:17] But if I do a..z like that, notice that it does that. But you can also go backwards. Z..A. And you can even skip with ..2, and it will do every odd one right? Or every five. And you can do echo, 1 to 100..10. It's probably more useful if you do it from 0 or more interesting.

[00:06:52] So, there's some pretty fun stuff with that. I mean, you can go, it's actually just doing ranges of characters. So that works with capital A's, but if I do like lowercase a, those are next to each other. So let's do capital A to the lowercase c. There you go, anyway, I was escaping every time.

[00:07:20] There's a whole range of things that you wanna do, for the most part you're not gonna use ranges to do crazy things like that, but I will find myself doing it with the numbers for sure. There's also some fanciness you can do with like using them next to each other.

[00:07:39] So I can do a to z and I can also do 1 to 5. It'll actually give me every permutation of that, a1, a2, a3, a4, a5, b1, right? And you can start getting like, pretty insane with how much you want to put in there, right?. But that's also how you can do that.

[00:08:02] Again, that's not terribly useful all the time, but there's going to be like one occasion like, hey, I needed to create like so many of these files all at once. How am I going to do that? This is a useful way of doing that.
>> What if your file contains a star or a question mark?

[00:08:19]
>> I'm not sure that's a valid path. So it actually might not even be valid for Linux to do that, but normally the way you can do that is with \. So let's try, file, Yeah, no it is, right there. You can see it. So the way I did it right there, this is called an escape character, which I imagine many of you if you've been programming have seen how to do that before.

[00:08:55] The backslash means like, the next character that you're about to see I want literally this character and not something else. Like you'll see it with spaces, so I can make touch a file, and then backslash space. And now that you can see right there, this one right here.

[00:09:23] It actually literally put the space character into my file name. So yeah, use the backslash. And I imagine someone's asking themselves questions like well then how do I do backsplash, or backsplash, backslash, you just put two backslashes, so if I do touchFile and then I'd put two backslashes.

[00:09:47] You can see right there I have the backslash in it right there.