You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: book/moving_around.md
+84-24Lines changed: 84 additions & 24 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -4,13 +4,25 @@ A defining characteristic of a shell is the ability to navigate and interact wit
4
4
5
5
## Viewing Directory Contents
6
6
7
-
@[code](@snippets/moving_around/ls_example.sh)
7
+
```nu
8
+
ls
9
+
```
8
10
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).
10
12
11
13
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"
@@ -21,21 +33,21 @@ The most general glob is `*`, which will match all paths. More often, you'll see
21
33
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.
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
113
125
ls $glob_pattern
114
126
```
115
127
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.
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 `../../../..`
134
193
:::
135
194
136
195
You can combine relative directory levels with directory names as well:
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
148
209
-[`mv`](/commands/docs/mv.md) to rename or move a file or directory to a new location
149
210
-[`cp`](/commands/docs/cp.md) to copy an item to a new location
150
211
-[`rm`](/commands/docs/rm.md) to remove items from the filesystem
151
-
-[`mkdir`](/commands/docs/mkdir.md) to create a new directory
152
212
153
213
::: tip NOTE
154
214
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
157
217
- They are more tightly integrated with Nushell, allowing them to understand Nushell types and other constructs
158
218
- 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.
159
219
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