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.
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:
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.
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.
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.
This will create an "index.html" file in your currently active directory.
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.)
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.
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.
Envato Tuts+ tutorials are translated into other languages by our community members—you can be involved too!Translate this post