Home Explore Blog CI



nushell

2nd chunk of `cookbook/system.md`
2011080ea8aa94f0a83521db0c08ed9fb588f8af1875b88f000000010000125e
# =>   9 │ bands.txt                       │ file │    156 B │ 2 hours ago
# =>  10 │ extra_features_cargo_install.sh │ file │     54 B │ 4 months ago
# =>  11 │ files                           │ file │      3 B │ an hour ago
# =>  12 │ payload.json                    │ file │     88 B │ 21 minutes ago
# =>  13 │ rustfmt.toml                    │ file │     16 B │ 10 months ago
# =>  14 │ urls.json                       │ file │    182 B │ 25 minutes ago
# => ────┴─────────────────────────────────┴──────┴──────────┴────────────────
```

---

### View all directories in the current directory

```nu
ls | where type == dir
# => ────┬───────────┬──────┬─────────┬───────────────
# =>  #  │   name    │ type │  size   │   modified
# => ────┼───────────┼──────┼─────────┼───────────────
# =>   0 │ .azureold │ dir  │     0 B │ 3 weeks ago
# =>   1 │ .cargo    │ dir  │     0 B │ 10 months ago
# =>   2 │ .vscode   │ dir  │     0 B │ 10 months ago
# =>   3 │ crates    │ dir  │ 12.3 KB │ 3 weeks ago
# =>   4 │ docs      │ dir  │  4.1 KB │ a day ago
# =>   5 │ images    │ dir  │  4.1 KB │ 2 weeks ago
# =>   6 │ pkg_mgrs  │ dir  │     0 B │ 10 months ago
# =>   7 │ samples   │ dir  │     0 B │ 10 months ago
# =>   8 │ src       │ dir  │  4.1 KB │ 3 hours ago
# =>   9 │ target    │ dir  │     0 B │ 2 weeks ago
# =>  10 │ tests     │ dir  │     0 B │ 4 months ago
# =>  11 │ wix       │ dir  │     0 B │ 2 weeks ago
# => ────┴───────────┴──────┴─────────┴───────────────
```

---

### Find processes sorted by greatest cpu utilization.

```nu
ps | where cpu > 0 | sort-by cpu | reverse
# => ───┬───────┬────────────────────┬───────┬─────────┬─────────
# =>  # │  pid  │        name        │  cpu  │   mem   │ virtual
# => ───┼───────┼────────────────────┼───────┼─────────┼─────────
# =>  0 │ 11928 │ nu.exe             │ 32.12 │ 47.7 MB │ 20.9 MB
# =>  1 │ 11728 │ Teams.exe          │ 10.71 │ 53.8 MB │ 50.8 MB
# =>  2 │ 21460 │ msedgewebview2.exe │  8.43 │ 54.0 MB │ 36.8 MB
# => ───┴───────┴────────────────────┴───────┴─────────┴─────────
```

---

### Find and kill a hanging process

Sometimes a process doesn't shut down correctly. Using `ps` it's fairly easy to find the pid of this process:

```nu
ps | where name == Notepad2.exe
# => ───┬──────┬──────────────┬──────┬─────────┬─────────
# =>  # │ pid  │     name     │ cpu  │   mem   │ virtual
# => ───┼──────┼──────────────┼──────┼─────────┼─────────
# =>  0 │ 9268 │ Notepad2.exe │ 0.00 │ 32.0 MB │  9.8 MB
# => ───┴──────┴──────────────┴──────┴─────────┴─────────
```

This process can be sent the kill signal in a one-liner:

```nu
ps | where name == Notepad2.exe | get pid.0 | kill $in
# => ───┬────────────────────────────────────────────────────────────────
# =>  0 │ SUCCESS: Sent termination signal to the process with PID 9268.
# => ───┴────────────────────────────────────────────────────────────────
```

Notes:

- `kill` is a built-in Nu command that works on all platforms. If you wish to use the classic Unix `kill` command, you can do so with `^kill`.
- Filtering with the `where` command as shown above is case-sensitive.

Title: Process Management in Nu
Summary
The section demonstrates process management in Nu, covering how to view directories, find processes with high CPU usage, and kill a hanging process. It shows how to use `ls` with `where` to list directories. It then demonstrates finding processes sorted by CPU utilization using `ps`, `where`, `sort-by`, and `reverse`. Finally, it explains how to find and kill a process by name using `ps`, `where`, `get`, and `kill`. It also notes the availability of the classic Unix `kill` command via `^kill` and highlights that the `where` command is case-sensitive.