Skip to content

Commit 469ecab

Browse files
authored
Moving Around Fixes and Updates (nushell#1672)
* Moving Around Fixes and Updates * Fix title case * Updates from review comments
1 parent 61adb5c commit 469ecab

File tree

1 file changed

+80
-25
lines changed

1 file changed

+80
-25
lines changed

book/moving_around.md

Lines changed: 80 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,28 @@
1-
# Moving around the System
1+
# Moving Around the System
22

33
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:
44

55
## Viewing Directory Contents
66

7-
@[code](@snippets/moving_around/ls_example.sh)
7+
```nu
8+
ls
9+
```
810

9-
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).
11+
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).
1012

1113
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"
1214

13-
@[code](@snippets/moving_around/ls_shallow_glob_example.sh)
15+
```nu
16+
ls *.md
17+
# => ╭───┬────────────────────┬──────┬──────────┬──────────────╮
18+
# => │ # │ name │ type │ size │ modified │
19+
# => ├───┼────────────────────┼──────┼──────────┼──────────────┤
20+
# => │ 0 │ CODE_OF_CONDUCT.md │ file │ 3.4 KiB │ 9 months ago │
21+
# => │ 1 │ CONTRIBUTING.md │ file │ 11.0 KiB │ 5 months ago │
22+
# => │ 2 │ README.md │ file │ 12.0 KiB │ 6 days ago │
23+
# => │ 3 │ SECURITY.md │ file │ 2.6 KiB │ 2 months ago │
24+
# => ╰───┴────────────────────┴──────┴──────────┴──────────────╯
25+
```
1426

1527
## Glob Patterns (wildcards)
1628

@@ -21,21 +33,21 @@ The most general glob is `*`, which will match all paths. More often, you'll see
2133
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.
2234

2335
```nu
24-
ls **/*.md
25-
╭───┬───────────────────────────────┬──────┬──────────┬──────────────╮
26-
│ # │ name │ type │ size │ modified │
27-
├───┼───────────────────────────────┼──────┼──────────┼──────────────┤
28-
│ 0 │ CODE_OF_CONDUCT.md │ file │ 3.4 KiB │ 5 months ago │
29-
│ 1 │ CONTRIBUTING.md │ file │ 11.0 KiB │ a month ago │
30-
│ 2 │ README.md │ file │ 12.0 KiB │ a month ago │
31-
│ 3 │ SECURITY.md │ file │ 2.6 KiB │ 5 hours ago │
32-
│ 4 │ benches/README.md │ file │ 249 B │ 2 months ago │
33-
│ 5 │ crates/README.md │ file │ 795 B │ 5 months ago │
34-
│ 6 │ crates/nu-cli/README.md │ file │ 388 B │ 5 hours ago │
35-
│ 7 │ crates/nu-cmd-base/README.md │ file │ 262 B │ 5 hours ago │
36-
│ 8 │ crates/nu-cmd-extra/README.md │ file │ 669 B │ 2 months ago │
37-
│ 9 │ crates/nu-cmd-lang/README.md │ file │ 1.5 KiB │ a month ago │
38-
╰───┴───────────────────────────────┴──────┴──────────┴──────────────╯
36+
ls **/*.md
37+
# => ╭───┬───────────────────────────────┬──────┬──────────┬──────────────╮
38+
# => │ # │ name │ type │ size │ modified │
39+
# => ├───┼───────────────────────────────┼──────┼──────────┼──────────────┤
40+
# => │ 0 │ CODE_OF_CONDUCT.md │ file │ 3.4 KiB │ 5 months ago │
41+
# => │ 1 │ CONTRIBUTING.md │ file │ 11.0 KiB │ a month ago │
42+
# => │ 2 │ README.md │ file │ 12.0 KiB │ a month ago │
43+
# => │ 3 │ SECURITY.md │ file │ 2.6 KiB │ 5 hours ago │
44+
# => │ 4 │ benches/README.md │ file │ 249 B │ 2 months ago │
45+
# => │ 5 │ crates/README.md │ file │ 795 B │ 5 months ago │
46+
# => │ 6 │ crates/nu-cli/README.md │ file │ 388 B │ 5 hours ago │
47+
# => │ 7 │ crates/nu-cmd-base/README.md │ file │ 262 B │ 5 hours ago │
48+
# => │ 8 │ crates/nu-cmd-extra/README.md │ file │ 669 B │ 2 months ago │
49+
# => │ 9 │ crates/nu-cmd-lang/README.md │ file │ 1.5 KiB │ a month ago │
50+
# => ╰───┴───────────────────────────────┴──────┴──────────┴──────────────╯
3951
```
4052

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

128+
## Creating a Directory
129+
130+
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:
131+
132+
- Will create multiple directory levels automatically. For example:
133+
134+
```nu
135+
mkdir modules/my/new_module
136+
```
137+
138+
This will create all three directories even if none of them currently exists. On Linux/Unix, this requires `mkdir -p`.
139+
140+
- Will not error if the directory already exists. For example:
141+
142+
```nu
143+
mkdir modules/my/new_module
144+
mkdir modules/my/new_module
145+
# => No error
146+
```
147+
148+
::: tip
149+
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.
150+
151+
Just repeat the command without the `-p` to achieve the same effect.
152+
:::
153+
116154
## Changing the Current Directory
117155

118-
@[code](@snippets/book/moving_around/cd_example.nu)
156+
```nu
157+
cd cookbook
158+
```
119159

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

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

124-
@[code](@snippets/book/moving_around/cd_without_command_example.nu)
164+
```nu
165+
cookbook/
166+
```
125167

126168
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.
127169

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

130-
@[code](@snippets/book/moving_around/multiple_cd_levels.nu)
172+
```nu
173+
# Change to the parent directory
174+
cd ..
175+
# or
176+
..
177+
Go up two levels (parent's parent)
178+
cd ...
179+
# or
180+
...
181+
# Go up three levels (parent of parent's parent)
182+
cd ....
183+
# Etc.
184+
```
131185

132186
::: tip
133187
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 `../../../..`
134188
:::
135189

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

138-
@[code](@snippets/book/moving_around/relative_cd_levels.nu)
192+
```nu
193+
cd ../sibling
194+
```
139195

140196
::: tip IMPORTANT TIP
141197
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.
@@ -148,7 +204,6 @@ Nu also provides some basic [filesystem commands](/commands/categories/filesyste
148204
- [`mv`](/commands/docs/mv.md) to rename or move a file or directory to a new location
149205
- [`cp`](/commands/docs/cp.md) to copy an item to a new location
150206
- [`rm`](/commands/docs/rm.md) to remove items from the filesystem
151-
- [`mkdir`](/commands/docs/mkdir.md) to create a new directory
152207

153208
::: tip NOTE
154209
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:
@@ -157,4 +212,4 @@ Under Bash and many other shells, most filesystem commands (other than `cd`) are
157212
- They are more tightly integrated with Nushell, allowing them to understand Nushell types and other constructs
158213
- 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.
159214

160-
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.
215+
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.

0 commit comments

Comments
 (0)