🎁

12 Days of Baddass Courses sale! Get instant access to the entire egghead library of courses and lessons for 58% off.

Runs out in:
15 : 06 : 02 : 41
Join egghead, unlock knowledge.

Want more egghead? It's 58% off for a limited time only!

This lesson is for members. Join us? Get access to all 3,000+ tutorials + a community with expert developers around the world.

Unlock All Content for 40% Off
1×
Become a member
to unlock all features
Autoplay

    Create and Copy Multiple Files with Brace Expansions in Bash

    Cameron NokesCameron Nokes
    bashBash

    Brace expansions are commonly used with the cp or mv commands in order to dynamically generate strings. In this lesson, we'll use a brace expansion to shorten a command like cp index.js index.js.backup to cp index.js{,.backup}. We'll also see how brace expansions can generate sequences. For example, touch test-{1..10} will generate the arguments to touch to create 10 test files.

    Note that the tree command that I use in this lesson has to be installed.

    Code

    Code

    Become a Member to view code

    You must be a Member to view code

    Access all courses and lessons, track your progress, gain confidence and expertise.

    Become a Member
    and unlock code for this lesson
    Discuss

    Discuss

    Transcript

    Transcript

    Instructor: Let's say I want to create a backup of a file. I want the backup to retain the same name as the file but just append .backup on the end. I'll just go ahead and create a dummy file here. If I wanted to create a backup of the file, I would do copy index.js.

    Instead of passing the file name there again, I can do braces -- that's why it's called a brace expansion as it uses braces -- and do comma and then .backup. Let's see how that works. Cool. That works just like we wanted.

    Another thing I can do with brace expansions is create several folders with the same structure. If I wanted to do a monorepo style setup, I could do packages and then I'll have my different package names here.

    Then I want each one to have a source folder in it. Let's do that and then see how that came out. Yeah, that's the folder structure I wanted.

    Let's dive into a little bit how it works a bit more, so let's do a basic example. Let's do echo and do pre. I'm going to do A, B, C there and braces and then post. Brace expansions follow this kind of format. There's what's called a preamble which is the string that occurs before our braces, and then there's a postscript which occurs after it.

    This is a list of strings. We can see when we run it, it basically iterates through this list and then prepends or appends the preamble and the postscript as needed.

    Know that this also supports empty items in the list. If we go back to our copy example, we'll just use echo instead. We had index.js, and then we had an empty element and m.backup.

    If we echo that out, we can see that that's exactly the arguments that we need to pass to the copy command. Basically, when we run this with copy, the brace expansion gets interpreted, and these arguments gets passed to copy and then copied exactly as we need to, which just saves a lot of typing along the way.

    Another thing brace expansions can do is rearranges. For example, if I do echo, brace, two dots, and then 10, it's going to interpret this as an integer sequence, and it fills in the missing numbers from that range.

    The same applies to letters. This can be useful for generating a lot of test files. For example, if I do touch and I do tests through 10, let's list that out. What you see here is all my test files that I created.

    Note that this works because the touch command accepts multiple inputs. Not all commands do that, so you have to check the commands documentation before you can be sure something like this will work.