- 1 Meta information about this chapter
- 2 Chapter videos
- 3 Exercises
- 4 Solutions
- 5 Links
Meta information about this chapter
Expand using link to the right to see the full content.
Books this chapter belongs to
This chapter focuses on file and directory layout as a tool to organize your files and folders and create structure.
The purpose of this chapter is to provide some more bash commands which can be used for creating directories and navigating the file system. This is to make sure that the students have the knowledge required to keep their work organized in a directory structure. Regardless if the student will work as a programmer or database or systems administrator, it is important to keep the files in order. It is also important to understand how you navigate the file system, since most systems are organized in some kind of directory structure.
The goal of this chapter is that the students know basic commands for navigating the file system and dealing with directories.
Instructions to the teacher
Many students that are new to command line interfaces, have problems grasping the concept of directory trees and paths. In particular the concept of relative paths and absolute paths seems to be hard for some students. It's important to address this early in a course since it is a very fundamental concept which will be used more or less implicitly in more advanced tasks and exercises. Also, understanding the concepts of flags and arguments are equally important.
- Vimeo channel - Bash Structure (eng)
- Order and creating files (Eng)
- Vimeo-kanal - Struktur och ordning Struktur och ordning (Swe)
Other videos (from previous chapter if you want to re-visit):
- Föreläsning: Bash - filer och kataloger (Swe)
Ex 01 - Create a directory
The first thing we want to help you with, is to maintain some order in your file system. It is not a good idea to keep all of your files in the home directory or Desktop (or similar). So the first command we want to teach you is
mkdir (make directory).
Open your terminal. You are now in your home directory. Type the following:
$ mkdir my_first_directory
You have now created a directory (sometimes called “folder”) with the name
my_first_directory . Just knowing that the directory is there without knowing what to do with it is not so much fun, but we’ll get to that soon!
You may now list all files in the current directory (the directory you ended up in when you started your terminal, the so called home directory). The command for listing files is called
What files and/or directories does the command ls give you?
You might want to know if you also can see the my_first_directory directory in the file explorer. Type the following in the terminal:
$ explorer .
(If you are running cygwin on Windows - consult with your teacher if you are running some other operating system)
Yes, the argument to the explorer command is a single dot. In fact, every directory has a directory inside it by the name of “.” (a dot). The dot directory can be thought of as a nickname for “current directory” or “here, the directory where I am”.
What do you see in the file explorer? Note the path to the directory that was selected in the file explorer. Depending on where you installed cygwin, it should be something like:
That is Windows’ way of expressing a path. Cygwin, on the other hand, gives you a UNIX-like environment. In UNIX and UNIX-like systems (Mac OS X, GNU/Linux, etc), a path is written using / (forward slash) as the directory separator character. We’ll soon look at cygwin paths. But first, while you are at it, take the time to change some settings in the windows explorer (if you haven’t already done that).
Ex 02 - change the options of the file explorer
We want the Windows file explorer not to hide “hidden files”, and we want it to stop hiding file extensions for “known file types” (if you have already told it to act like this, great). In Explorer, go to the Tools menu and select Folder options, and there select the View tab. (In newer versions of windows, this dialog is hidden somewhere else, and we encourage you to use a search engine to find out where). If the following options are ticked, untick them:
- Do not show hidden files and folder (unselect that, we don’t want them to hide stuff!)
- Hide extensions for known file types (no, thanks! I want to see the actual names of files!)
Here’s an image showing what the result should look like:
Click OK and close the Explorer application.
Ex 03 - changing directory and listing contents
Now it’s time to go back to the terminal and change directory, using
cd, to my_first_directory. To change directory means that you enter a new directory, and that one becomes the current working directory. Enter the following:
$ cd my_first_directory $ ls
What files does ls now show you?
Looks pretty lonely in there. But remember that every directory has the nickname directory . (dot) in it. Why don’t we see it? We have to tell ls that we want to see also files and directories that start with a dot. That can be done by giving ls an option called a (minus a, meaning “all”):
$ ls -a
What did ls show you? The dot directory, and actually also the dotdot directory. The dotdot is another nickname directory, meaning “one directory above here”, or “the directory where this directory was created”.
This means that we may go back to our home directory using the cd command again, giving it the argument of
$ cd ..
How can we be sure where we are? Well, there are a few ways. By default, cygwin comes with settings that actually shows the path to the current directory as part of the prompt on the line above the cursor. Look at it. It now should say something like:
What a disappointment! That doesn’t look like the path to the home directory? We’ll it kind of does. There is a known nickname for the home directory as well. It is called ~ (tilde). To see it at work, cd down to my_first_directory again, and then cd to ~
$ cd my_first_directory $ cd ~
Note how the prompt changes. Now for the second way to find out where we are, le'ts use
pwd. Issue the following:
It should give you something like this:
From this we can conclude that the windows path
C:\cygwin\home\YouUserName(or where you installed cygwin) in cygwin is refered to as simply
/home/yourusername (whatever your username is, is used as the name for the home directory in the folder /home).
If we are not sure what Windows calls a path, we can actually query cygwin. Enter the following:
$ cygpath -w ~
You gave cygpath the option w meaning “in Windows expression” and the argument ~ which we now know is a known nickname for our home directory.
We want you to put files that belong together in a directory that you create. For instance it’s a good ide to create one directory for every exercise session. You now know how to create a directory:
and how to enter it
$ cd directoryname
and how to move up one level
$ cd ..
and how to get to the home directory
$ cd ~
We also learned how to list files in current directory
and how to list all files including those whose name start with a dot
$ ls -a
Further, we saw that we could start the windows file explorer from the command line in cygwin:
$ explorer .
We learned some terminology. We can give a command an option like a and that options seem to start with a dash (a minus sign). We can also give commands arguments. Options often tell a command how to do something (list files, but include files starting with a dot). Arguments often tell a command what to do (e.g. "change directory to this directory", "list the contents of that directory").
Expand using link to the right to see the full content.
Solution to ex 01
When you run
ls directly after creating the directory, ls should list at least that directory. Without the
-a flag, ls only shows normal files and directories (files and directories whose name start with a dot are not shown by default).
Solution to ex02
There is no suggested solution to this exercise, you are just supposed to change the settings of the file explorer.
Solution to ex03
ls -a should show you all files, including those whose name start with a dot. The two special files
. (a dot) and
.. (two dots) work as aliases (nicknames) for two directories. The single dot is always an alias for the current directory (doing
cd . would mean "change directory to current directory", i.e. do nothing). The double dot is always an alias to the directory directly "above" current directory, which in other words is the directory where current directory was created.