User Tools

Site Tools


wiki:cli:files_and_directories

The part of the operating system responsible for managing files and directories is called the file system. It organizes our data into files, which hold information, and directories (also called “folders”), which hold files or other directories.

Several commands are frequently used to create, inspect, rename, and delete files and directories. To start exploring them, let's open a shell window:

$

The dollar sign is a prompt, which shows us that the shell is waiting for input; your shell may show something more elaborate.

Type the command whoami, then press the Enter key (sometimes marked Return) to send the command to the shell. The command's output is the ID of the current user, i.e., it shows us who the shell thinks we are:

$ whoami
jens

More specifically, when we type whoami the shell:

  • finds a program called whoami,
  • runs that program,
  • displays that program's output, then
  • displays a new prompt to tell us that it's ready for more commands.

Next, let's find out where we are by running a command called pwd (which stands for “print working directory”). At any moment, our current working directory is our current default directory, i.e., the directory that the computer assumes we want to run commands in unless we explicitly specify something else.

$ pwd
/home/bootcamp/jens/

To understand what a “home directory” is, let's have a look at how the file system as a whole is organized. At the top is the root directory that holds everything else. We refer to it using a slash character / on its own; this is the leading slash in /home/bootcamp/jens.

Inside that directory are several other directories: bin (which is where some built programs, also called binaries, are stored), data (for miscellaneous data files), users (where users' personal directories are located), tmp (for temporary files that don't need to be stored long-term), and so on:

We know that our current working directory /home/bootcamp/jensv is stored inside /home because /home is the first part of its name. Similarly, we know that /home is stored inside the root directory / because its name begins with /.

Underneath /home/bootcamp, we find one directory for each user with an account on this machine. The Simon's files are stored in /home/bootcamp/simon, Kara's in /home/bootcamp/kara, and Jens' in /home/bootcamp/jens.

Notice that there are two meanings for the / character.
When it appears at the front of a file or directory name,
it refers to the root directory. When it appears *inside* a name,
it's just a separator.

Let's see what's in my home directory by running ls, which stands for “listing”:

  $ ls
  
  examples.desktop   make   shell

ls prints the names of the files and directories in the current directory in alphabetical order, arranged neatly into columns. We can make its output more comprehensible by using the flag -F, which tells ls to add a trailing / to the names of directories:

  $ ls -F
  
  examples.desktop   make/   shell/

Here, we can see that /home/bootcamp/jens contains sub-directories. The names that don't have trailing slashes, like examples.desktop, are plain old files. And note that there is a space between ls and -F: without it, the shell thinks we're trying to run a command called ls-F, which doesn't exist.

What's In A Name?

You may have noticed that all of my files' names are “something dot
something”. This is just a convention: we can call a file mythesis or
almost anything else we want. However, most people use two-part names
most of the time to help them (and their programs) tell different kinds
of files apart. The second part of such a name is called the
filename extension, and indicates
what type of data the file holds: .txt signals a plain text file, .pdf
indicates a PDF document, .cfg is a configuration file full of parameters
for some program or other, and so on.

This is just a convention, albeit an important one. Files contain
bytes: it's up to us and our programs to interpret those bytes
according to the rules for PDF documents, images, and so on.

Naming a PNG image of a whale as whale.mp3 doesn't somehow
magically turn it into a recording of whalesong, though it *might*
cause the operating system to try to open it with a music player
when someone double-clicks it.

Let's go inside bootcamp with cd. cd stands for change directory.

  $ cd bootcamp

cd doesn't print anything.

Notice, by the way that we spelled the directory name. It doesn't have a trailing slash: that's added to directory names by ls when we use the -F flag to help us tell things apart. And it doesn't begin with a slash because it's a relative path, i.e., it tells ls how to find something from where we are, rather than from the root of the file system.

The leading / tells the computer to follow the path from the root of the filesystem, so it always refers to exactly one directory, no matter where we are when we run the command.

Let's make sure we are in bootcamp with pwd.

  $ pwd
  /home/users/jens/bootcamp

Let's look around with ls.

  $ ls
  shell     make

Now let's look a little bit closer let's run ls -a.

  $ ls -a -F
  ./         ../      shell/
  make/

-a stands for “show all”; it forces ls to show us file and directory names that begin with ., such as .. (which, if we're in /home/users/jens/bootcamp/, refers to the /home/users/jens directory). As you can see, it also displays another special directory that's just called ., which means “the current working directory”. It may seem redundant to have a name for it, but we'll see some uses for it soon.

Let's look at ls -F . and ls -F ..

  $ ls -F .
  shell/     make/
  $ ls -F ..
  desktop.examples    bootcamp/

We recognize the directory contents.

Let's go back to our home directory.

  $ cd ..
  $ pwd
  /home/bootcamp/jens
Orthogonality

The special names . and .. don't belong to ls;
they are interpreted the same way by every program.
For example,
if we are in /home/users/jens/data,
the command ls .. will give us a listing of /home/users/jens.
When the meanings of the parts are the same no matter how they're combined,
programmers say they are orthogonal:
Orthogonal systems tend to be easier for people to learn
because there are fewer special cases and exceptions to keep track of.

Let's take an excursion to the /bin directory.

  $ cd /bin
  $ ls

Does anyone recognize any of the programs?

How do we get back to our home directory?

Key Points

  • The file system is responsible for managing information on the disk.
  • Information is stored in files, which are stored in directories (folders).
  • Directories can also store other directories, which forms a directory tree.
  • / on its own is the root directory of the whole filesystem.
  • A relative path specifies a location starting from the current location.
  • An absolute path specifies a location from the root of the filesystem.
  • Directory names in a path are separated with / on Unix, but \\ on Windows.
  • .. means “the directory above the current one”; . on its own means “the current directory”.
  • Most files' names are something.extension. The extension isn't required, and doesn't guarantee anything, but is normally used to indicate the type of data in the file.
  • Most commands take options (flags) which begin with a -.
wiki/cli/files_and_directories.txt · Last modified: 2022/07/21 06:59 by 127.0.0.1