Home > How to Tips

How to Create A File in Mac Terminal and Edit it? (Text/Python/Bin)

Updated on Tuesday, August 27, 2024

Written by

Jenny Zeng

Approved by

Jessica Shee

English Français Deutsch やまと Español Português

Summary: How do you create a file from Terminal on Mac? This guide will answer your question and tell you how to open, edit, or read a file using Terminal. If you're interested in making a file on Mac by right-clicking, download iBoysoft MagicMenu.

Many users are familiar with how to create a new file on Mac within specific applications, such as TextEdit. But fewer know how to create a file using Terminal on Mac.

Terminal is the built-in command-line interface of your operating system. It allows you to make any type of file on Mac, as long as you enter the correct command.

This guide will teach you the command to create any new file in Mac Terminal, including .txt, .bin, .py, .doc, .sh, etc. Besides, you'll also learn how to open, edit, or read a file in Terminal.

Alternative to creating a new file in Mac Terminal

If you're looking for an easy way to create new files on Mac that doesn't require launching an app and clicking multiple times, iBoysoft MagicMenu may be a better choice.

Unlike Terminal, which demands command-line knowledge, this context menu utility lets you right-click to create any new file on Mac. With it, you can make a new file on your desktop, in Finder, or in any folder you have read/write permissions.

It's packed with a template library, where common file types are pre-configured, including Text (.txt), RTF (.rtf), Word Doc (.doc), Adobe Illustrator (.ai), Word Doc-07 (.docx), WPS DPS (.dps), WPS ET (.et), Keynote (.key), Markdown (.md), Numbers (.numbers), Pages (.pages), Preference file (.plist), PowerPoint (.ppt), PPT-07 (.pptx), Adobe Photoshop (.psd), Python (.py), Ruby (.rb), Shell (.sh), WPS (.wps), Excel XLS (.xls), Excel XLS-07 (.xlsx), and Extensible Markup Language (.xml).

If the file you wish to create is not in the library, you can add it to iBoysoft MagicMenu as a template. Once it's done, you can right-click to create a new file with the template.

Here's how to create a new file on Mac by right-clicking:

Step 1: Download and install iBoysoft MagicMenu.

Step 2: Navigate to the folder where you want to make a file.

Step 3: Right-click on the empty space, move the cursor to "New File," and select the type of file you want to create, e.g., .txt, .doc, .py, etc.

Without opening any application, a new file is created in your desired folder.

Is this tool useful? Share it with others!

 

How to create a file in Terminal on Mac?

To create a file using Terminal on Mac, you can take the following steps:

  1. Open the Terminal app from your Applications > Utilities folder.
  2. Type the command below and hit Enter to move to the directory where the new file will be stored.cd folder_pathTerminal defaults to your user home directory, represented by ~. To change to a different directory, you can type cd, leave a space, then drag and drop the folder into Terminal and hit Enter.
    For example, to create a new file in the photos directory on the desktop, the command should be: cd /Users/jenny/Desktop/photos or cd ~/Desktop/photos. 
    However, If the target directory is a subdirectory of the current folder, simply using the folder name will work as well, such as cd Desktop or cd Downloads.
  3.  Execute the proper command below to create a file in macOS Terminal.
    To create an empty new file in Terminal:touch filename.extensionTo create a file in Mac Terminal with content:echo "Content to be written" > filename.extensionTo create a new file in Mac Terminal and allow you to type the content directly into Terminal: (Press Ctrl + D to save and close the file.)cat > filename.extension

To show you how to apply Step 3, we'll offer examples in the following paragraphs, helping you understand how to create a Text, Python, Bin, or hidden file on Mac Terminal.

How to create a Text file in Mac Terminal?

If you need to create an empty .txt file in Terminal:touch filename.txt

For example: touch Test.txt

If you want to create a Text file in Terminal and edit it in a text editor:nano filename.txt

For example: nano Test.txt

If you wish to create a Text file in Terminal with content:echo "Content to be written" > filename.txt

For example: echo "This is a beautiful day." > Test.txt

If you wish to make a file in Terminal and write to it in the Terminal windows: cat > filename.txt

For example: cat > Test.txt

How to create a Python file in Terminal on Mac?

To create a Python file in Terminal and name it Test, run: touch Test.py

To create a Python file with the content "I have an adorable cat.", run: echo "I have an adorable cat." > Test.txt

How to create a bin file in Terminal on Mac?

touch filename.bin

To create a bin file in Terminal and name it Test, run: touch Test.bin

Related reading: How to Open Bin files on Mac?

How to create a hidden file in Mac Terminal?

To make a hidden file in Terminal, you must first create the file and then hide it.

touch filename.extention

chflags hidden filename.extention

To create a hidden file name Test.txt, run: touch Test.txt first and then chflags hidden Test.txt

If you wish to check the hidden file later, execute:ls -lo

If you want to unhide the file, execute:chflags nohidden filename.extention

How to open or edit file from Terminal on Mac?

To open a file from Terminal on Mac, be it a hidden file or not, you need the "open" command.

open filepath

For instance: open /Users/jenny/Desktop/Test.txt (You can drag and drop the file to the Terminal window to acquire the full path of the file.)

To open a file in Terminal with a specified app:

open -a application filepath

For instance: open -a TextEdit /Users/jenny/Desktop/Test.txt

After the file is opened, you can edit it freely in the app. If you want to write to a file without launching an app, try the echo or cat command we mentioned before.

How to read file in Terminal on Mac?

A compatible app is needed to read the content of a file. If you have a Text file or a python file, you can read the file from Terminal using one of the following commands:

To read a file in the Terminal window:

cat filepath

For instance: cat /Users/jenny/Desktop/Test.txt

To read a file in a pager:

less filepath

For instance: less /Users/jenny/Desktop/Test.txt (Press Q to exit the reader.)

Hopefully, you're clear about how to create a new file in Mac Terminal after reading this article. If you find the information helpful, please share it.

 

How to manage a new file in macOS Terminal (Quick summary)?

Here are the general steps to manage a new file in Mac Terminal:

  1. Launch Terminal.
  2. Go to the folder where you need to create a file in Terminal.
    cd folder_path
  3. Run the appropriate command below to create and manage a file in Mac Terminal. 
    Make an empty file in Terminal:touch filename.extensionOpen a file in Terminal:open "filename"Write to a file in Terminal:echo "Content to be written" > filename.extensionHide a file in Terminal:chflags hidden filename.extension Delete a file in Terminal:rm filename.extension