Skip to content
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
110 changes: 85 additions & 25 deletions book/moving_around.md
Original file line number Diff line number Diff line change
@@ -1,16 +1,28 @@
# Moving around the System
# Moving Around the System

A defining characteristic of a shell is the ability to navigate and interact with the filesystem. Nushell is, of course, no exception. Here are some common commands you might use when interacting with the filesystem:

## Viewing Directory Contents

@[code](@snippets/moving_around/ls_example.sh)
```nu
ls
```

As seen in other chapters, the [`ls`](/commands/docs/ls.md) command returns the contents of a directory. Nushell's `ls` will return the contents as a [table](types_of_data.html#tables).
As seen in the Quick Tour, the [`ls`](/commands/docs/ls.md) command returns the contents of a directory. Nushell's `ls` will return the contents as a [table](types_of_data.html#tables).

The [`ls`](/commands/docs/ls.md) command also takes an optional argument to change what you'd like to view. For example, we can list the files that end in ".md"

@[code](@snippets/moving_around/ls_shallow_glob_example.sh)
```nu
ls *.md
# => ╭───┬────────────────────┬──────┬──────────┬──────────────╮
# => │ # │ name │ type │ size │ modified │
# => ├───┼────────────────────┼──────┼──────────┼──────────────┤
# => │ 0 │ CODE_OF_CONDUCT.md │ file │ 3.4 KiB │ 9 months ago │
# => │ 1 │ CONTRIBUTING.md │ file │ 11.0 KiB │ 5 months ago │
# => │ 2 │ README.md │ file │ 12.0 KiB │ 6 days ago │
# => │ 3 │ SECURITY.md │ file │ 2.6 KiB │ 2 months ago │
# => ╰───┴────────────────────┴──────┴──────────┴──────────────╯
```

## Glob Patterns (wildcards)

Expand All @@ -21,21 +33,21 @@ The most general glob is `*`, which will match all paths. More often, you'll see
Nushell also supports a double `*` which will traverse paths that are nested inside of other directories. For example, `ls **/*` will list all the non-hidden paths nested under the current directory.

```nu
ls **/*.md
╭───┬───────────────────────────────┬──────┬──────────┬──────────────╮
│ # │ name │ type │ size │ modified │
├───┼───────────────────────────────┼──────┼──────────┼──────────────┤
│ 0 │ CODE_OF_CONDUCT.md │ file │ 3.4 KiB │ 5 months ago │
│ 1 │ CONTRIBUTING.md │ file │ 11.0 KiB │ a month ago │
│ 2 │ README.md │ file │ 12.0 KiB │ a month ago │
│ 3 │ SECURITY.md │ file │ 2.6 KiB │ 5 hours ago │
│ 4 │ benches/README.md │ file │ 249 B │ 2 months ago │
│ 5 │ crates/README.md │ file │ 795 B │ 5 months ago │
│ 6 │ crates/nu-cli/README.md │ file │ 388 B │ 5 hours ago │
│ 7 │ crates/nu-cmd-base/README.md │ file │ 262 B │ 5 hours ago │
│ 8 │ crates/nu-cmd-extra/README.md │ file │ 669 B │ 2 months ago │
│ 9 │ crates/nu-cmd-lang/README.md │ file │ 1.5 KiB │ a month ago │
╰───┴───────────────────────────────┴──────┴──────────┴──────────────╯
ls **/*.md
# => ╭───┬───────────────────────────────┬──────┬──────────┬──────────────╮
# => │ # │ name │ type │ size │ modified │
# => ├───┼───────────────────────────────┼──────┼──────────┼──────────────┤
# => │ 0 │ CODE_OF_CONDUCT.md │ file │ 3.4 KiB │ 5 months ago │
# => │ 1 │ CONTRIBUTING.md │ file │ 11.0 KiB │ a month ago │
# => │ 2 │ README.md │ file │ 12.0 KiB │ a month ago │
# => │ 3 │ SECURITY.md │ file │ 2.6 KiB │ 5 hours ago │
# => │ 4 │ benches/README.md │ file │ 249 B │ 2 months ago │
# => │ 5 │ crates/README.md │ file │ 795 B │ 5 months ago │
# => │ 6 │ crates/nu-cli/README.md │ file │ 388 B │ 5 hours ago │
# => │ 7 │ crates/nu-cmd-base/README.md │ file │ 262 B │ 5 hours ago │
# => │ 8 │ crates/nu-cmd-extra/README.md │ file │ 669 B │ 2 months ago │
# => │ 9 │ crates/nu-cmd-lang/README.md │ file │ 1.5 KiB │ a month ago │
# => ╰───┴───────────────────────────────┴──────┴──────────┴──────────────╯
```

Here, we're looking for any file that ends with ".md". The double-asterisks further specify _"in any directory starting from here."_
Expand Down Expand Up @@ -113,29 +125,78 @@ The quoting techniques above are useful when constructing glob-literals, but you
ls $glob_pattern
```

## Creating a Directory

As with most other shells, the [`mkdir` command](/commands/docs/mkdir.md) is used to create a new directory. However, unlike some other `mkdir` implementations, Nushell's version:

- 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`.

::: 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.

```

```

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

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

These features greatly simplify directory creation in scripting.

## Changing the Current Directory

@[code](@snippets/book/moving_around/cd_example.nu)
```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:

@[code](@snippets/book/moving_around/cd_without_command_example.nu)
```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:

@[code](@snippets/book/moving_around/multiple_cd_levels.nu)
```nu
# Change to the parent directory
cd ..
# or
..
Go up two levels (parent's parent)
cd ...
# or
...
# Go up three levels (parent of parent's parent)
cd ....
# Etc.
```

::: tip
Multi-dot shortcuts are available to both internal Nushell [filesystem commands](//commands/categories/filesystem.html) as well as to external commands. For example, running `^stat ....` on a Linux/Unix system will show that the path is expanded to `../../../..`
:::

You can combine relative directory levels with directory names as well:

@[code](@snippets/book/moving_around/relative_cd_levels.nu)
```nu
cd ../sibling
```

::: tip IMPORTANT TIP
Changing the directory with [`cd`](/commands/docs/cd.md) changes the `PWD` environment variable. This means that a change of a directory is kept to the current scope (e.g. block or closure). Once you exit the block, you'll return to the previous directory. You can learn more about this in the [Environment](./environment.md) chapter.
Expand All @@ -148,7 +209,6 @@ Nu also provides some basic [filesystem commands](/commands/categories/filesyste
- [`mv`](/commands/docs/mv.md) to rename or move a file or directory to a new location
- [`cp`](/commands/docs/cp.md) to copy an item to a new location
- [`rm`](/commands/docs/rm.md) to remove items from the filesystem
- [`mkdir`](/commands/docs/mkdir.md) to create a new directory

::: tip NOTE
Under Bash and many other shells, most filesystem commands (other than `cd`) are actually separate binaries in the system. For instance, on a Linux system, `cp` is the `/usr/bin/cp` binary. In Nushell, these commands are built-in. This has several advantages:
Expand All @@ -157,4 +217,4 @@ Under Bash and many other shells, most filesystem commands (other than `cd`) are
- They are more tightly integrated with Nushell, allowing them to understand Nushell types and other constructs
- As mentioned in the [Quick Tour](quick_tour.html), they are documented in the Nushell help system. Running `help <command>` or `<command> --help` will display the Nushell documentation for the command.

While the use of the Nushell built-in versions is typically recommended, it is possible to access the Linux binaries. See [Escaping to system](escaping.html#escaping-to-the-system) for details.
While the use of the Nushell built-in versions is typically recommended, it is possible to access the Linux binaries. See [Running System Commands](./running_externals.md) for details.
Loading