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
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.
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
- How to show line numbers in Nano on Mac
- How to install Jupyter Notebook on macOS Sonoma
- How to Disable Remote Management using Mac Terminal
- Test internet speed using macOS Terminal command
- Docker Desktop needs privileged access macOS
- Google Search Hot Trends Screensaver for Mac OS X
- How to do calculations in Mac Terminal
- How to make TextEdit the default text Editor on Mac
- How to Show Full Website Address on Safari for Mac (Ventura/Sonoma)
- The Zsh Shell - Mac Tutorial
- Opening mac Terminal
- How to change the name of your Mac running on macOS Ventura 13
- How to find the Battery Cycle Count on macOS Ventura
- How to set an emoji as Zsh terminal prompt in macOS
- How to access Trash Folder using Mac Terminal Command
- macOS Ventura XCode Command Line Tools Installation
- Hide Bluetooth icon on Mac Menu Bar macOS Ventura 13
- How to run .sh file in Mac Terminal
- Terminal Command to get the list of available shells in macOS
- How to see Storage Information on macOS Ventura
- How to Go To /usr/local/bin on Mac Terminal?
- How to do screen recording on Mac
- How to Find File and Directory Size in Mac Terminal
- Open .bash_profile File in TextEdit using Terminal
- Remove Now Playing icon from macOS Big Sur Menu Bar
- Changing Android Intent Tittle using java code - Android
- Angular CLI ng Error: You need to specify a command before moving on. Use --help to view the available commands - HowTos
- Ubuntu Linux: Unzip a zip file using Terminal - Ubuntu
- Android Studio : Build Tools 23.0.0 rc1 is deprecated - Android-Studio
- Fahrenheit to Celsius Temperature Convertor: Tool & Formula - Tools
- 33: Python Program to send an email vid GMail - Python
- How to recover SharePoint FARM if the SQL Server IP changes - SharePoint
- [Fix] Ubuntu E: Unable to locate package sudo error - Ubuntu