Skip to main content

How to create a text file in Linux

Just need a .txt file? Create one online in seconds →

Open a terminal and run touch notes.txt to create an empty file in the current directory. To start with some content instead, use printf '%s\n' 'Hello' > notes.txt. You can also write in nano, vim, or a desktop text editor. The right choice depends on whether you need a blank placeholder, a short command output, or an editable document.

Create an empty file with touch

1. Choose a writable folder

Start in your home directory, which normally belongs to your account. Run these commands separately:

cd ~
pwd

pwd prints the current directory. A filename without a directory, such as notes.txt, refers to this location. If you need a project subfolder, create it with mkdir -p text-practice and enter it with cd text-practice. These examples use ordinary user permissions.

2. Create the file

touch notes.txt

If the path does not exist, touch creates a zero-byte file. If it already exists, the command updates timestamps without erasing its contents. It does not create missing parent directories. Quote names containing spaces: touch "meeting notes.txt" creates one file with that full name.

3. Check the result

ls -l notes.txt
wc -c < notes.txt

The first command shows the file, owner, permissions, and size. The second reports bytes; a newly created empty file returns zero. An empty file has no characters from which to identify an encoding. Its .txt extension signals intended use but does not add a header or change the bytes.

Write a line with echo or printf

For a simple line, run:

echo 'Shopping list' > shopping.txt

The shell redirects the command's standard output into the file. echo normally adds a newline. For predictable formatting, especially strings containing backslashes or beginning with a dash, use printf:

printf '%s\n' 'Shopping list' 'Apples' 'Rice' > shopping.txt

Each string becomes a separate line. Keep the format string fixed as '%s\n' and pass the content as arguments, so percent signs in your text are treated as data. Single quotes preserve literal characters such as $; to include an apostrophe, choose appropriate shell quoting or use an editor.

Replace with >, append with >>

> creates the destination or truncates an existing file before the command writes. >> creates the destination if necessary and otherwise adds output at its end:

printf '%s\n' 'Tea' >> shopping.txt
cat shopping.txt

Check the filename before using >. Appending does not automatically insert a separator before the new content: if the existing file lacks a final newline, the next text starts on that same line. The examples above deliberately finish every record with a newline. Keep a copy before replacing important content.

Enter several lines with cat or a heredoc

For interactive input, run cat > draft.txt, type a few lines, and press Enter after the last line. At the start of the next empty line, press Ctrl + D to signal end of input. cat then exits and the shell prompt returns. This method is useful for a quick paste, but a full editor is easier when you need to revise earlier lines.

A here document, often called a heredoc, gives the input a visible ending:

cat > draft.txt <<'EOF_TEXT'
Meeting agenda
1. Review progress
2. Agree on next steps
Budget: $100
EOF_TEXT

Type the closing EOF_TEXT on its own line without extra spaces. Quoting the opening delimiter prevents the shell from expanding variables and command substitutions in the body. The dollar sign in this example stays literal. Use >> instead of > to append a block. If the prompt keeps waiting, check that the closing delimiter matches exactly. GNU Bash's redirection reference

Edit a document with nano

Run nano notes.txt. If nano is installed, it opens a terminal editor; an absent filename is created when you save. Type your content, then press Ctrl + O to write the file. Confirm the name with Enter and press Ctrl + X to exit. The ^ notation in nano's shortcut bar means Ctrl, so ^O corresponds to Ctrl + O.

Reopen the file with the same command to edit it again. If nano reports an error saving, check the destination path and permissions before closing the buffer. A message that a command was not found means the editor is unavailable; use your distribution's package manager or another installed editor. You do not need to install every editor in this guide.

Edit with vim

Run vim notes.txt, press i to enter Insert mode, and type. Press Esc to return to Normal mode, then type :wq and press Enter to save and quit. To leave without saving your changes, use Esc followed by :q! and Enter. The exclamation mark discards unsaved edits, so use it deliberately.

Vim separates typing from commands. If letters seem to trigger actions instead of appearing, press i. If a save command appears inside the text, press Esc before trying it again. A small installation may provide vi rather than vim; check which editor is available. Vim's first editing steps

Use GNOME Text Editor or gedit

On a graphical desktop, open Text Editor from the application launcher. GNOME Text Editor and gedit are separate applications; your distribution may include either, or an alternative such as Kate or Mousepad. Start a new document, type your content, press Ctrl + S, and choose a writable folder and a name ending in .txt.

If available, gnome-text-editor notes.txt or gedit notes.txt opens that path from Terminal. These commands require a graphical session and the corresponding application. On a remote server, use a terminal editor instead. After saving, reopen the file from your file manager to confirm its location and contents. GNOME Text Editor help

File permissions, UTF-8, and LF endings

A permission error can mean the directory is not writable, or an existing file belongs to another account. Inspect it with ls -ld . and ls -l notes.txt. Prefer a folder you own rather than adding sudo to ordinary note-taking commands. For a private file that you own, chmod 600 notes.txt permits reading and writing by the owner only. Access can still depend on directory permissions and other system controls.

For new documents, UTF-8 supports accented letters, Asian scripts, and emoji. Terminal output depends on the program and locale; locale charmap reports the character map for your current locale. Choose UTF-8 explicitly in a graphical editor if it offers an encoding selector. A .txt name alone cannot guarantee the encoding.

Linux tools conventionally use LF line endings. Windows-originated input may contain CRLF, which some parsers treat differently. See CRLF versus LF and what a TXT file stores. file notes.txt can provide a useful hint about content and line endings, but encoding detection is not proof; reopen with the intended encoding and check representative characters.

The same commands on Chromebook

On a supported Chromebook, enable the Linux development environment in Settings and open its Terminal. The command examples above also work there. Save in your Linux home directory first, then find the result under Linux files in the ChromeOS Files app. Other folders must be shared with Linux before it can access them. Work or school administrators may restrict this feature; see Google's setup instructions.

FAQ

Does touch overwrite an existing document?

No. It updates timestamps and preserves existing content. A shell redirection with > can overwrite content, even when used with a command that would otherwise be harmless.

Why can I not find the file after saving?

Run pwd to check where relative paths resolve, then ls -l with the filename. Linux names are case-sensitive on common filesystems: Notes.txt and notes.txt can be different files.

Must a Linux text file end in .txt?

No. Linux tools can read plain text with other extensions or none. Use .txt when sharing a general-purpose document so a recipient can recognize it easily.

Sources

Create a text file online

Characters
0
Words
0
Lines
0
Bytes
0

Your text stays in your browser. Nothing is uploaded.