Skip to content

Commit 1f263ae

Browse files
committed
Moving Around Fixes and Updates
1 parent eee5970 commit 1f263ae

File tree

1 file changed

+84
-24
lines changed

1 file changed

+84
-24
lines changed

book/moving_around.md

Lines changed: 84 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,25 @@ A defining characteristic of a shell is the ability to navigate and interact wit
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,78 @@ 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 a new directory. However, unlike some other `mkdir` implementations, Nushell's version:
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+
::: tip
141+
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.
142+
143+
Just repeat the command without the `-p` to achieve the same effect.
144+
145+
```
146+
147+
```
148+
149+
- Will not error if the directory already exists. For example:
150+
151+
```nu
152+
mkdir modules/my/new_module
153+
mkdir modules/my/new_module
154+
# => No error
155+
```
156+
157+
These features greatly simplify directory creation in scripting.
158+
116159
## Changing the Current Directory
117160

118-
@[code](@snippets/book/moving_around/cd_example.nu)
161+
```nu
162+
cd cookbook
163+
```
119164

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

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

124-
@[code](@snippets/book/moving_around/cd_without_command_example.nu)
169+
```nu
170+
cookbook/
171+
```
125172

126173
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.
127174

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

130-
@[code](@snippets/book/moving_around/multiple_cd_levels.nu)
177+
```nu
178+
# Change to the parent directory
179+
cd ..
180+
# or
181+
..
182+
Go up two levels (parent's parent)
183+
cd ...
184+
# or
185+
...
186+
# Go up three levels (parent of parent's parent)
187+
cd ....
188+
# Etc.
189+
```
131190

132191
::: tip
133192
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 `../../../..`
134193
:::
135194

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

138-
@[code](@snippets/book/moving_around/relative_cd_levels.nu)
197+
```nu
198+
cd ../sibling
199+
```
139200

140201
::: tip IMPORTANT TIP
141202
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 +209,6 @@ Nu also provides some basic [filesystem commands](/commands/categories/filesyste
148209
- [`mv`](/commands/docs/mv.md) to rename or move a file or directory to a new location
149210
- [`cp`](/commands/docs/cp.md) to copy an item to a new location
150211
- [`rm`](/commands/docs/rm.md) to remove items from the filesystem
151-
- [`mkdir`](/commands/docs/mkdir.md) to create a new directory
152212

153213
::: tip NOTE
154214
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 +217,4 @@ Under Bash and many other shells, most filesystem commands (other than `cd`) are
157217
- They are more tightly integrated with Nushell, allowing them to understand Nushell types and other constructs
158218
- 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.
159219

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.
220+
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)