🎓 A1Learner
🔍
0 of 50 lessons completed

Making Files

A text file is a file that holds plain words and lines. Touch, echo, cp, and mv make and move text files.

An empty file

Touch creates an empty file, and ls shows its name.

$ touch ~/practice/hello.txt
$ ls ~/practice
$ rm ~/practice/hello.txt

One line with echo

Echo writes one line into the file, and cat prints it.

$ echo Hello > ~/practice/hello.txt
$ cat ~/practice/hello.txt
$ rm ~/practice/hello.txt

Many lines with cat

The lines go in with cat, and cat prints them back.

$ cat > ~/practice/note.txt <<'EOF'
First line
Second line
EOF
$ cat ~/practice/note.txt
$ rm ~/practice/note.txt

Copy and move

Cp copies a file, and mv gives the copy a new name.

$ cp ~/practice/notes/shopping.txt ~/practice/shopping-copy.txt
$ mv ~/practice/shopping-copy.txt ~/practice/shopping-moved.txt
$ ls ~/practice
$ rm ~/practice/shopping-moved.txt

Example

$ touch ~/practice/hello.txt
$ echo Hello > ~/practice/hello.txt
$ cat ~/practice/hello.txt
$ rm ~/practice/hello.txt
$ ls ~/practice

Key points

  • Text files hold words you can read as they are.
  • Use touch to create an empty text file.
  • Use echo with > to write one line into a text file.
  • A heredoc is a block of lines typed after a command, ending with a marker word.
  • Use cat with > to write many lines into a text file.
  • Use cp to copy a text file.
  • Use mv to move a text file or give it a new name.
  • Use cat to print a text file on the screen.

In this example, five lines make, write, print, and remove one file. The first line creates an empty file. The second line writes Hello into it. The third line prints Hello. The fourth line removes the file. The fifth line lists the practice directory, and hello.txt is gone.

Note

Echo with > writes over what a text file held before, so copy first when the old lines matter.

Try it Yourself

Edit the code, then press Run.

Running is simulated

Exercise

What does touch do?