How to create a text file in Command Prompt
To create a text file in Command Prompt on Windows, you can use command-line tools such as echo, type nul > filename.txt, or notepad filename.txt. On Linux and macOS terminals, common approaches include touch filename.txt, cat > filename.txt, and redirections using > or >>. These methods let you quickly generate empty files or prepopulate them with content without opening a graphical editor.
Using echo to create and add content
Windows Command Prompt and PowerShell
The echo command can write a line of text and redirect output to a file. Use a single > to create a new file or overwrite an existing one, and >> to append without deleting previous content. Examples below show both approaches.
| Command | Effect | Platform |
|---|---|---|
| echo Hello World > filename.txt | Creates filename.txt with “Hello World” | Windows CMD, PowerShell |
| echo Hello World >> filename.txt | Appends “Hello World” to filename.txt | Windows CMD, PowerShell |
To create a file with multiple lines, chain multiple echo commands or use parentheses. In Command Prompt, remember that & separates commands on one line.
Linux and macOS terminal
On Unix-like systems, echo works similarly, but be mindful of newline interpretation across shells. Common patterns include:
echo "Hello World" > filename.txtto create or overwrite a fileecho "Hello World" >> filename.txtto append text- Using single quotes to preserve exact spacing and avoid variable expansion
Creating an empty text file
Windows methods
To make a zero-byte file quickly, you can redirect nothing to a filename. In Command Prompt, type nul > filename.txt is a common approach. In PowerShell, New-Item -ItemType File filename.txt is the idiomatic cmdlet-based method.
Unix-like methods
On Linux and macOS, touch filename.txt creates an empty file instantly and updates its access and modification timestamps. You can also use redirection with no content: > filename.txt or touch when you only need a placeholder file.
Using start or open commands to build in an editor
If you prefer a visual editor, you can launch Notepad from Command Prompt or open a file in the default GUI editor from PowerShell and terminal.
- Windows CMD:
notepad filename.txt - PowerShell:
notepad filename.txtorii filename.txt - Linux:
xdg-open filename.txtorgnome-open filename.txt - macOS:
open filename.txt
These commands create the file if it does not exist and open it for editing, which is useful when you want to enter multiple lines without scripting redirections.
Practical considerations and troubleshooting
Filenames with spaces must be quoted in most shells, and redirection operators can behave differently depending on the shell and platform. Verify file encoding if you need compatibility with specific tools; UTF-8 is widely supported, but some legacy Windows tools may expect other encodings. If a file appears locked or permissions-related errors occur, check write access to the target directory and run elevated prompts only when necessary.
Summary comparison
| Method | Platform | Use case |
|---|---|---|
| echo text > file.txt | Windows CMD, PowerShell, Linux, macOS | Create or overwrite with single-line content |
| echo text >> file.txt | Windows CMD, PowerShell, Linux, macOS | Append content to existing file |
| touch file.txt | Linux, macOS | Create empty file quickly |
| type nul > file.txt | Windows CMD | Create empty file without content |
| notepad file.txt | Windows | Open GUI editor, creates file if missing |
| xdg-open file.txt | Linux | Open file in default GUI application |
| open file.txt | macOS | Open file in default GUI application |
Security and scripting notes
When embedding credentials or sensitive data, avoid leaving files in shared or world-readable locations and prefer secured configuration management. In scripts, check for existing files before overwriting and use appropriate error handling. Command-line redirections are generally safe for local use, but validate paths and filenames to prevent unintended file creation or injection in larger automation workflows.
Frequently asked questions
- Does the file get created if the folder is read-only?
- Will using these methods affect file encoding?
No; you need write permission for the target directory. On Windows, you may need to run as an administrator or choose a different location. On Linux and macOS, you need ownership or appropriate ACLs for the directory.
Redirection typically uses the system’s default ANSI code page on older Windows CMD, UTF-8 on modern Windows PowerShell and most Unix shells. For precise control, use editors or tools that let you choose encoding.
Conclusion
Creating text files in Command Prompt and terminal environments is straightforward once you know the right redirection and command combinations. For quick content, use echo with > or >>; for empty files, prefer type nul > on Windows and touch on Unix-like systems; and when you want an interactive editor, launch Notepad, gedit, or the open command.