When working in Mac’s Terminal application, you will often find the need to create a file. No matter the reason, simply creating a file can be done in a snap using the touch command or a text editor such as nano.
If you have some specific text you wish to start the file with, you can also just direct the output of an echo command to the file in Mac Terminal. There are numerous ways to create a file in Terminal Mac, which are very straightforward.
My name is Eric, and I have been using command line interfaces such as Mac Terminal for many years. Creating files is a common everyday task, but it is also an important one that you need to know how to do when working with Terminal. I can show you a few easy ways to do this.
Keep reading below, and we’ll look at common ways to create files. I will also show you a simple way to verify the file was created and view the contents of the file.
Contents
Use the Touch command
If your initial goal is to just create an empty file, the quickest way to do this is with the touch command. While the touch command has some optional parameters, you won’t need any of them to create an empty file. All you need to do is run the command, as shown below.
touch newfilename
For example, let’s create a new empty file called mytest.txt.
touch mytest.txt
If you look at the properties of the file, you will notice that the size is 0 bytes, indicating that it is an empty file.
Direct Output to a File
If you want to create a file with some initial text in it quickly, you can direct the output of an echo command into a file. To do this, you just use the echo command along with the initial text you wish to have in the file and then use the > to direct the output to the new file.
The command to do this will have a format as shown below.
echo “Initial Text” > newfile
For example, I will create a new file called mytest2.txt with the text “Hello World” in it.
echo “Hello World” > mytest2.txt
As you can see, the file mytest2.txt was created, and its size is greater than 0 bytes which means that it is not empty. By displaying the file’s contents, you can see the text “Hello World” was entered into the file when it was created.
Use an Editor
One more method you can use to create a file in Mac Terminal is to use an editor. You can run the command to start the editor along with the new file name to create the new file. You can use a few different editors, such as vi, vim, and nano.
We will use nano for our example below. When you run the nano application and specify the name of a file that does not yet exist, nano (and other editors as well) will create the file automatically as long as you save it when you exit the editor. Let’s take a look.
The command should have the following format.
nano newfilename
For our example, we will create a file called mytest3.txt.
nano myfile3.txt
When you hit the return key, the nano file editor will start with the specified file. In our case, it is myfile3.txt. Once the editor has started, you can enter text or other data if you wish, or just put a space or blank line if you do not have anything to enter. Nano will not create and save the file if it is completely empty.
To create and keep the file, you must exit the nano editor and save the file as your exit.
Hit CONTROL-X to exit the editor.
Select Y to save the changes, and your file will be created. You should be able to see it in your current directory.
How to Verify and View the Created File
The first way to verify that a file has been created is to look for it in the directory where you created it. Most of the time, you will create it in your current directory, which will be our assumption here. If you created it in another directory, you will need to navigate to that directory.
Just use the ls command to look for the file in your directory. To see the details of the file, you can add the -la parameters, which will show hidden files and the details of those files.
ls -la
Hit return, and you should see your newly created file.
If you want to see the contents of the newly created file, you could use an editor such as nano, but the quickest and easiest way is just to use the cat command followed by the filename.
cat newfilename
Or from one of our examples above, it would be as shown below.
cat mytest2.txt
Once you hit return, you will see the file’s contents displayed on the screen.
FAQ
Below are a few questions about creating a file in Terminal Mac.
What happens if I use the touch command on an already existing file?
If you use the touch command with a file that already exists, it will not remove, delete or modify the existing file. The only thing it will do is update the file’s modified date to the current date and time. In essence, it is touching the file so that the current date and time are updated.
Can I direct output from commands other than echo?
Most other commands you run from the command line can have their output redirected to a file using the > symbol. Keep in mind that when doing this, you will not see the output from the command on the screen; it will only go to the file.
Can I do a Save As in the nano editor?
Yes, if you hit CONTROL+O, you can specify a new file name to save the file as. This can also be done when using the CONTROL+X command and then answering yes to save the file. Before it saves, it allows you to change the name of the file you are saving to.
Conclusion
There are many ways to create a file in Terminal Mac, and I have shown you a few of the easiest methods above. You can use the touch command, direct the output of a command to a file, or use an editor. Once created, you can verify the file is there with the ls command, and you can view the contents using the cat command.
I hope the above information can help you create files in Terminal Mac. As usual, let me know if you have questions or comments. I would love to hear from you.