Operating Systems / Chapter 2: File Systems / File Systems

Files

In the Computer I/O course, we studied how computers write to and read from storage devices. We were able to write to storage by issuing the "w" command in the shell. For example:

> w 3 John

Writes "John" to disk starting at location 3.

We were also able to read from storage by issuing the "r" command. For example:

> r 3 4

Reads 4 bytes (characters) from storage starting at location 3.

The read and write commands are very useful, but there is room for improvement. The following are the main issues:

Remembering data location and size is hard.

In a real computer, we can't physically see the data that's written on disk with our eyes, so if we don't remember the location of the data, we're in trouble. And trying to remember the size of the data is cumbersome at best (imagine writing a book and having to remember the number of characters in the book).

Data can be overwritten

We need to be careful not to overwrite data that is already present. For example, suppose that we issued the following command:

> w 0 JonathanJordan

Later, someone else issues this command:

> w 2 MikeSmith

After some time, we try to read what we wrote earlier with the following command:

> r 0 15

JoMikeSmithdan

That's not what we wrote! "MikeSmith" has overwritten parts of "JonathanJordan."

Files

One popular solution is to give the data a name. We can remember names more easily than numbers. Named data that is stored in disk is called a file.

In LWOS, we can use the "write" command to create files. Here is an example:

> write name.txt JonathanJordan

This command tells the computer to create a file called "my_name.txt" on disk with content "JonathanJordan." It is customary (but not required) to add an extension to the end of the name to indicate the type of the file. ".txt" means that this is a text file (as opposed to images, videos, and other types).

We can then ask LWOS to read this file from disk and print it on the screen using the cat command:

> cat my_name.txt

JonathanJordan

Here we are using the cat command. We supplied the name of the file to print (my_name.txt). If the file with the specified name does not exist, then the computer will print an error message:

> cat your_name.txt

cat: your_name.txt: No such file or directory

Now if someone else wants to store some information, they can create another file:

> write his_name.txt MikeSmith

> cat his_name.txt

MikeSmith

Since "MikeSmith" was stored in a different file (his_name.txt), my_name.txt still has the same text as before:

> cat my_name.txt

JonathanJordan

Creating text with spaces inside is a bit tricky:

> write you.txt Billy Idol

usage: write path data

Th shell printed an error message because the "write" command can only have 2 arguments. The shell treats "Billy" and "Idol" as two separate arguments, so as written, there are 3 arguments my_name.txt, Billy, and Idol.

If we surround the tokens Billy and Idol by double quotes, then the shell will treat them as 1 argument:

> write you.txt "Billy Idol"

> cat you.txt

Billy Idol

The write command is not convenient if we want to create multi-line text files, or edit an existing file. In real shells, we can use text editing programs such as vi and emacs. In GUI based operating systems, we can use Microsoft Word, Google Docs. At Logicwalk, we have already used text editors when we wrote instructions in the Computer Architecture course and code in the Python courses. We will also provide text editors in this course as necessary.

Try creating files and printing file contents using the simulator below:

Ready Logicwalk Computer Network
Quiz (1 point)

Please enter the LWOS command that will create a file called "location.txt" that contains "Australia" inside.

Become a subscriber to save your progress, see the correct answer, and more!

We want to ensure that the file names are different, so we don't mistakenly overwrite their contents. But when there are many files, say a million, it is hard to give all of them unique names. It is also difficult to find files in a list of million. We need a way to organize the files. Let's put files inside folders.

Previous Lesson Next Lesson

Comments

Please log in to add comments