# CLI Part I: Navigation **CHEG 667-013 — Chemical Engineering with Computers** Department of Chemical and Biomolecular Engineering, University of Delaware --- ## Key idea Install and use the command line interface to navigate the file system. ## Key goals - Open a terminal on your operating system - Navigate the file system with `pwd`, `ls`, and `cd` - Understand the root directory, absolute and relative paths, file permissions, and linked files - Get help using `man` pages --- Our goal this week is to learn about the command line interface (CLI). We will start by discussing computer operating systems and the CLI. As we progress, we will learn about various tools for automating tasks, writing and running programs, including shell scripts, and manipulating the file system. Our focus will be on Unix commands. This is the native environment for Linux and macOS. For Windows users, there is a CLI called *PowerShell*, but it uses different commands. While it is worth familiarizing yourself with PowerShell, to follow along you'll need to install the *Windows Subsystem for Linux*. As of 2019, WSL 2 runs a virtualized full Linux kernel. ## 1. Running a command line interface ### Windows Windows 10 and 11 users need to install the *Windows Subsystem for Linux* (WSL). In short: open PowerShell **as Administrator**, run `wsl --install`, restart your computer, then launch Ubuntu from the Start menu to create a username and password. See [**WSL.md**](WSL.md) for detailed step-by-step instructions and troubleshooting. - [How to install Linux on Windows with WSL](https://learn.microsoft.com/en-us/windows/wsl/install) - [Set up a WSL development environment](https://learn.microsoft.com/en-us/windows/wsl/setup/environment) - An alternative to WSL is to run a virtual machine (VM) using a hypervisor program like VMWare or VirtualBox. The latter is available at https://www.virtualbox.org ### macOS and Linux Because the Macintosh OS is a derivative of Unix, users will have access to the Unix command line interface through the *Terminal* program. Just run Terminal. Most of the commands we will discuss are more or less identical between Linux and macOS, but there can be some differences. Most Linux installations will boot to a GUI like Gnome or KDE. From there, you can run a terminal program like *xterm*. > **Exercise 1:** Open a terminal window. Install WSL, if necessary. ## 2. Two tips before we start Before we dive in, two things that will make your life easier from day one: **Tab completion.** When typing a file or directory name, press the `Tab` key and the shell will try to complete it for you. If there's only one match, it fills in the rest. If there are multiple matches, press `Tab` twice to see the options. This saves enormous amounts of typing and prevents typos. Get in the habit of using it — experienced users press `Tab` constantly. ``` $ cd Dow $ cd Downloads/ ``` **Clearing the screen.** When your terminal gets cluttered with output, type `clear` or press `Ctrl-L` to get a fresh screen. Your history is still there — just scroll up. ## 3. Navigating the file system You should see a prompt. The prompt might include user and directory information. Here are some commands we use to navigate the file system: - `pwd` — return working directory name - `ls` — list directory contents - `cd` — change directory The command `pwd` is perhaps the easiest to understand; it shows us our current working directory: ``` $ pwd /home/ef1j ``` This tells us that we're in the *home directory* of user `ef1j`. We see that it has the *absolute path* of `/home/ef1j`. We'll look at this more in detail below. **NOTE:** The tilde character, `~`, is special. It can be used to refer to our home directory or the home directory of another user, as in `ls ~luser`. Each command can have a number of options. Let's take a closer look at the `ls` command. I might see something like this when I type it after starting the terminal: ``` $ ls Desktop/ Downloads/ Pictures/ Templates/ Documents/ Music/ Public/ Videos/ ``` The command shows that there are several *directories* in `/home/ef1j` (Desktop, Downloads, etc.). I can get more information using the option `-l`: ``` $ ls -l total 4 drwxr-xr-x 2 ef1j ef1j 2 Jul 30 2024 Desktop/ drwxr-xr-x 2 ef1j ef1j 2 Jul 30 2024 Documents/ drwxr-xr-x 2 ef1j ef1j 2 Jul 30 2024 Downloads/ drwxr-xr-x 2 ef1j ef1j 2 Jul 30 2024 Music/ drwxr-xr-x 2 ef1j ef1j 2 Jul 30 2024 Pictures/ drwxr-xr-x 2 ef1j ef1j 2 Jul 30 2024 Public/ drwxr-xr-x 2 ef1j ef1j 2 Jul 30 2024 Templates/ drwxr-xr-x 2 ef1j ef1j 2 Jul 30 2024 Videos/ ``` which now shows the owner of each directory (`ef1j`), its group, and the date it was created or last modified. If I type the command `ls -a`, I might see the following: ``` $ ls -a ./ .dbus/ .mozilla/ Videos/ ../ Desktop/ Music/ .vnc/ .bash_aliases Documents/ Pictures/ .wget-hsts .bash_history Downloads/ .profile .Xauthority .bash_logout .gnupg/ Public/ .xsession-errors .bashrc .ICEauthority .ssh/ .cache/ .lesshst .sudo_as_admin_successful .config/ .local/ Templates/ ``` It shows a number of *hidden files and directories* that are part of the system settings or used for application support. ## 4. Getting information and help Now is a good time to introduce where we can get some help with Unix commands. Unix systems have a built-in help system of *manual pages*. The command to access these is `man`. - Type `man ls` to see the options for the list command. The `man` command creates a formatted *man page* in the terminal window. (Hence the answer to the oft-asked question of how a command works: *"RTFM!"* or *"Read the freakin' man (page)!"*) - Use *space* to scroll ahead, *u* to move back up, and */* to search for a term. > **Exercise 2:** Try several `ls` commands and options, like: > - `ls -l` — long format > - `ls -lt` — long format, newest first > - `ls -lh` — long format, "human readable output" > - `ls -F` — append indicator (one of `*/=>@|`) to entries > > Note how you can combine options. Use the `man` page to find more options to try. Maybe these will be more interesting once we have more files? Note the following: - Command options can be separate or combined: `ls -l -t` is the same as `ls -lt`. Try both! - Sometimes options have input. ## 5. Changing directories When we start a terminal session, usually we're placed in our home directory. This is part of the file system structure that you may normally see as folders in a GUI. Try going one step higher in the file system: ``` $ cd .. ``` In my case, I go one directory up to the `/home` directory. If I list the current directory, I see my user folder, and if I use the `pwd` command, it tells me that I'm in `/home`. ``` $ ls ef1j/ $ pwd /home ``` Note the following: - Two dots, `..`, refers to the directory immediately above the current directory. - One dot, `.`, refers to the current directory. It can be important in some commands, including running a program in the current directory. A few tips: - Typing `cd` alone will take you to your home directory. - `cd -` will take you back to the directory before the last `cd` command. Try it! ## 6. Absolute and relative paths You'll encounter two ways to specify a location in the file system: An **absolute path** starts from the root directory `/` and spells out the full location. It works no matter where you are in the file system: ``` $ cat /home/ef1j/cheg667/bar ``` A **relative path** starts from your current directory. If you're already in `/home/ef1j`, you can write: ``` $ cat cheg667/bar ``` Both commands do the same thing — one is just shorter. Here are the special shortcuts for relative paths: | Symbol | Meaning | Example | |--------|---------|---------| | `.` | Current directory | `./a.out` (run a program here) | | `..` | Parent directory | `cd ..` (go up one level) | | `~` | Home directory | `cd ~/cheg667` | A good rule of thumb: use absolute paths in scripts (so they work regardless of where you run them) and relative paths when typing interactively (less typing). ## 7. Root directory Go one more level up using `cd ..` or type `cd /`. If you use `ls`, you should see something like this: ``` $ ls bin@ dev/ home/ lib32@ libx32@ mnt/ proc/ run/ snap/ sys/ usr/ boot/ etc/ lib@ lib64@ media/ opt/ root/ sbin@ srv/ tmp/ var/ ``` This is the *root* directory. It contains directories that hold many of the system files. Here are a few you might see: - `/bin` — executable programs or binaries (in this case, it points to `/usr/bin`) - `/boot` — system startup files - `/dev` — system device files - `/etc` — system configuration files - `/lib` — various *libraries* that the different software uses - `/media` — removable drives and disks will normally show up here - `/mnt` — mount point for manually mounting drives and devices - `/opt` — installed software, like `anaconda` environments - `/proc` — computer info (process information pseudo-file system) - `/root` — root user home - `/sbin` — superuser programs - `/sys` — information about devices - `/tmp` — temporary files - `/usr` — system files, including executables and binaries - `/var` — log files, lock files, spool files, and other system info > **Exercise 3:** Explore! Using `cd` and `ls`, look around the file system. Use `ls -l` and `ls -lt` to look at when the files and directories were created or modified. Who is the owner of the file? Be sure to look at the files in `/bin` or `/usr/bin`. You should be able to find the programs we've been using (`ls`, `cd`, `man`) and many others. ### Linked files You might see some files like this: ``` lrwxrwxrwx 1 root root 7 Apr 24 2022 bin -> usr/bin/ ``` The `l` indicates that this is a *linked* file or directory. Notice how it points to a different directory, `/usr/bin`. This allows the file system to have more than one name for a file or directory. See `man ln` for more information. ### File permissions In my directory listing, I also see the following for the `/etc` directory: ``` drwxr-xr-x 105 root root 195 Feb 4 16:12 etc/ ``` The letters indicate that the file is a directory. There are three groups of letters after this that indicate permissions: - `r` — read, `w` — write, and `x` — execute Each of the three groups corresponds to: - the file owner, the group, and all or everyone. In this case, `root` is the owner. This is the super-user or system administrator. The user `root` can read, write, or execute files and programs in `/etc`. Members of the `root` group can, too. As a normal user who is not in the `root` group, we can only read and execute files and programs. ## Exercises > **Exercise 4:** In your home directory, type `ls -R`. What does this command do? What do you see? > **Exercise 5:** Type `ls /root`. What happens? Why? > **Exercise 6:** What does the `cat` command stand for? What are some other uses of the command besides creating and printing a file? ## Additional resources and references - [Command line for beginners](https://ubuntu.com/tutorials/command-line-for-beginners#1-overview) - [Using Linux](https://linuxcommand.org) - [The Linux filesystem explained](https://www.linuxfoundation.org/blog/blog/classic-sysadmin-the-linux-filesystem-explained)