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.
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.
Have Questions? Post them here!
- Add Line Break in Microsoft Excel Cell on Mac (macOS)
- How to view the desktop when using macOS Stage Manager?
- Mac - How to Install VirtualBox
- Must Know Homebrew Commands for Mac/Linux Users
- Show Desktop Keyboard Shortcut on Mac
- Check Reboot History Mac
- How to set Out Of Office (OOO) on Outlook for Mac
- How to Screenshot on Mac Ventura 13 - Step by Step Guide
- How to install maven in macOS using Terminal Command
- How to change macOS Safari default language
- How to hide or display Wifi icon in macOS Bug Sur Menu Bar
- Permanently Set or Change $JAVA_HOME on Mac (macOS)
- Two Ways to Extract rar (*.rar) files on Mac
- How to change Ping TTL value on macOS
- How to upgrade Docker Desktop on Mac
- [fix] macOS Ventura xcrun: error: invalid active developer path missing xcrun at: /Library/Developer/CommandLineTools/usr/bin/xcrun
- Installing brew on M1/M2 ARM Mac
- How to increase macOS Terminal text font size (Big Sur)
- How to open new tab in Sublime Text Editor [macOS]
- macOS: Change Weather App Temperature unit from Fahrenheit to Degree
- Clear SFTP Prompt Screen on macOS Terminal
- How to install homebrew (brew) on M1 Mac
- [Fix] brew: command not found Mac or Linux Terminal Error
- How to download and install macOS 13 Ventura
- How to install Zoom Add-in to Outlook (Mac)
- Eclipse like Auto Import Shortcut in Intelij IDE Android Studio - Android-Studio
- Resolve - zsh: command not found: code - zsh
- Setting Java_Home Environment variable on Windows Operating System - Java
- Access URL for SharePoint Tenant Admin Center (Online Office 365) - SharePoint
- [Android Studio] Button on click example - Android-Studio
- How to resolve Certificate Expired WhatsApp Error - WhatsApp
- How to see HTTP Request Response Headers in Google Chrome Browser - Chrome
- Java: Create Temporary Directory and File and Delete when application terminates - Java