The simplest way to create a file in terminal
The fastest way to create an empty file is the touch command. Type touch filename.txt and press Enter. The file appears in your current folder when ready, ready for you to open and edit in any text editor.
If you want to create a file with text already inside it, use the echo command instead: echo "Your text here" > filename.txt. This creates the file and puts your text into it in one step. The > symbol tells the terminal to send the text into a new file rather than printing it on screen.
Both methods work the same way on Mac and Linux. On Windows with PowerShell, touch works identically. If you are using the older Command Prompt instead of PowerShell, use type nul > filename.txt to create an empty file.
Key Takeaways
- The touch command creates an empty file: touch filename.txt
- The echo command creates a file with text inside: echo "text" > filename.txt
- The file appears in your current folder, which you can check by typing ls (Mac and Linux) or dir (Windows).
- You can create files with any extension — .txt, .html, .py — and the terminal does not care what you name it.
- If a file with that name already exists, echo will overwrite it, but touch will only update the date it was last changed.
Understanding file names and extensions in terminal
When you create a file in terminal, the name you type is exactly the name it gets. If you type touch myfile.txt, the file is called myfile.txt — not myfile or myfile.txt.txt. Terminal does not add anything or change what you write.
The part after the dot — the extension — tells your computer what kind of file it is. A .txt file opens in a text editor. A .html file opens in a web browser. A .py file is Python code. Terminal lets you create any extension you want, even ones that do not exist yet. The extension is just text; it does not change what the file actually contains.
Spaces in file names work, but they are annoying in terminal. If you type touch my file.txt, terminal thinks you are trying to create two files: one called my and one called file.txt. To create a file with a space in the name, put the whole name in quotes: touch "my file.txt". Most people avoid spaces and use hyphens or underscores instead: my-file.txt or my_file.txt.
Creating files in a specific folder
By default, the file you create lands in your current folder — the one shown in your terminal prompt. If you want to create a file somewhere else without changing folders first, include the path in the command.
On Mac and Linux, type touch ~/Documents/myfile.txt. The tilde ~ means your home folder, so this creates myfile.txt inside your Documents folder. On Windows PowerShell, use touch $HOME\Documents\myfile.txt instead. The $HOME variable points to your home folder the same way the tilde does on Mac and Linux.
If the folder does not exist yet, this command will fail. You have to create the folder first using mkdir foldername, then create the file inside it. For example: mkdir myfolder, then touch myfolder/myfile.txt.
Creating multiple files at once
If you need several files with similar names, you can create them all in one command. Type touch file1.txt file2.txt file3.txt and all three appear at once. This is faster than running the command three separate times.
You can also use a pattern. The command touch file{1..5}.txt creates five files: file1.txt, file2.txt, file3.txt, file4.txt, and file5.txt. The curly braces with two dots tell terminal to count from 1 to 5 and repeat the pattern for each number. This works on Mac, Linux, and PowerShell, but not on older Windows Command Prompt.
The echo command does not work the same way for multiple files. If you need to create several files with different text inside each one, you have to run echo once for each file.
Checking that your file was created
After you create a file, you can verify it exists by listing the contents of your current folder. On Mac and Linux, type ls. On Windows PowerShell, type dir. Both commands show you every file and folder in the current location, and your new file should appear in the list.
If you want more detail, use ls -l on Mac and Linux, or dir on Windows. This shows the file size, the date it was created, and other information. An empty file created with touch shows a size of 0 bytes. A file created with echo shows the number of bytes in the text you added.
If your file does not appear in the list, check that you typed the name correctly and that you are looking in the right folder. You can see your current folder by typing pwd (on Mac and Linux) or cd (on Windows PowerShell). Both commands print the full path to where you are right now.
Opening and editing your new file
Once the file exists, you can open it in a text editor from terminal. On Mac, type open myfile.txt and it opens in your default text editor. On Linux, type nano myfile.txt to edit it right in the terminal, or gedit myfile.txt to open it in a graphical editor. On Windows PowerShell, type notepad myfile.txt to open it in Notepad.
If you created the file with echo and it already has text inside, opening it shows that text. You can then add more, delete what is there, or change it however you want. When you are done editing, save the file using Ctrl+S (or Cmd+S on Mac) and close the editor.
You can also edit a file directly from terminal without opening a separate editor. On Mac and Linux, use nano myfile.txt to edit inside the terminal itself. This is useful if you are working on a remote computer or prefer not to switch windows. Press Ctrl+X to exit nano when you are finished.
Common mistakes and how to fix them
The most common mistake is forgetting the file extension. If you type touch myfile instead of touch myfile.txt, you create a file with no extension. It still works, but your computer may not know what program to open it with. You can rename it later by typing mv myfile myfile.txt, which changes the name from myfile to myfile.txt.
Another mistake is using echo when you meant to add text to an existing file. The command echo "new text" > myfile.txt erases everything in myfile.txt and replaces it with new text. If you want to add text without erasing what is already there, use two angle brackets instead: echo "new text" >> myfile.txt. The double >> means "add to the end" rather than "replace".
If you create a file in the wrong folder, you can move it using the mv command: mv myfile.txt ~/Documents/myfile.txt. This moves myfile.txt from your current folder to your Documents folder. You can also delete it with rm myfile.txt and create it again in the right place.
Frequently Asked Questions
Can I create a file with no extension?
Yes. Type touch myfile with no dot or extension at all. The file works fine, but your computer may not know which program to open it with. Most files have extensions so the system knows what to do with them.
What is the difference between touch and echo?
touch creates an empty file. echo creates a file with text already inside it. Use touch when you want to create an empty file and edit it later. Use echo when you want to put text in the file right away.
Can I create a file with a path that does not exist yet?
No. If you type touch ~/newfolder/myfile.txt and newfolder does not exist, the command fails. Create the folder first with mkdir ~/newfolder, then create the file inside it.
How do I create a file with a specific size or format?
Terminal creates files as plain text by default. If you need a specific format like PDF or Word, create the text file in terminal, then open it in the program that handles that format and save it in the format you want.
What happens if I create a file with a name that already exists?
touch does not overwrite it — it just updates the date the file was last changed. echo will erase the old file and replace it with new text. If you are not sure, check what files exist first by typing ls.