Chapter 2

Files, folders, and the tree

Everything on your computer is kept in files. Files are kept in folders. Folders are kept in other folders. This chapter explains how that is put together, and how you tell the computer where something is.

What a file is

A file is a saved lump of data with a name. A photo is a file. A song is a file. The C program you will write is a file.

The computer does not know what is inside a file until it looks. To the disk, every file is just a long line of numbers. What those numbers mean depends on the program that reads them.

The name does not decide what a file is

On Windows, a name ending in .txt feels like a kind of file. On Linux, that ending is only part of the name. Nothing more.

You can rename a photo to notes.txt. It is still a photo. You have only changed its name. This is why step 2 installs a command called file: it opens the file, looks at the first few numbers, and tells you what it really is.

What a folder is

A folder holds files. It can also hold other folders. You will see the word directory used for the same thing. Directory is the older word, and most commands use it. Folder and directory mean the same.

A folder does not really contain your files, the way a box contains books. It is a list. The folder holds a list of names, and where to find each one on the disk. This is a small point, but it explains a few things later.

One tree, not many drives

Windows gives each disk a letter. Your main disk is C:. A pen drive might be D:. Each one starts its own tree.

Linux does not do this. There is one tree. It starts at a single point called the root, written as one slash: /. Everything on the machine hangs somewhere below it.

/                     the root - the top of everything
├── bin               programs that come with the system
├── etc               settings files
├── home
│   └── yourname      your own folder
│       ├── practice
│       └── notes
├── mnt
│   └── c             your Windows disk, if you use WSL
├── tmp               space for junk, cleared often
└── usr               more programs and their files

Look at /mnt/c in that picture. Your Windows disk is not a separate world. It is a branch of the same tree. That is why you can reach your Windows files from Linux without doing anything special.

A path is a set of directions

A path tells the computer where something is. You build one by naming each folder on the way down, with a slash between the names.

/home/yourname/notes/day1.txt

Read it from left to right:

The first slash and the middle slashes do different jobs

The slash at the start means the root. The other slashes only separate names. So /home/yourname starts at the root. home/yourname does not.

Two ways to write a path

The shell always remembers one folder you are "in". It is called your current directory. The command pwd prints it.

Once there is a current directory, there are two ways to point at a file.

KindStarts withMeansExample
Absolute / The full way from the root. Works from anywhere. /home/asha/notes/day1.txt
Relative a name The way from where you are now. notes/day1.txt

An absolute path is like giving your full address. A relative path is like saying "two doors down". The second one is shorter, but it only works if the other person knows where you are standing.

This is why the same command can work and then fail

A student types cat notes/day1.txt and it works. Later the same command says No such file. Nothing is broken. They have moved to a different folder, and the relative path now points somewhere else.

When a file cannot be found, run pwd first. Most of the time the answer is there.

Four short names

Four names come up again and again. They are not folders you made. The system provides them.

NameMeans
/the root, the top of the tree
.the folder I am in now
..the folder one level up
~my home folder

Every folder holds . and .., even a folder you just made and left empty. They are put there for you. That is why cd .. works everywhere.

Why you must type ./hello

In step 3 you build a program and run it with ./hello, not hello.

When you type a bare name, the shell looks for it in a fixed list of system folders. Your new program is not in that list. Writing ./hello gives a path instead of a name, and a path says exactly where to look: here.

Your home folder

Every user on the machine gets one folder of their own, under /home. Yours is /home/ plus your username. You can write files there. You cannot write in most other places without special permission.

This is why the setup steps put things in your home folder: ~/python-samples, ~/java-samples, ~/c-samples. They belong to you, so nothing has to ask permission.

Hidden files

A file whose name starts with a dot is hidden. Plain ls does not show it. ls -a does.

Nothing is being kept secret. It is only a way to keep settings files out of sight. You have one already, called .bashrc. It holds settings for your shell, and several of the setup scripts add a line to it.

Three rules about names

Capital letters matter

Report.java and report.java are two different files on Linux. On Windows they are the same file. This surprises people who come from Windows.

It also explains a rule in step 5: a Java class called Report must be in a file called Report.java, with the same capital R.

Spaces cause trouble

The shell uses spaces to separate one word from the next. A file called my notes.txt looks like two things to the shell: my and notes.txt.

You can work around it with quotes. It is easier not to have the problem. Use a dash or an underscore instead: my-notes.txt or my_notes.txt.

The ending is only a habit

.txt, .c, .java are useful to people, and some programs do look at them. But Linux does not force it. The ending is part of the name, nothing more.

What deleting really does

Remember that a folder is a list of names. When you delete a file, the system removes its name from that list and marks the space as free.

There is no Recycle Bin in the terminal. Nothing is moved anywhere first. The name is simply gone, and the space will be used by the next thing that needs it.

This is why rm has no undo

There is nothing to undo to. The file was not put in a holding place. Read the whole line before you press Enter, every time.

Try it

Step 1.5 walks through these commands with a copy button on each one. If you have not done it yet, do it now. This chapter makes far more sense once your hands have done the work.

Two small things to try that are not on that page:

$ cd /home/../home/../etc

That is a silly path on purpose. It goes up and down and lands in /etc anyway. Run pwd after it. Paths are followed one step at a time, so a strange one still works.

$ ls -a ~

Count how many names begin with a dot. Those were all made for you when your account was created.

You can now explain