An operating system is made up of directories and files. A directory is another word for a folder on your computer. All directories and files have a path. A path is as an address where a directory or file exists in your operating system. When you are using the Bash command line shell, you are always in a directory, located at a specific path. You can see the path you are at by entering the command pwd in the Bash command line shell:
pwd
>> /Users/bernie
pwd stands for print working directory (a working directory is the directory you are
currently in), and the path that prints on your computer will be the name of whatever directory you are in.
The folders in your operating system are a tree. A tree is an important concept in Computer Science called a data structure (covered in Part IV). In a tree, there is a root at the top. The root can have branches, and each one of the branches can have more branches, and those branches can have branches. This goes on indefinitely.
[add illustration of a tree data structure]
root / \ home etc / \ bernie bin / \
test projects
Every branch on the tree is a directory, including the root. The tree shows how they are connected to each other. When you are using Bash, at any given time you are at one of the locations on the tree, and a path is a way of expressing that location. There are two ways of expressing a location on Unix-like operating systems: absolute paths and relative paths. An absolute path is a way of expressing a location starting from the root directory. An absolute path is made up of the name of folders in the tree, in order, separated by backslashes. The absolute path to the bernie directory (for the operating system illustrated above) is
/home/bernie . The first slash represents the root directory; followed by the home directory;
followed by another slash and the bernie directory.
The other way of specifying a location on your computer is called using a relative path.
Instead of starting at the root directory, a relative path is relative to the directory you are in.
Your operating system knows a path is relative when it doesn’t begin with a backslash. If we were located in the home directory in the example above, the relative path to the projects directory would be bernie/projects . If we were in the home directory, the relative path to bernie is simply bernie .
Navigating
You can use the cd command, which stands for change directory, to navigate to a directory using an absolute or relative path. Enter the cd command followed by the absolute path / to navigate to the root directory:
$ cd /
You are now in your root directory, which you can verify with the command pwd : $ pwd
>> /
The command ls , which stands for list directories, prints the folders in the directory you are in:
$ ls
>> bin dev initrd.img lost+found ...
You can create a new directory with the command mkdir . Directory names cannot have spaces in them. Use the mkdir command to make a new directory called tstp :
$ mkdir tstp
>>
Verify the new directory exists with the command ls . Now use the cd command to enter the tstp directory by passing in the relative path to tstp as a parameter:
$ cd tstp
Now use the cd command followed by two periods to return to the folder you were in before you entered the tstp directory:
$ cd ..
Delete the new directory using the command rmdir , which stands for remove directory:
$ rmdir tstp
Use the command ls to verify the directory was deleted.
Flags
Commands have a concept called flags that allow the issuer of the command to change the commands behavior. If you use a command without any flags, all of the commands flags are set to false. But if you add a flag to a command, the flag is set to true and the behavior of the command changes. The - and -- symbols are used to attach flags to a command. --author is an example of a flag you can add to the ls command to print the author of all of the
directories and files that get listed. This example is for Linux, on OS X you can use the same flag but you need to use one dash instead of two. Here is an example of using the --author flag with the ls command:
$ ls --author
>> drwx---+ 13 coryalthoff 442B Sep 16 17:25 Pictures ...
When you add the --author flag to your ls command, the name of each directory and folder in your current directory will print—as well as some additional information, including the name of the person that created them.
vim
Vim is a command line text editor. It's like Microsoft Word, except you use it from the command line. If you are using Ubuntu, first install vim with the command apt-get install vim . Make sure to enter Y when prompted. If you are using a Mac, it should come with vim If you are using the online bash shell, it already has vim installed.
You can create a new file with vim by going to the command line and using the command vim [name of the file to create] . Use the command vim self_taught.txt to create a new text file called self_taught.txt . Press the i or insert key and type a paragraph of text into the file. Any paragraph of text you find on the internet will do. The reason you have to hit the i or insert key when you first enter a file is because vim has different modes optimized for different activities. vim starts in, Normal Mode, which is not meant for adding text to the file (it is meant for easy navigation)—you can delete text in normal mode, but you cannot insert new text. Once you press the i or insert key and enter normal mode, you can use vim like a word processor—try typing into vim.
Since you can’t use your mouse to move the cursor around, it’s important to learn a few shortcuts to jump to different locations in your document, so that you don’t end up using the arrow keys on your keyboard (because that’s slow). To practice moving around, first make sure you are in Normal Mode ( control-c ). You can move to the beginning of a word by
pressing b and the end of a word with e . 0 will move you to the beginning of the line you are on, while the dollar sign $ will move you to the end of the line. H will move you to the first line of the page and L will move you to the last. You can delete entire lines of text in normal mode by pressing the d key twice. Spend some time using these keys to get familiar with with navigating through a file using vim.
To exit vim you need to first switch to Normal Mode by pressing control-c . Next press the shift key and then hit the colon key (while still holding the shift key). From here you can type q! if you want to quit without saving your changes to the file, or type x if you want save your changes and quit. Once you’ve typed one of these options, press the enter key to exit.
vim is useful in a few situations: servers are usually only accessed with a command line shell, so if you want to make changes to a file on a server, you need to use a command line text editor, and once you get good at using vim, it is often faster to use it to make changes than using a conventional word processor. Try typing vimtutor in the Bash command line shell and see what happens.
Touch
You can use the command touch followed by a filepath to quickly create a new file:
$ touch purple_carrots.txt
>>