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!
- How to find path of file on Mac Terminal
- How to disable Siri on Mac Monterey
- [fix] macOS Ventura xcrun: error: invalid active developer path missing xcrun at: /Library/Developer/CommandLineTools/usr/bin/xcrun
- Select Line Number TextEdit on Mac
- Go to Specific file path using Mac Finder
- How to find Java HOME in Mac Terminal
- bash: command not found error [macOS Terminal Linux, Unix or Windows]
- Must Know Homebrew Commands for Mac/Linux Users
- How to Insert an Inverted Question Mark on Mac ¿
- Hide Keyboard Text Input Icon on macOS Ventura Menu Bar
- How to make macOS Terminal window Transparent (or Opaque)
- How to check of Updates on Microsoft Edge Browser on Mac (macOS)
- Limit scrollback rows in macOS Terminal
- How to identify installed Java (JDK) Version on macOS
- Keyboard Shortcut to delete a file on a Mac [Macbook]
- Open Docker from Terminal Command on Mac
- Installing MongoDB on Linux/Unix/macOS/Ubuntu
- How to take a screenshot on a Mac - updated for Ventura 13 [updated 2023]
- ls: .: Operation not permitted - Mac Terminal ZSH Error
- How to remove username from Mac Menu Bar?
- macOS Ventura XCode Command Line Tools Installation
- Where is .zshrc file located in macOS
- How to Show Path of Files or App in Mac Spotlight Search
- How to enable root user on Mac Terminal
- Install Java 17 (LTS) JDK on ARM based M1/M2 Mac Natively
- Okta Hacked - Source Code Stolen from GitHub Repo - News
- Trigger Notification Center Message using Mac Terminal Command - MacOS
- Convert Java Object to YAML using Jackson Library - Java
- Sort a List using Java 8 Stream Examples - Java
- How to take a screenshot on Microsoft Windows OS on PC or Laptop - Microsoft
- [Docker M1/M2 Mac] qemu-x86_64: Could not open /lib64/ld-linux-x86-64.so.2: No such file or directory AWS CLI - Docker
- Android : Duplicate registration for activity com.example.abc - Android
- Perform an Empty Commit in Git without anything in Staging Area - Git