There are multiple ways in which you can create a file using the Mac Terminal, let us take a look at a few ways to do so,
1. Using touch command
If you want to create an empty file, simply use the command touch followed by the name of the file,
Example:% touch myfile.txt
% ls -l
total 0
-rw-r--r-- 1 code2care staff 0 Sep 22 12:45 myfile.txt
As you can see the file is created in the location where you run the command.
2. Redirect Echo Content of Standard Output to a new file
You make do an echo followed by a string that you want to write to a file.
Example:% echo "Write this to a new file" > myfile1.txt
% ls -l
total 8
-rw-r--r-- 1 code2care staff 0 Sep 22 12:45 myfile.txt
-rw-r--r-- 1 code2care staff 19 Sep 22 12:51 myfile1.txt
% cat myfil1.txt
Write this to file
You can also create a file using > filename and then type the text you want to write to the file, or simply press Control + D if you just want to create an empty file.
3. Using cat command
You can create a file and write to the file using the cat command,
Example:% cat > myfile3.txt
Write your text,
once you are done. Press Control + D to save and quit.
4. Using Nano Text Editor
This is one of the very effective ways of creating and writing a file on the Mac Terminal, Nano is the most user-friendly text editor for the console.
To create a new file, simply type nano followed by the file name,
Example:% nano myfile4.txt
![Nano Text Editor to create and write files using Mac terminal Nano Text Editor to create and write files using Mac terminal](https://code2care.org/howto/mac-create-file-terminal/images/Nano Text Editor to create and wirte file using Mac terminal.jpg)
Once you type the command, the file will be opened in the Terminal as an interactive UI, you can write the content of the file and press Control + X followed by a Y to save the file.
5. Using Vi (Vim) Text Editor
By far this is the most loved Text Editor available for the Terminal by computer programmers and System Admins, it works similar to Nano but is far more advanced when it comes to dealing with files.
Example:% vi myfile5.txt
This will open up a file editor, you can write content to the newly created file and press Escape followed by :wq! to save the file.
![Creating a new file and writing using Vi Editor Mac Terminal Creating a new file and writing using Vi Editor Mac Terminal](https://code2care.org/howto/mac-create-file-terminal/images/Creating a new file and writing using Vi Editor Mac Terminal.jpg)
Conclusion
As we saw there are multiple ways in which we can create a new file, the quickest one to create a blank file is using the touch command and if you want to create and write to the file it's better to go with Nano or Vi Editors.
Facing issues? Have Questions? Post them here! I am happy to answer!
Rakesh (He/Him) has over 14+ years of experience in Web and Application development. He is the author of insightful How-To articles for Code2care.
Follow him on: X
You can also reach out to him via e-mail: rakesh@code2care.org
- Fix: grunt: command not found (Mac/Windows)
- AppleScript Example with TextEdit
- How to copy Password of Wifi on your Mac
- Install OpenJDK Java (8/11/17) versions using brew on Mac (Intel/M1/M2)
- How to check if a port is in use using terminal [Linux or macOS]
- Cut, Copy and Paste Keyboard Shortcuts on Mac Keyboard
- [macOS] NetBeans IDE cannot be installed. Java (JRE) found on your computer but JDK XX or newer is required.
- How to Remove Widgets from macOS Sonoma 14 Desktop
- Turn off Auto-Capitalization on macOS Ventura 13
- How to Go To /usr/local/bin on Mac Terminal?
- How to Open TextEdit on Mac
- Mac: How to Open Android Studio from Terminal
- How to change the default location of Mac Screenshot
- Display Seconds in Digital Clock on Mac Menu Bar [macOS]
- How to install homebrew (brew) on M1 Mac
- Must Know Homebrew Commands for Mac/Linux Users
- Steps to Uninstall Roblox on Mac (macOS)
- How to change the System Settings Sidebar icon size Mac Ventura 13
- How to set Out Of Office (OOO) on Outlook for Mac [Updated 2023]
- Trigger Notification Center Message using Mac Terminal Command
- [fix] macOS Ventura - Python3 xcrun: error: invalid active developer path missing xcrun at CommandLineTools
- How to Install Rust Language on Mac (macOS)
- How to minimize all Windows in Mac
- Display ls command file sizes in KB (kilobytes) MB (megabytes) or GB (gigabytes) [Linux/macOS]
- How to make TextEdit the default text Editor on Mac
- How to integrate Salesforce CRM Sales and Service with Microsoft Teams - Teams
- Install Docker Desktop on M1/M2 Apple Silicon ARM Chip Mac - Docker
- Format Python Code in Visual Studio Code (VS Code) - Python
- Iterate over an Array using Java 8 Stream and foreach - Java
- SharePoint - Use Today's Date Time in list view filter and calculated column - SharePoint
- Reset Font: Family, Style and Size of Notepad on Windows 11 to Default - Windows-11
- How to flatten a nested list in Python - Python
- How to create Custom RatingBar Android Programming Tutorial - Android