using the terminal isnt hard
Apr 13, 2015
5 minutes read

For many of us who try to jump into programming in a relatively short period of time, with relatively little technical experience, interacting with computers without a graphical interface can seem daunting. Especially when we are expected to use this new strange interface with all of its new concepts and new terms, as a tool to help us write and manage our code. Its already overwhelming enough that you have to learn how to use complicated software to manage source control, collaboration, secure communication, and editing.

where are all the buttons!!!

where are the buttons!

Personally I felt ok doing the basic stuff like navigating around but when I encountered a problem with something the only thing to do was go straight to stack overflow. Although using internet searches specific to your problem works, in the long run some basic proficiency with the terminal will save you time.

Here are some things I have learned that made my life easier. Please share anything you think could be helpful to our fellow beginners!



Once you are in terminal land you are usually accessing parts of the file system you might have never seen before too. This is where it really helps to learn about file systems. Each major operating system linux, osx, and windows has its own conventions, although osx and linux are both unix-like so they are very similar.

When we are navigating and performing other operations we deal with file paths a lot. These can often be long and contain confusing names like so:

/var/www/ghost/lib

The first and last parts are very important. This is because a file path is a route from one point to another, separated by slashes. The thing before the first slash is the start and last thing is the destination, which could either be a directory or a file. File systems are tree structures like shown below:

They can be quite complex. What helped me with unix-like systems was to decide on two landmarks.


*The first is root:*

Whenever a file path starts with just a slash like the directory above it is starting from the root directory, which is the folder that contains everything. Every path technically starts at the root. If you use the cd command and go to cd / you can look at all the top level folders. Many of them will have weird names like bin and opt etc.

# what root directory looks like in ubuntu
root@command-center:/# ls
bin   dev  home        initrd.img.old  lib64       media  opt   root  sbin  sys  usr  vmlinuz
boot  etc  initrd.img  lib             lost+found  mnt    proc  run   srv   tmp  var  vmlinuz.old

I would encourage you to read more about what different directories are used for in various operating systems.


The second is the user directory.

This is where ‘user’ is the name of the current user. When you see a path that begins with ~ it is starting from the current users home dir.

example:

cd ~/projects/meteorStuff/microscope/

All paths really start from root but since we usually store things we use often in our own users directory the ~ alias stores the path to that directory so we don’t have to type it out. Then we just type out the path to the file or directory we want from that point. Tab completion is your friend.

Having landmarks is great because when you need to get to a different directory, instead of doing alot of cd ../ you can just use either / or ~ at the beginning of the path to keep it nice and short.

Often while following directions to install or run scripts in the terminal there are file paths involved, when things don’t work sometimes it is due to bad file paths. When you get an error that says something isn’t found, check if the path to the file or directory is correct!!! don’t go looking for another answer on google!


Side note:

A relative path is an address relative to the current directory (i.e., the directory in which a user is currently working). An absolute path (also called a full path) is an address relative to the root directory

The paths I have been showing you have mostly been absolute paths. When you use cd to change directories you often use a relative path.


$PATH


Another file path related issue is the $PATH environment variable. When you type any command into the terminal what is happening is not magic, there is a script somewhere in the file system that you are running. To run that script the computer has to know where the file is. $PATH is a variable that contains a colon separated list of paths. When you try to run run something it looks in each of the paths listed until it finds the script you are trying to run. If you echo out the $PATH variable it might look somewhat like this:

me@Macbook ~ $  echo $PATH
/usr/local/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/opt/X11/bin

# or if you want each list item on a new line
me@Macbook ~ $  echo $PATH | tr ':' '\
'
/usr/local/bin
/usr/local/bin
/usr/bin
/bin
/usr/sbin
/sbin
/opt/X11/bin

So if I run a command such as ls my machine will look at each of these paths until it finds a binary file called ‘ls’ under the /bin directory.

You can’t do much with installing and running command line tools without having to mess with $PATH a little.

I hope these little tidbits of knowledge help you out. Learning more about the shell (terminal) has been one of the most helpful parts of my continued learning. Like any skill, things seem hard at first but once you have a foundation of knowledge to build on learning new things gets easier all the time.


Back to posts