Create a file on Linux


Next:3.2.3 Copy, move andUp:3.2 First commands Previous:3.2.1 View the contents of the directory

Subsections


3.2.2 Create and delete directories and files

A directory system only makes sense if it contains files and directories. There are many programs available on the shell for creating and deleting files and directories.

3.2.2.1 Create

A simple way to create an empty file is with the command. ole @ defiant: ~> touch test file ole @ defiant: ~> ls -l total of 8 drwxr-xr-x 2 ole users 4096 Sep 25 12:25 Documents drwxr-xr-x 2 ole users 4096 Sep 25 12:25 public_html - rw-r - r-- 1 ole users 0 Sep 26 14:37 testfile

The command is actually supposed to set the time of the last change and the last access to the current time. If the specified file does not exist, a new file is simply created.

Creating a new directory is just as easy. The order is called here.

ole @ defiant: ~> mkdir test ole @ defiant: ~> ls Documents public_html test test file ole @ defiant: ~> mkdir job hobby ole @ defiant: ~> ls Documents job hobby public_html test test file

As with the command, you do not have to limit yourself to one parameter. You can easily specify multiple files or directories to be created.

It is also possible to specify the directory with its parent directories. However, these must exist beforehand.

ole @ defiant: ~> cd / ole @ defiant: /> mkdir / home / ole / test / newfolder ole @ defiant: /> ls / home / ole / test newfolder

3.2.2.2 Delete

What you create must also be able to be deleted. This applies to directories as well as files. The command (remove directory) takes care of deleting directories. ole @ defiant: ~> rmdir job ole @ defiant: ~> rmdir test rmdir: "test": The directory is not empty

However, it only deletes empty directories. As soon as there is only one file or directory in it, deletion of the directory is denied.

Files, on the other hand, are disposed of with the command (remove).

ole @ defiant: ~> rm test file ole @ defiant: ~> rm test rm: "test" is a directory

On the other hand, it is difficult with directories and refuses to delete them with a corresponding message.

This is also the normal way the shell commands work. If a command works, there is usually no comment, but the prompt asks the user to take action again. The commands only report to the user again if errors have occurred.

But you can also persuade them to delete directories. With the option (recursive) deletes directories With their content.

ole @ defiant: ~> rm -r test

Sometimes also asks whether a file should be deleted. If that is too annoying, especially when entire directory structures are to be deleted, then the switch ensures silence. It ensures that the files are deleted without prompting.

But you should be careful with the command. Just imagine what happens when the administrator enters the following:

defiant: ~ $ rm -rf /

3.2.2.3 The editor

The editor you'll find on every Linux / Unix system is the one or one of its clones. Its user guidance is a bit idiosyncratic and takes getting used to. But since it is available on every system, you should be familiar with it.

You can easily call up a file for editing by writing the name after the command.

ole @ defiant: ~> vi myfile

If the file does not exist, it assumes an empty new file, as you can see in the status line at the bottom.

"myfile" [new file] 0.0-1 Everything

The has different modes for the processing of documents. When you start, you end up in edit mode, which provides special functions for editing the documents. These functions are not made available via a menu, as is the case with most editors, but rather via keyboard shortcuts.

If you want to write text, you have to switch to input mode. To do this, press the key in edit mode. The status line now shows or.

- INSERT - 0.1 Everything

Here you can enter text, move through the text with the direction keys and delete characters to the right of the cursor with the key or.

To switch back to edit mode, simply press the button. To be on the safe side, you can also press the button several times. then acknowledges this with a beep.

In edit mode you can either delete the character under the cursor as in input mode with the key or by pressing the key. But there are also more extensive delete commands in connection with the key. So deletes the characters up to the end of the word including the following space. With he also deletes the rest of the word, but without taking the space with him. With the combination () he deletes the text from the cursor to the end of the line. And to delete the entire line in which the cursor is located, simply press the () key twice. You can also specify how often the command should be executed by placing a number in front of the command. So you can delete with four lines or with the three words to the right of the cursor.

The multiple execution also works with the input mode. Simply type in a number before switching on the input mode. After the input mode has ended, what has just been entered is output several times. So you can z. E.g. insert 75 minus signs into the text with the key combination.

You can also call up a command mode from the edit mode. This is done with the colon (). The colon appears in the lower status line and you can then type in further commands, which you conclude with the key.

Ends the
Finished without asking
Terminates and saves the document beforehand

In Linux, the clone is supplied and executed by the command. With the command you receive a small tutorial with exercises for operating the.



Next:3.2.3 Copy, move andUp:3.2 First commands Previous:3.2.1 View the contents of the directory