Creating Files and Directories


There are many ways to create files and directories from the command line. The method you choose depends on your purpose. Two common purposes are to capture the output of a command into a file, and to create a new, empty directory. Chapter 6 covers the more complex tasks of editing files from the command line, but we'll go over the basics here.

To create a new directory:

  • mkdir path

    The mkdir command (for make directory ) takes one or more arguments, each of which is a path of a new directory. The path (as with all path arguments) may be relative or absolute. The mkdir command produces no output unless an error is encountered .

Tip

  • Use ls to check that your new directory was created ( Figure 5.20 ).


Figure 5.20. Use the ls command to check that mkdir really created a directory. (What you see will be different.)
 localhost:~ vanilla$  ls  Current Projects Library       Pictures          bin Desktop          Movies        Public            system-status Documents        Music         Sites localhost:~ vanilla$  mkdir "My Projects"  localhost:~ vanilla$  ls  Current Projects Library       My Projects       Sites Desktop          Movies        Pictures          bin Documents        Music         Public            system-status localhost:~ vanilla$ 

To create a series of nested directories:

  • Use the -p option to mkdir .

    For example,

    mkdir -p test/ dir1 / dir2

    makes all the directories in the path if they do not already exist ( p for path ). So if test already exists, only dir1 and dir2 will be created; if test and dir1 already exist, then only dir2 will be created.

To create an empty file:

  • touch path

    The touch command takes one or more paths as arguments. If any of the files do not exist, they are created. The touch command updates the file's access time on each file. You could create several empty files at once. The following would create three files ( assuming none already existedif one of them did, its timestamp would be updated, but the contents of the file would be unaffected):

    touch file1 ../test/file2 /tmp/file3

Creating a file from command output

The output of most commands can be sent into a file instead of your screen by adding > path/to/file at the end of the command line. If the file already exists, the old contents are replaced . Use >> to append or create instead of replacing the old contents. (Review the section "To save output in a file" in Chapter 2.)



Unix for Mac OS X 10. 4 Tiger. Visual QuickPro Guide
Unix for Mac OS X 10.4 Tiger: Visual QuickPro Guide (2nd Edition)
ISBN: 0321246683
EAN: 2147483647
Year: 2004
Pages: 161
Authors: Matisse Enzer

Similar book on Amazon

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