Chapter 1.1.3 - Command line tools
Back in the days, computers didn't have convenient user interfaces, and everything was done using the command line. Despite the advent of operating systems with accessible user interfaces, the command line is still a useful part of every system. In this section, you will learn how to use it.
Families of operating systems
There are two main families of operating systems systems: Unix and DOS. DOS was developed by Microsoft and it is used in Windows operating systems. Unix is derived from AT&T Unix, developed in 1970, and it is used in both Linux and Mac OS. The command line tools and syntax differs between these two systems, but we will use Unix throughout this course.
Current working directory
When you use a command line, you are located within a directory from which you can access other files and directories. For example, if you open the Terminal (command line) in Mac OS, by default the current working directory will be set as your home directory located in /Users/$USER (where $USER is your username). From there, you can access all files and directories within that folder with their respective names.
Executables
The command line allows to run executables (or, simply, programs). In order to do so, simply type the name of the executable you wish to run and pass the required arguments (parameters), if needed. For example, in Unix, cd is an executable that allow to change directories. If you are in your home folder on Mac OS, you can navigate to "Documents" by typing cd Documents. Here are other useful executables:
ls: lists the contents of the current working directory. If a parameter is passed, it lists the contents of that directory.pwd: prints the path to the current working directory.rm: removes the given file (for example,rm a.txt).mkdir: creates a new directory (for example,mkdir new-directory).
If you want to pass a parameter with spaces, wrap it inside quotation marks. For example, if you want to create a directory called "New Directory", type mkdir "New Directory".
There are many other executable that come with Linux/Mac OS and executables that you can install yourself, but these are the most common ones that you need to remember.
Flags
Executables can also accept flags to change their behavior. Flags are similar to arguments but are prefixed with either - or --. For example, ls accepts an -a flag which would force ls to print hidden files within the directory (hidden files are simply files that are not visible by default and whose names start with .). rm accepts -r flag which forces rm to traverse the files within the given directory (and their files) and delete them too. For example, if you were to run rm -r /Users/$USER/Desktop, you would erase all desktop contents! (You should probably not try it.)
Absolute vs relative paths
An absolute path to a file or a directory is a path that includes all folders starting from the root directory /. For example, the absolute path to Desktop on Mac OS is /Users/$USER/Desktop.
A relative path to a file or a directory is a path to it from your current working directory. For example, if your current working directory is /Users/$USER, the relative path to Desktop is Desktop.
Each Unix directory also contains a link to itself and to its parent directory:
- The alias for the parent directory is
... A parent directory is a directory that contains another directory, which is its child. For example,/Usersis a parent directory for$USER, which is its child directory. If your current working directory is/Users/$USERand you runcd .., your current working directory will change to/Users. - The alias for the current directory is
.. If your current working directory is/Users/$USERand you runcd ., your current working directory will remain the same.
Finally, there is another useful alias for the home directory: ~.
PATH
Usually, you need to give the path to an executable that you wish to run. For example, if you create an executable called my-executable and place it within your current working directory, you would need to run ./my-executable (note that you need to include the alias to the current working directory). However, it would be tedious to know where each executable is located within your filesystem, and so Unix provides a global variable called PATH that contains folders with executables that need to be accessed globally. When you type ls, your operating system actually goes through each folder in PATH and checks if it contains that executable. It then finds the directory with the absolute path of /bin and runs ls within it. If you want to know where a global executable is located, run where followed by the executable's name. For example, where ls outputs /bin/ls.