Initial commit: CLI walkthrough for CHEG 667-013
Six-module walkthrough covering navigation, files, reading/searching, processes/editors, scripting, and advanced tools (ssh, regex, tar, etc.). Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
commit
c57d7539d8
10 changed files with 1415 additions and 0 deletions
275
01-navigation/README.md
Normal file
275
01-navigation/README.md
Normal file
|
|
@ -0,0 +1,275 @@
|
|||
# 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<Tab>
|
||||
$ 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)
|
||||
162
01-navigation/WSL.md
Normal file
162
01-navigation/WSL.md
Normal file
|
|
@ -0,0 +1,162 @@
|
|||
# Installing WSL (Windows Subsystem for Linux)
|
||||
|
||||
This guide walks you through installing WSL on Windows 10 or 11. macOS and Linux users can skip this entirely.
|
||||
|
||||
---
|
||||
|
||||
## Step-by-step installation
|
||||
|
||||
### 1. Check your Windows version
|
||||
|
||||
Press `Win+R`, type `winver`, and press Enter. You need **Windows 10 build 19041 or later**, or **Windows 11**.
|
||||
|
||||
### 2. Open PowerShell as Administrator
|
||||
|
||||
This is important — a normal PowerShell window will not work.
|
||||
|
||||
- Click the **Start** button, type **PowerShell**
|
||||
- Right-click **Windows PowerShell** and select **Run as administrator**
|
||||
- Click **Yes** when prompted by User Account Control
|
||||
|
||||
Alternatively, right-click the Start button and choose **Terminal (Admin)**.
|
||||
|
||||
### 3. Install WSL
|
||||
|
||||
In the administrator PowerShell window, type:
|
||||
|
||||
```powershell
|
||||
wsl --install
|
||||
```
|
||||
|
||||
This single command does several things:
|
||||
|
||||
- Enables the WSL and Virtual Machine Platform features
|
||||
- Downloads the latest Linux kernel
|
||||
- Sets WSL 2 as the default version
|
||||
- Downloads and installs Ubuntu
|
||||
|
||||
You should see progress output as it downloads and installs.
|
||||
|
||||
### 4. Restart your computer
|
||||
|
||||
When the install finishes, **restart your computer**. This is required — the features enabled in step 3 won't be active until you reboot.
|
||||
|
||||
### 5. Launch Ubuntu
|
||||
|
||||
After restarting, open the **Start** menu and search for **Ubuntu**. Click it to launch.
|
||||
|
||||
The first time you open Ubuntu, it will spend a minute or two decompressing files. This is normal — just wait.
|
||||
|
||||
### 6. Create your Linux username and password
|
||||
|
||||
Ubuntu will prompt you to create a new account:
|
||||
|
||||
```
|
||||
Enter new UNIX username:
|
||||
```
|
||||
|
||||
Pick a simple lowercase name with no spaces (e.g., `jsmith` or your UDel login).
|
||||
|
||||
Next, it will ask for a password. **Nothing will appear on screen while you type your password.** This is normal Linux behavior — it's not broken, it's just hiding your input for security. Type your password and press Enter. You'll be asked to confirm it.
|
||||
|
||||
### 7. Verify it's working
|
||||
|
||||
You should now see a Linux prompt that looks something like:
|
||||
|
||||
```
|
||||
jsmith@DESKTOP-ABC123:~$
|
||||
```
|
||||
|
||||
Try a few commands to confirm everything is working:
|
||||
|
||||
```bash
|
||||
pwd
|
||||
ls
|
||||
whoami
|
||||
```
|
||||
|
||||
### 8. Update your packages
|
||||
|
||||
It's good practice to update Ubuntu right after installing:
|
||||
|
||||
```bash
|
||||
sudo apt update && sudo apt upgrade -y
|
||||
```
|
||||
|
||||
You'll be prompted for the password you just created.
|
||||
|
||||
You're all set!
|
||||
|
||||
---
|
||||
|
||||
## Troubleshooting
|
||||
|
||||
### `wsl --install` just shows help text
|
||||
|
||||
This means WSL was already partially enabled on your machine (perhaps from a previous attempt). Instead, run:
|
||||
|
||||
```powershell
|
||||
wsl --install -d Ubuntu
|
||||
```
|
||||
|
||||
### The install hangs or stalls at 0%
|
||||
|
||||
Try downloading the distribution from the web instead:
|
||||
|
||||
```powershell
|
||||
wsl --install --web-download -d Ubuntu
|
||||
```
|
||||
|
||||
### "The virtual machine could not be started" (error 0x80370102)
|
||||
|
||||
Your computer's hardware virtualization may be disabled. You need to enable it in your BIOS/UEFI settings:
|
||||
|
||||
1. Restart your computer and enter the BIOS (usually by pressing `F2`, `F10`, `Del`, or `Esc` during boot — it varies by manufacturer)
|
||||
2. Look for a setting called **Intel VT-x**, **Intel Virtualization Technology**, or **AMD-V** (usually under CPU or Advanced settings)
|
||||
3. Enable it, save, and restart
|
||||
|
||||
### "WslRegisterDistribution failed" (error 0x8007019e)
|
||||
|
||||
The WSL feature itself isn't enabled. Open PowerShell as Administrator and run:
|
||||
|
||||
```powershell
|
||||
Enable-WindowsOptionalFeature -Online -FeatureName Microsoft-Windows-Subsystem-Linux
|
||||
```
|
||||
|
||||
Then restart and try again.
|
||||
|
||||
### `wsl` is not recognized as a command
|
||||
|
||||
Make sure you're using a 64-bit PowerShell. Try typing `wsl.exe` instead of `wsl`.
|
||||
|
||||
### I forgot my Linux password
|
||||
|
||||
From PowerShell (not inside Ubuntu), run:
|
||||
|
||||
```powershell
|
||||
wsl -u root
|
||||
```
|
||||
|
||||
Then reset your password:
|
||||
|
||||
```bash
|
||||
passwd your_username
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## Launching WSL after installation
|
||||
|
||||
Once installed, you can open your Linux terminal in any of these ways:
|
||||
|
||||
- **Start menu**: Search for **Ubuntu**
|
||||
- **PowerShell or Command Prompt**: Type `wsl`
|
||||
- **Windows Terminal** (recommended): Ubuntu appears as an option in the dropdown tab menu. Windows Terminal supports multiple tabs and is a nicer experience overall.
|
||||
|
||||
---
|
||||
|
||||
## References
|
||||
|
||||
- [Install WSL (Microsoft docs)](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)
|
||||
- [WSL troubleshooting (Microsoft docs)](https://learn.microsoft.com/en-us/windows/wsl/troubleshooting)
|
||||
Loading…
Add table
Add a link
Reference in a new issue