Create a New File in Mac Terminal

In this tutorial, we’ll cover several methods to create files using basic commands like touch, echo, cat, and text editors such as nano and vim. By the end of this tutorial, you’ll know multiple ways to create files and choose the one that suits your needs.

Using the touch Command

The easiest and most commonly used method to create an empty file in the Mac Terminal is with the touch command. The touch command creates an empty file if the file doesn’t already exist, or it updates the timestamp of an existing file if it does.

To create a new file using touch, use the following syntax:

touch filename.txt
Create a New File in Mac Terminal - Using the touch Command

In this example, filename.txt is the name of the new file. You can specify any file name and extension, depending on the type of file you want to create (e.g., file.md for markdown files, script.sh for shell scripts).

You can verify the creation of the file by listing the contents of the directory:

ls
Create a New File in Mac Terminal - Using the touch Command

If the file was successfully created, you’ll see it listed among the other files in the directory.

Using the echo Command

The echo command allows you to create a file with content directly from the Terminal. This can be useful if you want to quickly add some text to a new file. The basic syntax for creating a file with echo is:

echo "Hello, World!" > newfile.txt
Create a New File in Mac Terminal - Using the echo Command

In this example, the text "Hello, World!" is written to newfile.txt. The greater-than sign (>>) redirects the output of the echo command into the specified file. If the file already exists, it will be overwritten. If it doesn’t exist, a new file will be created with the specified content.

Using the cat Command

The cat command, typically used to display the contents of a file, can also be used to create files with content. To create a new file with cat, use the following syntax:

cat > myfile.txt

Once you run this command, the Terminal will wait for your input. You can type whatever content you want in the file, and when you’re done, press Ctrl + D to save and exit. For example:

cat > myfile.txt
Sample Text
Ctrl + D
Create a New File in Mac Terminal - Using the cat Command

After pressing Ctrl + D, the file will be created with the content you entered. You can verify the contents of the file by using the cat command again:

cat myfile.txt
Create a New File in Mac Terminal - Using the cat Command

Creating Files Using the nano Text Editor

nano is a simple command-line text editor that you can use to create and edit files directly in the Terminal. To create a new file with nano, run the following command:

nano filename.txt
Create a New File in Mac Terminal - Using the nano Text Editor

This opens a blank file in the nano editor. You can type any content you want directly into the file.

Create a New File in Mac Terminal - Using the nano Text Editor

When you’re done, press Ctrl + X to exit. Nano will prompt you to save the file—press Y for yes, and then Enter to confirm the file name.

After saving, the file will be created in your current directory, and you can list the contents to verify:

ls
Create a New File in Mac Terminal - Using the nano Text Editor

Creating Files Using the vim Text Editor

For users who prefer a more advanced text editor, vim offers more powerful editing capabilities. To create a new file with vim, use the following command:

vim filename.txt
Create a New File in Mac Terminal - Using the vim Text Editor

This opens the vim editor, where you can start editing your file.

Create a New File in Mac Terminal - Using the vim Text Editor

To begin typing, press I to enter Insert Mode. Type your content, and when you’re done, press Esc to exit insert mode.

Create a New File in Mac Terminal - Using the vim Text Editor

To save the file and quit, type:

:wq
Create a New File in Mac Terminal - Using the vim Text Editor

The file will now be created in your current directory. You can verify its creation by listing the directory contents:

ls
Create a New File in Mac Terminal - Using the vim Text Editor