Creating Directories with mkdir


Creating Directories with mkdir

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.

Code Listing 2.1. Typing mkdir plus a directory name creates a new directory. Listing the files, in long format, shows the new directory. The "d" at the beginning of the line shows that it's a directory.

$ 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 $ 

To create a directory:

1.

ls

Start by listing existing directories to make sure that the planned name doesn't conflict with an existing directory or filename.

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.


Naming Directories (and Files)

As you start creating directories (and files), keep in mind the following guidelines:

  • Directories and files must have unique names. For example, you cannot name a directory Golf and a file Golf. You can, however, have a directory called Golf and a file called golf. The difference in capitalization makes each name unique. By the way, directories are often named with an initial cap, and filenames are often all lowercase.

  • Directory and filenames should not include the following characters: angle brackets (< >), braces ({ }), brackets ([ ]), parentheses (( )), double quotes (""), single quotes (' '), asterisks (*), question marks (?), pipe symbols (|), slashes (/ \), carets (^), exclamation points (!), pound signs (#), dollar signs ($), ampersands (&), and tildes (~).

    Different shells handle special characters differently, and some will have no problems at all with these characters. Generally, though, special characters are more trouble than they're worth.

  • Generally, avoid names that include spaces and hyphens (-). Some programs don't deal with them correctly, so to use spaces and hyphens you have to use odd workarounds. Instead, stick to periods (.) and underscores (_) to separate words, characters, or numbers.

  • Use names that describe the directory's or file's contents so you easily remember them.





Unix(c) Visual Quickstart Guide
UNIX, Third Edition
ISBN: 0321442458
EAN: 2147483647
Year: 2006
Pages: 251

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net