Advertisement
Tips & Shortcuts

Quick Tip: Lightning Fast File Creation with Terminal

by

Wouldn't it be great if you could quickly create any type of file in OS X and place it in any directory? With a few quick Terminal commands, you can! Read on to see what they are.


But Terminal Scares Me!

I know, Terminal is a scary place. If you don't know what you're doing, you can seriously screw with your system in a bad way. Don't worry though, we're here to help you through it.

Even if you've never opened Terminal, you'll be able to pull this one off...

Today's Quick Tip is perfect for anyone with any level of Terminal experience. Even if you've never opened Terminal, you'll be able to pull this one off, I promise.


Three Steps to File Creation Bliss

Being able to create new files on the fly via the keyboard is pretty much guaranteed to make you feel like some sort of Mac wizard.

Before you start calling yourself Gandalf though, there are three commands that you must understand. Each of these will play into our final file creation snippet.

Step 1. Change the Directory

Before we learn how to create new files, we have to learn how to make sure they go into the right spot. By default, when you open up Terminal, it should be pointing to your home folder.

Just to be sure of your surroundings, type in the "ls" and hit enter to see the contents of the currently active directory. This will spit out a list of files and folders that is a direct representation of what's in your home folder if you bring it up in Finder.

terminal
The result of the ls command is a list of the contents of the current directory.

This concept is key. Navigating directories in the Terminal is just like navigating in the Finder, you're just using text instead of a GUI.

For today's example, we'll want to use the Desktop folder. To do this, we need to "change directories" with "cd", like so:

cd Desktop/

Step 2. Create a Folder

Once you're on the Desktop, it's time to make a folder. This is accomplished via the "mkdir" command.

mkdir webproject

The command above should yield a folder on your Desktop titled "webproject". Change the name to anything you like and watch the folder pop up instantly on your Desktop.

terminal
Create a folder with mkdir

Step 3. Create Files with Touch

Creating a file with Terminal is super easy. All you have to do is type "touch" followed by the name of the file that you wish to create.

touch index.html

This will create an "index.html" file in your currently active directory.

terminal
Create a file with touch

Putting it All Together

Now that we know how all three steps work, now let's put them into practice into one long command that will do everything that we need to. Note that you can type multiple commands on a single line if you separate them with a semicolon.

cd Desktop/; mkdir webproject; cd webproject; touch index.html; touch style.css

Let's break this down to see what we've accomplished:

  • First, we used "cd" to change our directory to the Desktop.
  • Next, we used "mkdir" to create a folder for our project.
  • After creating a folder, you need to navigate into it, so we turned once again to our good friend "cd".
  • Finally, we created two files with the touch command.

Tip: You can easily create multiple files with a single touch command, just separate the file names with a space: touch index.html style.css


Cheat with TextExpander

If you're thinking that this is an awful lot to remember and type each time you want to create a file, you're right. Until you're really comfortable with the Terminal, this can be a bulky and awkward process. However, with TextExpander or any other text expansion app, you can pull it off with just a few keystrokes.

All you have to do is toss the line of code above exactly as it appears into a macro and then choose something short and sweet to expand it (I use "web#" for this particular example.)

terminal
Make Terminal commands easy with TextExpander

If you really want to get fancy, you can use fill in values to turn this into a customizable macro that can be different each time you run it. To do this, enter the following snippet into the "Content" portion of your macro.

cd Desktop/; mkdir %fill:folder%; cd %fill:folder%; touch %fill:file%;

As you can see, we used the same variable (folder) for the first two fields. This means that you only have to type this in once and it will automatically sync in both locations. If you activate this macro with TextExpander, you'll get a nice little form to fill out.

terminal
Use fill in values for extra goodness.

Go Try It!

Now that you've seen how to create files in Terminal, it's time for you to give it a shot. Try out a few examples and let us know what you think in the comments below.

Related Posts
  • Computer Skills
    App Training
    Getting Spiffy With PowerlinePowerline
    That line at the bottom of your editor and the prompt for your favorite shell have one thing in common: information! But, it's not always clear, and it's often hard to change. It's much easier now than ever before to do just that with Powerline. I'll show you how easy it is to take charge of the status line and make it into what you want it to be.Read More…
  • Computer Skills
    Alfred
    Alfred DebuggingAlfred400
    No matter how hard I try, I still have problems with scripts that I write. No matter the approach taken, mistakes in typing, logic, or both will cause programs to not behave the way expected. Debugging is the art of finding and removing these mistakes. In Alfred, debugging is even harder due to the scripts being executed in a sub-process. You need the skills this tutorial will teach.Read More…
  • Code
    WordPress
    An Introduction To Deploying WordPress with MinaImage400
    As a PHP application, WordPress is usually deployed by a very old method: uploading files via FTP. We have some deployment tools, but they often requires some type of Ruby skill. For example, one popular, powerful tool is Capistrano, but it's also very heavy with many Ruby/Rails related features. I also think that it's little bit tricky to install Capistrano for a PHP developer without any Ruby knowledge. So what options do we have as WordPress developers? In this tutorial, I will introduce you Mina: A small, light tool aims to fast deployment and server automation.Read More…
  • Computer Skills
    App Training
    Alfred Workflows for IntermediatesAlfred400
    Welcome back! Since you are reading this, you must have enjoyed the beginner's tutorial. Great! Now, let’s dig a little deeper by making a workflow that will help manage our Downloads folder, as well as a Projects Manager.Read More…
  • Code
    Tools & Tips
    Setting Up a Mac Dev Machine From Zero to Hero With DotfilesMac dev zero hero preview 400
    Setting up a new machine can often be an exciting prospect. However, as developers, there are a lot of tools we need that don't come as standard. In this post, I'd like to go through some of the techniques I use to help set up my machine quickly, efficiently and with added super powers.Read More…
  • Computer Skills
    Automation
    Write an Alfred Extension to Create Files on the FlyNewtermproject f
    Recently, we took a look at how to use Terminal to quickly create files. Today, we're going to review and apply this knowledge to make our own Alfred extension that creates a new directory of files in an instant. We'll even hit on some AppleScript along the way so you can really get your dose of geeky goodness.Read More…