Creating Directories

While you can create files several ways, you will use a single command to create directories: mkdir. You learned how to use this command in Creating Directories, but let’s dig in a little deeper by performing a common task: setting up a hierarchy of directories to store documents.

Navigate to your home directory, and then create a directory named files:

 $ ​​cd
 $ ​​mkdir​​ ​​files

Then, use the ls command to verify the new directory exists:

 $ ​​ls​​ ​​-l
 total 72
 -rw-r--r-- 1 brian brian 0 Mar 2 12:51 about.html
 -rw-r--r-- 1 brian brian 17 Mar 2 12:54 children.txt
 drwxr-xr-x 2 brian brian 4096 Mar 2 12:25 Desktop
 drwxr-xr-x 2 brian brian 4096 Mar 2 12:52 Documents
 drwxr-xr-x 2 brian brian 4096 Mar 2 11:59 Downloads
»drwxr-xr-x 2 brian brian 4096 Mar 2 13:04 files
 ...

Now, create a directory inside of the files directory called photos. Based on what you’ve learned so far, your first instinct might be to do this by changing the working directory to the files directory and then issuing another mkdir command, but that’s not efficient. Instead, specify the full relative path to the new directory:

 $ ​​mkdir​​ ​​files/photos

Like the ls, cat, and cd commands, the mkdir command accepts both relative and absolute paths, and it supports the double-dot syntax for parent directories. You don’t have to change your working directory when you want to manipulate files or directories in other locations.

Let’s create two more directories underneath files: one for movies and one for music files. You can pass multiple paths to the mkdir command, so you can create both directories like this:

 $ ​​mkdir​​ ​​files/movies​​ ​​files/music

You have to specify the path to each new directory, though. If you’d executed the command mkdir files/movies music, then the movies directory would be placed under files, but the music directory would end up in your current working directory.

Let’s create a structure to hold documents like Markdown files and diagrams. Specifically, let’s make the structure look like this:

 files
»├── docs
»│   ├── diagrams
»│   └── markdown
 ├── movies
 ├── music
 └── photos

You might be thinking you can do this:

 $ ​​mkdir​​ ​​files/docs/diagrams​​ ​​files/docs/markdown

Unfortunately, you’ll see this error message:

 mkdir: cannot create directory ‘files/docs/diagrams’: No such file or directory
 mkdir: cannot create directory ‘files/docs/markdown’: No such file or directory

To create a directory with the mkdir command, the parent directory must exist first. The files directory exists, but the docs directory doesn’t. So you could create the docs directory and then issue these two commands again, but you don’t have to.

The -p switch tells mkdir to create the entire path, including any directories that don’t yet exist. Try it out:

 $ ​​mkdir​​ ​​-p​​ ​​files/docs/diagrams​​ ​​files/docs/markdown

That’s pretty handy. The -p switch also suppresses errors if you attempt to create a directory that already exists. For example, if you tried to execute this command again:

 $ ​​mkdir​​ ​​files

You’ll get an error:

 mkdir: cannot create directory ‘files’: File exists

But if you use the -p switch, you’ll get no errors. This comes in handy when you automate things with scripts, which you’ll do in Chapter 9, Automation.

Let’s add one more structure—a place for the source code. You’ll make a dev directory, and beneath it, you’ll create the directories elm, go, and js. And instead of specifying each path, you’ll use a handy shortcut named brace expansion. You can place the four directories you want to create inside of curly braces, separated by commas, like this:

 $ ​​mkdir​​ ​​-p​​ ​​files/code/{elm,go,js}

This command creates all three directories under the files/code directory by taking the prefix files/code and appending it to each entry in the list between the braces. It’s a powerful shortcut, and you can use it in many places.

Use tree to ensure that your completed directory structure now looks like this:

 $ ​​tree​​ ​​files
 files
 ├── code
 │   ├── elm
 │   ├── go
 │   └── js
 ├── docs
 │   ├── diagrams
 │   └── markdown
 ├── movies
 ├── music
 └── photos

If you’re really trying to save time, you could create this entire structure in a single command since you can nest braces inside of braces:

 $ ​​mkdir​​ ​​-p​​ ​​files/{movies,music,photos,docs/{diagrams,markdown},code/{go,js,elm}}

But that’s just showing off.

You built this entire tree with a series of commands without ever leaving your current working directory. This ends up being a huge time-saver if you find yourself creating structures like this for projects. You can use the mkdir command to create directory structures anywhere on the filesystem as long as you have access to the parent directory. And you can prefix the mkdir command with sudo if you need to create the directory elsewhere.

Now that you have a directory structure to play with, it’s time to look at how you can manipulate files and directories.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.191.78.136