Introduction to Bash, VIM & Regex

Commands to Help Reduce Repetitive Typing

Introduction to Bash, VIM & Regex

Check out a free preview of the full Introduction to Bash, VIM & Regex course

The "Commands to Help Reduce Repetitive Typing" Lesson is part of the full, Introduction to Bash, VIM & Regex course featured in this preview video. Here's what you'd learn in this lesson:

After showing the wildcard character to matching files, James demonstrates how to use brace expansions, which allow for expanding patterns that would be repetitive to type out by hand.

Preview
Close

Transcript from the "Commands to Help Reduce Repetitive Typing" Lesson

[00:00:00]
>> James Halliday: The next thing that I wanna show is both wildcards and brace expansions. There's this really neat feature. First, I'll do wildcards. So you might have seen wildcards in some other context before, but they're also available in Mesh. So I could type cat beep.txt boop.txt and then moby-dick.txt.

[00:00:23]
And then it would concatenate all of those, or for something a bit shorter, I could type cat space *.txt, and what that's gonna do is it's gonna expand out to be all of those arguments, so. Goodness.
>> James Halliday: Why? It's gzipped. Hang on, I gotta gunzip that.
>> James Halliday: Here's a great example of where I can use brace expansions.

[00:01:03]
So now I have two files, moby-dick.txt which is actually gzipped. And I have the better one which is moby-dick.txt_ which I want to replace the existing file. So I can use the move command that we covered already. I could type out long form, moby-dick.txt_, just like the original file.

[00:01:26]
Then I wanna copy it over, moby-dick.txt, which is the destination. Or, a bit shorter way to write that would be to use an open curly brace, an underscore, comma, and a closing brace. So, what this does, and you can always test what these kinds of different expansions like wildcards and braces you're going to expand to by putting echo in front of whatever you're gonna do.

[00:01:56]
And so this will show you what that commands expands out to, so it's gonna be the same as this. So how it works is all of the different things that you might list separated by commas inside of braces are going to be expanded. So I'll go ahead and run that commence and you can see that now the file txt_ doesn't exist and the other one does.

[00:02:24]
Which is now uncompressed.
>> Speaker 2: You used underscore because that was the trailing character of that file, and you used just a comma with nothing because that was also the trailing character, if you will.
>> James Halliday: Yes. Yeah, correct. So, there's the common element of both of those files was moby-dick.txt.

[00:02:43]
And then, one of them had an underscore at the end, and the other one didn't have anything at the end, so that's why I used underscore and then empty string.
>> Speaker 2: Can you chain that brace then? For here's the common ground to this point, here are the variations to this point, and then continue the string with more [CROSSTALK]?

[00:03:01]
>> James Halliday: Yes, yeah you can. So I can show an example of what that would look like. So-
>> Speaker 2: Like beep and boop, for example.
>> James Halliday: Yeah, so for example, maybe I wanna cat everything that starts with the b, and is ee and oo, and then a p.txt, right?

[00:03:18]
So, if I wanna see what that expands out to, I can see beep.txt and boop.txt. Which I could also use a star for that. So, if I want anything, not just ee and oo, that begins with an b then goes p.txt. Can use the star. You can also chain the brace expansions together which does a kind of combinatoric enumeration which is sometimes useful.

[00:03:52]
Probably not very common, but it's really fun to play with. So, you could do something like. Also things that you expand with brace expansion unlike wildcards, don't have to exists. So if you just need to generate options and things for different commands, you could do that. So do something like {a,b}{c,d} will be ac ad bc bd, or you can have even more options.

[00:04:24]
Maybe x, y, z which just generates a, what is that a combination or a permutation? I don't know but it does that, so. [LAUGH]
>> Speaker 2: Something I wasn't sure about till I tried, it was the stars actually matching the files in your-
>> James Halliday: Correct.
>> Speaker 2: Folder with the other one is creating new stuff.

[00:04:45]
>> James Halliday: Yeah. So when you use the brace expansion, it just generates patterns and stars will look for files, and if the files exist then it will, or we'll print them. And if you're gonna use the star wildcard expansion, you can preface that with directories or preface it with any other part of the string that you know about.

[00:05:09]
I could do cat. cat *.txt will expand to be this, beep, boop and moby-dick. You can also test this stuff, which is the echo command, Cindy. Echo is a very simple command that prints out a string. So, it's a handy way of providing stuff for standard out, or just printing different commands.

[00:05:37]
We'll be using this a lot to play around with different commands that read from standard in later. So that's brace expansion. A practical example of when you might wanna use this stuff, or when I've used it a lot in the past, is if you need to. So you could.

[00:05:59]
Maybe you're tasked with creating a bunch of numbered directories for example. So you could type that out. You can make a script even. That lists out every single one or another thing you can do with brace expansion is you can do sequences, so if I need to generate a sequence of image directories from 0 to 100, there we go.

[00:06:26]
So img and then your number from 0 to 100, inclusive. So if I wanna mkdir, all of those image directories. Maybe I'll even mkdir a -p something like images/img, whatever. Now, in images directory, I have 101 image directories, or actually 100, because the top one is, or no, it is.

[00:06:51]
So, this 101. I guess that's how it works. You could also specify a step, which is a bit obscure, but sometimes it's useful. So if I get rid of that directory, I'll cover the rm command in a moment, but maybe I only want every ten images, or ten numbers.

[00:07:08]
I think that does it, right? Yes, so now only 0, 10, 20, 30, 40, 50.
>> Speaker 2: So the first argument is starting, second is ending, and third is-
>> James Halliday: Increment.
>> Speaker 2: Increments.
>> James Halliday: Yes. So just some fun stuff to play around with. It doesn't come up too much but it's just kinda handy.

[00:07:28]
This is kind of how a lot of this command line stuff goes. You just kinda have to learn a lot of trivia and then the more of these little tricks you know, the faster you can become and kind of like the more stuff you can do. But it's sort of just a gradual process of learning these little one-off pieces.

[00:07:49]
In the aggregate, they kind of amount to being proficient, I guess. It's kind of handy because you can get away with not knowing very much to do what you want. And then as you learn new pieces, you just get better and better. So that beats having to learn a ton of stuff up front, I think.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now