The fastest way to create a file from the command line
You can create a file in terminal using the touch command, which makes an empty file instantly. Type touch filename.txt and press Enter, and the file appears in your current folder. If you want to add text to the file at the same time, use echo or a text editor like nano instead — both are covered below.
The method you choose depends on whether you want an empty file or one with content already inside. All three approaches work on Mac, Linux, and Windows (if you have Windows Subsystem for Linux or Git Bash installed).
Key Takeaways
- The touch command creates an empty file in one step: type touch filename.txt and press Enter.
- The echo command creates a file with text inside it: type echo "Your text here" > filename.txt and press Enter.
- The nano text editor opens a window where you can type, save, and exit without leaving the terminal.
- Make sure you know which folder you are in before creating a file, or use the full path: touch /Users/yourname/Documents/filename.txt.
Creating an empty file with touch
The touch command is the simplest way to make a file. Open terminal, type the command exactly as shown, and press Enter:
touch myfile.txt
That creates a file called myfile.txt in the folder you are currently in. The file is empty — it has no content yet. If you want to check that the file was created, type ls and press Enter to see a list of everything in that folder. Your new file should appear in the list.
You can create multiple files at once by listing them after touch, separated by spaces:
touch file1.txt file2.txt file3.txt
This creates three empty files in one command. The touch command also works with any file type — touch document.pdf or touch image.jpg — though the file will be empty and may not open in programs that expect actual content.
Creating a file with text using echo
If you want to put text inside the file when you create it, use the echo command with the > symbol (called a redirect). Type this exactly:
echo "Hello, this is my file" > myfile.txt
Press Enter, and terminal creates myfile.txt with the text "Hello, this is my file" already inside. The text goes between the quotation marks. If the file already exists, this command replaces everything in it with the new text.
To add text to a file without erasing what is already there, use two redirect symbols instead:
echo "New line of text" >> myfile.txt
The double >> appends the text to the end of the file. This is useful when you want to build up a file line by line from the command line.
Creating and editing a file with nano
The nano text editor lets you create a file and type into it without leaving terminal. Type this command:
nano myfile.txt
Press Enter, and nano opens a text window inside terminal. You can now type whatever you want. The file does not have to exist yet — nano creates it when you save. Type your content normally, as if you were in any text editor.
When you are done typing, press Ctrl + X (hold Control and press X). Nano asks if you want to save the file. Press Y for yes, then press Enter to confirm the filename. The file is saved and you return to the regular terminal prompt.
Nano is slower than touch or echo if you just need an empty file or a single line, but it is the best choice if you want to write multiple lines or edit text interactively without switching to a separate program.
Making sure the file goes in the right folder
Before you create a file, check which folder you are in. Type pwd and press Enter — this shows your current location. If you are not in the folder where you want the file, navigate there first using the cd command.
If you know the full path to where you want the file, you can create it there directly without changing folders. For example:
touch /Users/yourname/Documents/myfile.txt
Replace /Users/yourname/Documents with the actual path where you want the file. This creates myfile.txt in your Documents folder no matter which folder you are currently in. On Windows with Git Bash, paths look like C:/Users/yourname/Documents/myfile.txt.
Common mistakes and how to fix them
The most common mistake is forgetting the filename or using a space in the filename without quotes. If you type touch my file.txt, terminal creates two files: my and file.txt. To create a file with a space in the name, put the name in quotes: touch "my file.txt".
Another mistake is using the wrong redirect symbol. Remember: > replaces the file contents, and >> adds to the end. If you accidentally overwrite a file, you cannot undo it from terminal, so be careful when using echo with the single > symbol on a file that already exists.
If you get a "permission denied" error, you do not have write permission in that folder. Try creating the file in your home folder or Documents folder instead, or ask your system administrator for permission to write to that location.
Frequently Asked Questions
Can I create a file with no extension?
Yes. Type touch myfile without the .txt or other extension, and terminal creates a file with no extension. This works fine, though programs may not know what type of file it is. Most text files should have .txt, and scripts should have .sh or .py depending on the language.
What if I want to create a file in a folder that does not exist yet?
Terminal will not create the folder for you. You have to make the folder first using mkdir foldername, then create the file inside it. For example: mkdir newfolder, then touch newfolder/myfile.txt.
How do I create a file with multiple lines using echo?
Use the double redirect >> to add lines one at a time, or use a special character. Type echo -e "Line 1\nLine 2\nLine 3" > myfile.txt to create a file with three lines. The \n means "new line". Nano is easier if you have many lines to type.
Does touch work on Windows?
Touch works on Windows only if you have Git Bash or Windows Subsystem for Linux installed. If you are using the regular Windows Command Prompt, use type nul > filename.txt instead to create an empty file.