-->
Previous Table of Contents Next


Creating and Deleting Files

Linux has many ways to create and delete files. In fact, some of the ways are so easy to perform that you have to be careful not to accidentally overwrite or erase files!


Warning:  
Go through the following sections very carefully. You should be logged in as your “ordinary” username, not as root! Only when you’re absolutely sure you understand these sections thoroughly should you use these commands while logged in as root.

There is no “unerase” command in Linux! Be sure you know what you’re doing!


Return to your home directory by typing cd. Make sure you’re in your /home/<user> directory by running pwd.

In the last chapter, you created a file by typing ls -l /bin > output. Remember, the > symbol means “redirect all output to the following filename.” Note that the file output didn’t exist before you entered this command. When you redirect to a file, Linux automatically creates the file if it doesn’t already exist.

What if you want to type text into a file rather than some command’s output? The quick and dirty way is to use the command cat.

cat: That Useful Feline

The cat command is one of the simplest, yet most useful, commands in Linux. (It certainly does more than any living feline!) The cat command basically takes all input and outputs it to a file or other source such as the screen. By default, cat takes its input from the keyboard and outputs it to the screen. Type cat at the command line:


darkstar:~$ cat

The cursor moves down to the next line, but nothing else seems to happen. Now cat is waiting for some input so type a few short lines:


hello

hello

what

what

asdf

asdf

Everything you type is repeated onscreen as soon as you press Enter!

How do you get out of this? At the start of a line, type ^D (Ctrl+D). (In other words, hold the Ctrl key and press D.) If you’re not at the beginning of a line, you have to type ^D twice. ^D is the Linux “end of file” character. When a program such as cat encounters a ^D, it assumes that it has finished with the current file, and it goes on to the next one. In this case, if you type ^D by itself on an empty line, there is no next file to go on to, and cat exits.

In this simple exercise, cat accepted input from the keyboard and displayed it back to you on the screen. Not a very useful command so far, is it? Fortunately, there’s a lot more flexibility to cat.


Note:  
When you say that a program exits, you mean that it has finished running and that you are back at the Linux command prompt. It may seem odd to talk about the program exiting when, from your point of view as a user, you have exited the program. This turn of phrase goes back to the early days of UNIX, when it was coined by the people who were programming the system. They looked at things from the program’s point of view, not the user’s!

So how do you use cat to create a file? Simple! You redirect the output of cat from the screen to a file:


darkstar:~$ cat > newfile

Hello world

Here’s some text

You can type as much as you want. When you are finished, press ^D by itself on a line; you are back at the Linux prompt. Instead of showing you each line as you typed it on the screen, cat has redirected it to a file called newfile.

Now you want to look at the contents of newfile. You can use the more or less commands, but instead, let’s use cat. Yes, you can use cat to look at files simply by providing it with a filename:


darkstar:~$ cat newfile

Hello world

Here’s some text

darkstar:~$

Cool! You can also add to the end of the file by using >>. Whenever you use >>, whether with cat or any other command, the output is always appended to the specified file. (Note that the ^D character does not appear onscreen. It’s shown in the examples for clarity.)


darkstar:~$ cat >> newfile

Some more lines

^D

darkstar:~$ cat newfile

Hello world

Here’s some text

Some more lines

darkstar:~$

To discover what cat actually stands for, let’s create another file.


darkstar:~$ cat > anotherfile

Different text

^D

darkstar:~$

Now, try this:


darkstar:~$ cat newfile anotherfile> thirdfile

darkstar:~$ cat thirdfile

Hello world

Here’s some text

Some more lines

Different text

darkstar:~$

cat stands for concatenate; cat takes all the specified inputs and regurgitates them in a single lump. We’ve concatenated newfile and anotherfile together into thirdfile. This by itself would not be very interesting, but combine it with the various forms of input and output redirection available in Linux and you have a powerful and useful tool.

Sometimes you want to change just one line of a file, or perhaps you are creating a large and complicated file and don’t want to rely on cat (which doesn’t allow you to correct errors). For this, you should use one of the editing programs available in Linux. They are discussed in Chapter 16, “Text Editors: vi and emacs.”


Previous Table of Contents Next