Darnell Davis
deesclouds

Follow

deesclouds

Follow
The Ultimate File Machine: Automating File Creation in the Terminal

Photo by dylan nolte on Unsplash

The Ultimate File Machine: Automating File Creation in the Terminal

Darnell Davis's photo
Darnell Davis
·Feb 9, 2023·

2 min read

Ever found yourself tired of creating multiple files one by one in the command line? Well, let's say goodbye to that tedious and time-consuming task!

I'll share with you a simple solution that lets us create multiple files within a single command. This technique is useful on Unix-based operating systems like macOS, Linux, or WSL(Windows Subsystem for Linux).

When Would You Use This Command?

This command is incredibly useful in many scenarios where we would need to create multiple files with similar names. For instance, if you're working on the Codewars Training JS series and want to create 42 files for your solutions, you can save time by using this command instead of repeating the process of creating new files each time.

Creating Codewars Files

Let's take a look at an example of how you can use this command. Suppose you want to create 42 Codewars solutions. You can do this by typing the following command into the terminal:

touch codewars{0001..0042}.js

The command above is creating 42 files with names like codewars0001.js, codewars0002.js, and so on. This part {0001..0042} is called a brace expansion, which lets us generate a range of values. The .js extension is because I'm using the programming language JavaScript to solve the Codewars Challenges.

If you prefer to have the numbers appear after the file extension, feel free to use the following command:

touch codewars.js{001..042}

This will create files such as codewars.js001, codewars.js002, etc. The choice between these two file naming conventions is a personal preference, and you can choose whichever works best for your workflow.

The for loop is a powerful tool that you can also use to automate the process of creating multiple files. This is the syntax:

for i in {0001..0042}; do touch codewars$i.js; done

This loop increments through the values within the brace expansion {0001 to 0042} and creates files adding the values after the $ symbol, which is called bling before the file extension. The done keyword is the signal that tells the loop to stop running.

By using these techniques, you can save time and effort when creating multiple files in the command line. Whether we are working on coding challenges or any other project requiring multiple files.

I learned about these techniques from an article written by MarkoNtech.
Give it a try, see the results for yourself, and let me know if this was helpful.

 
Share this