Home Explore Blog CI



nushell

3rd chunk of `book/moving_around.md`
f96e65582312ab61eea4a938571ee4f72d8ab67a9e77e9c200000001000008b3
   The [`glob` command](/commands/docs/glob.html) (note: not the same as `into glob`) produces a [`list`](types_of_data.html#lists) of filenames that match the glob pattern. This list can be expanded and passed to filesystem commands using the [spread operator](operators.html#spread-operator):

   ```nu
   # Find files whose name includes the current month in the form YYYY-mm
   let current_month = (date now | format date '%Y-%m')
   ls ...(glob $"*($current_month)*")
   ```

3. Force `glob` type via annotation:

   ```nu
   # Find files whose name includes the current month in the form YYYY-mm
   let current_month = (date now | format date '%Y-%m')
   let glob_pattern: glob = ($"*($current_month)*")
   ls $glob_pattern
   ```

## Creating a Directory

As with most other shells, the [`mkdir` command](/commands/docs/mkdir.md) is used to create new directories. One subtle difference is that Nushell's internal `mkdir` command operates like the Unix/Linux `mkdir -p` by default, in that it:

- Will create multiple directory levels automatically. For example:

  ```nu
  mkdir modules/my/new_module
  ```

  This will create all three directories even if none of them currently exists. On Linux/Unix, this requires `mkdir -p`.

- Will not error if the directory already exists. For example:

  ```nu
  mkdir modules/my/new_module
  mkdir modules/my/new_module
  # => No error
  ```

  ::: tip
  A common mistake when coming to Nushell is to attempt to use `mkdir -p <directory>` as in the native Linux/Unix version. However, this will generate an `Unknown Flag` error on Nushell.

  Just repeat the command without the `-p` to achieve the same effect.
  :::

## Changing the Current Directory

```nu
cd cookbook
```

To change from the current directory to a new one, use the [`cd`](/commands/docs/cd.md) command.

Changing the current working directory can also be done if [`cd`](/commands/docs/cd.md) is omitted and a path by itself is given:

```nu
cookbook/
```

Just as in other shells, you can use either the name of the directory, or if you want to go up a directory you can use the `..` shortcut.

You can also add additional dots to go up additional directory levels:

```nu
# Change to the parent directory

Title: Creating Directories and Changing the Current Directory in Nushell
Summary
Nushell can convert strings to globs, list matching filenames, and then expand the results. Nushell's `mkdir` command creates directories, functioning like `mkdir -p` by default, creating multiple levels and not erroring if the directory already exists. The `cd` command changes the current directory; either the command or the directory name can be used. The `..` shortcut moves up directory levels.