Create a File

Create a File

Let's help a teacher get ready for a new class. She needs an empty attendance file now and a mark sheet with one student's score in it. Both are files, but creating an empty file and writing text into a file are different jobs. Choosing the right operation matters when a report already contains useful information.

We will start with touch for an empty file and printf for text. The shell's > operator sends that text into a destination. Pay attention to that operator: it can replace existing content even though the command itself looks very small.

What you’ll learn

  • Create an empty file with touch and a text file with printf.
  • Choose between replacing and appending content without losing existing marks.

Creating a file and writing content differ

  1. Empty attendance

    touch creates a missing file without clearing an existing one.

    attendance.txt
  2. Write marks

    printf produces a line; > creates or replaces the destination.

    Diya: 72
  3. Inspect

    cat reads the saved content so you can check the result.

    marks.txt → Diya: 72
Ask whether you need an empty path, new content or an extra line.

Create attendance.txt and write the first mark

Prepare the example

Work in a fresh folder you own. The filenames below belong to this exercise. Type each line in order; the first line prepares attendance, while the second creates the separate mark sheet. If a file with either name already exists in your practice folder, inspect it before continuing.

Bash · Linux terminal

touch attendance.txt
printf 'Diya: 72\n' > marks.txt
cat marks.txt

Expected result

Diya: 72

touch attendance.txt creates the attendance file if it is missing. If it already exists, touch updates timestamps without emptying its contents. That makes it useful when you need a path to exist but must keep whatever is already there.

Next, printf produces the mark line. Its \n ends the line, and > directs the output into marks.txt. The final cat reads that file back so we can check the saved text. If you later want to add another student, think about appending with >>; repeating a write with > would replace the earlier list.

Empty files, redirected text and editor buffers

Linux gives you several ways to create a file. The best method depends on whether you want an empty file, a file with one line of text, or a file you plan to edit interactively.

Method 1: Using touch

The touch command is the simplest way to create an empty file.

touch myfile.txt

This creates an empty file called "myfile.txt" in the current directory.

If the file already exists, touch simply updates the timestamp, which is helpful for scripts.

Method 2: Using echo and Redirection

You can also create a file and put some initial content into it using the echo command with >.

echo "Hello, Linux!" > hello.txt

This creates hello.txt and adds the line: Hello, Linux!

Be careful: using > will overwrite the file if it already exists.

Method 3: Using cat to Create and Write

You can use cat to create a file and immediately start typing into it.

cat > notes.txt

You'll be in input mode now. Type some lines, then press Ctrl + D to save and exit.

Method 4: Using a Text Editor

Sometimes, you want to create and edit a file at the same time. Here are a few options:

nano notes.txt
vim notes.txt
code notes.txt  # if you're using VS Code

These commands open a text editor with the file. These editors can start a buffer for a missing file; the file is created when you save successfully.

Where Is My File Created?

If you don’t provide a path, your file is created in the current directory. You can check where you are with:

pwd

Choose the right file-creation method

  • touch file.txt — creates an empty file
  • echo "text" > file.txt — creates a file with content
  • cat > file.txt — lets you type and save manually
  • nano file.txt — creates and edits a file in one go

Try it, then check the worked answer

Your task: Create an empty file without erasing an existing mark list.

Work in a fresh folder you own. The filenames below belong to this exercise. Type each line in order; the first line prepares attendance, while the second creates the separate mark sheet. If a file with either name already exists in your practice folder, inspect it before continuing.

Bash · complete example for this question

touch attendance.txt
printf 'Diya: 72\n' > marks.txt
cat marks.txt

Expected result

Diya: 72

Worked answer

touch marks.txt

Use touch marks.txt when the existing mark list must remain intact. Removing the file and recreating it loses the data. Redirecting even empty output with > also truncates it. Here the choice is about preserving content, not merely ending up with a file of the right name.

Check your decision

Create an empty file without erasing an existing mark list.
Choose one answer. You can try again.

Answer and explanation

touch marks.txt

Use touch marks.txt when the existing mark list must remain intact. Removing the file and recreating it loses the data. Redirecting even empty output with > also truncates it. Here the choice is about preserving content, not merely ending up with a file of the right name.

Choose touch, replacement or append deliberately

Ask whether you need an empty path, new content or an extra line. touch, > and >> answer those different needs. Check the current folder before creating files, and read back a written file rather than assuming the intended text reached it.

Reaching the recap and completing the quizzes are separate milestones.

    Next lesson: Rename a File →