UNIX Unbounded: A Beginning Approach

Chapter 5: Introduction to the UNIX File System


This chapter introduces the file structure of a UNIX system. The objectives important to this chapter are:

  • types of files in UNIX
  • directories
  • directory commands
  • listing directories
  • printing files

UNIX considers almost everything in it to be a file. It divides files into three types:

  • Regular files - source code, data programs, graphics, and most others
  • Directories - files that are organizational pockets for other files
  • Special files - contain information about peripherals; like resource objects in Novell NDS
  • The file system in UNIX is hierarchical, which means it has layers and can be drawn like a branching tree. The start of the directory structure is called the root directory.

    Every user on the system has a home directory which is the user's default current directory at the time of logging in.

    A current directory is simply the directory that the user is logged into at any given moment. It is also called the working directory.

    Paths are the description of where a file is in the directory structure. Absolute paths start at the root directory, and relative paths start at the current directory.

    Naming files according to accepted rules is important in any operating system. The rules in UNIX say to avoid almost any character in file names except:

    • Uppercase letters
    • Lowercase letters
    • Numbers
    • Underscores
    • Dots

    Unlike DOS, UNIX allows you to use as many dots (periods) in a file name as you like.

    Do not use spaces in file names.

    You can find out the current directory by using the pwd command. It reports what directory the machine is thinking about.

    Creating directories is easy. You would do well to organize your thoughts before doing so, as the reason to have directories is to organize your files. Create a new directory under inside the current one with the mkdir command, followed by the name of the new directory. By using a path, ending in the name of a new directory, you can create a new one in any directory, instead of just in the current one.

    You can use the -p option with mkdir to create a path instead of a single new directory. The command mkdir -p xx/yy/zz would create all three levels specified. It will not work unless none of them exist before the command is executed.

    The rmdir command can be used to remove an empty directory. It will not work if the named directory has files or other directories in it.

    The list command, ls, is used to list contents of directories. If you enter only the Ls command, without an argument, it reports the contents of the current directory. These listings are in ASCII key order, by default.

    To change from the current directory to another, use the cd command, followed by the path to the directory you want to make the current directory.

    The Ls command has several options. Some of the more useful ones are:

    • -a list all files, even hidden ones
    • -F put a slash after directory names in the list and a star after executables
    • -R recursively list files in all subdirectories, as well as the one specified
    • -l long listing format, gives more information

    The long format is often useful in determining critical information about files. It is rather cryptic. First, on the left, you see either a dash or a letter d. The dash means that the object listed is a file, and the d means that it is a directory. Next, in the same column, come nine characters. They are grouped in three groups of three. The first three are the rights the user who owns the file has in regard to the file. The user may have Read (R), Write (W) and Execute (X) rights. If any of these letters appear in the first group of three, they are what the user gets. The next group of three characters represent the same rights, but for members of the group that the user belongs to. And the last group of three represents the rights granted to all other users on the system. Any character may be a dash, meaning that the corresponding right is not assigned.

    Next, we see the number of links to this file. Trust me, this is not important yet.

    In the third column, we see the User name of the owner of the file.

    Fourth column, we see the Group that the owner belongs to .

    Fifth column, the size of the file in bytes.

    Sixth column (and a bit) shows the date the file was last modified.

    The last column shows the file name.

    Some files are not normally shown in a list. These are the hidden files. They are hidden because their names begin with a period. Yes, really, that's all it takes. And you can see them in the list by using the -a option with the Ls command.

    You can reference the parent directory of the current one with dot-dot (two periods) and reference the current directory with a single dot (one period).

    When using options in UNIX commands, you can stack them up. Simply follow the dash with as many as you like.

    So, what do you do to see what is in a file? Well, you could use our new friend vi, or cat. The cat command means to concatenate, and with only a file as an argument, it means to concatenate the file to the screen. The command

    cat filename

    is fine to put a short file on the screen. Our author mentions that a long one will scroll up the screen too fast to read. In that case, I recommend using the command
    cat filename | more 

    This command uses the pipe character to send output of the cat command through the more command, causing the display to pause when the screen is full. Much easier to read.

    The command to print a file is lp followed by the name of a file. It can be used with options, too. Review your text for some useful ones.

    Deleting files is easy. Sometimes, too easy. The rm command is followed by the name of a file to delete. It can be made very dangerous, by using it with the -r option. When pointed at a directory, the rm -r will delete the directory, and all its contents, even other subdirectories and their contents. Watch where you point that thing!