You might think of directories as being drawers in a file cabinet; each drawer contains a bunch of files that are somehow related. For example, you might have a couple of file drawers for your unread magazines, one for your to-do lists, and maybe a drawer for your work projects.
Similarly, directories in your Unix system act as containers for other directories and files; each subdirectory contains yet more related directories or files, and so on. You’ll probably create a new directory each time you start a project or have related files you want to store at a single location. You create new directories using the mkdir command, as shown in Code Listing 2.1.
$ ls Projects all.programs.txt local.programs.txt schedule Xrootenv.0 files newer.programs short.fortunes all.programs fortunes newest.programs temp $ mkdir Newdirectory $ ls -l total 159 drwxrwxr-x 2 ejr users 1024 Jun 29 11:40 Newdirectory drwxrwxr-x 2 ejr users 1024 Jun 28 12:48 Projects -rw-rw-r– 1 ejr users 7976 Jun 28 14:15 all.programs -rw-rw-r– 1 ejr users 7479 Jun 28 14:05 all.programs.txt -rw-rw-r– 1 ejr users 858 Jun 28 12:45 files -rw-rw-r– 1 ejr ejr 128886 Jun 27 09:05 fortunes -rw-rw-r– 1 ejr users 0 Jun 28 14:05 local.programs.txt -rw-rw-r– 1 ejr users 497 Jun 28 14:13 newer.programs -rw-rw-r– 1 ejr users 7479 Jun 28 14:13 newest.programs lrwxrwxrwx 1 ejr users 27 Jun 26 11:03 schedule -> /home/deb/Pre -rw-rw-r– 1 ejr ejr 1475 Jun 27 09:31 short.fortunes drwxrwxr-x 2 ejr users 1024 Jun 26 06:39 temp $ |
1. | |
2. | mkdir Newdirectory Type the mkdir command to make a new directory; in this case, it’s called Newdirectory. Refer to the sidebar Naming Directories (and Files) for guidelines. |
3. | ls –l Now you can use ls -l (the -l flag specifies a long format) to look at the listing for your new directory (Code Listing 2.1). The d at the far left of the listing for Newdirectory indicates that it’s a directory and not a file. Of course, after you trust Unix to do as you say, you can skip this verification step. |
✓ Tips
If you attempt to create a directory with a file or directory name that already exists, Unix will not overwrite the existing directory. Instead, you’ll be told that a file by that name already exists. Try again with a different name.
You can create several directories and subdirectories at once with the –p flag. For example, if you want to create a new subdirectory called Projects with a subdirectory called Cooking in that and a subdirectory called Desserts in that, you can use mkdir –p Projects/Cooking/Desserts and get it all done at once. Without the –p flag, you have to create Projects, Cooking, then Desserts in order, which is a longer recipe to make the same tree structure.
13.58.190.38