Teach Yourself UNIX Shell Programming in 14 Days

Chapter 2: UNIX Tools

Objectives:

This chapter discusses UNIX concepts such as files, directories and the UNIX file system. Objectives important to this chapter are:

  • Manipulation of UNIX files
  • Creation and removal of files and directories
  • Directory navigation
  • Reading and setting file permissions
  • Hiding files from normal listings
  • UNIX commands: cp, mkdir, mv, rm, rmdir, more, less, head, tail, file
Concepts:

The UNIX file system is a hierarchical system, usually described as being like a branching tree. This is the same type of file system you find in MS-DOS. Some people say that the people who wrote DOS stole the file system from UNIX.

UNIX treats directories like they were files, more so than DOS does. We can think of several types of files in UNIX:

  • ordinary files - contain data, text, or programs
  • directory files - contain other files (serve as organizers)
  • special files - reserved for special use by UNIX, usually Input/Output related

UNIX cares about case. Files (and anything else, for that matter) must be referred to by their correct names, including correct capitalization. UNIX allows filenames to have some punctuation in them, and allows them to be fairly long. Try to limit your filenames to about fourteen characters or less. Use descriptive filenames: cryptic ones are little help a week later.

Don't use special characters in filenames. Their uses will be discussed later. For now, stay away from these: *, ?, >, <, &, and |. It is possible to use a hyphen as the first character. Don't do it. (These are your author's warnings. It reminds me of the story of the thief and the sultan. The thief sold him a flying carpet that would take the sultan anywhere, as long as he heeded the warning not to think about a blue camel. So as soon as the sultan stepped on the carpet... Don't be like the sultan. Don't use dumb filenames.)

There are several commonly found directories in most UNIX systems. You should be aware of their usual contents:

  • bin - holds executable files; programs
  • dev - for device drivers
  • etc - for anything that doesn't fit elsewhere
  • lib - library storage; common code area
  • lost+found - place to look for files after a crash
  • tmp - a swap area for temporary files

The path to a file is the sequence of directories you must pass through to find that file. Since directories can hold files and other directories, path descriptions (pathnames) can get quite long. In UNIX, directory names in a path statement are separated by forward slashes, the opposite of DOS paths. A full pathname is the path to a file, starting from the root directory. A relative pathname is the path to file from some arbitrary starting point, like your home or current directory. To find out the path to your current directory, use the pwd command.

To create a directory under your current directory, use the mkdir command, followed by the name you want the new directory to have.

When you log into UNIX, you are placed into a HOME directory. The system variable HOME holds the pathname to that directory. (It is different for almost everybody.)

A single dot is UNIX shorthand for the current directory. Two dots are shorthand for the parent of the current directory.

UNIX files (and directories) have permissions assigned to them. There are three basic permissions:

  • read - you can see what is in a file
  • write - you can change what is in a file
  • execute - you can run a file, if it contains commands

UNIX also divides the world into three categories, with regard to files. First, you should know that users on a UNIX system are classified as belonging to groups. These groups are artificial, and are set up by the system administrator. A user on the system must fall into one of three categories with respect to any particular file:

  • user - person who owns the file, and probably wrote it
  • group - person in the same group as the user who owns it
  • other - everybody else in the universe

Think of permissions as being in three groups of three when seen on a list of files. Use the ls command with the modifier -al (In DOS we use forward slashes to show how to do a command. In UNIX we use hyphens.) The command might look like:

	Ls -al

It means you want the long form listing of all files in the current directory.

On the left side of the listing are the permissions. Directories have a d first, file permission lists start with a hyphen (They love that hyphen in UNIX. Just wait...) A file's permissions might look like this:

	-rwxr-xr--

This is three sets of three letters (or hyphens). The first set is for the User, and rwx means he/she can read, write and execute that file. The second set of three is for the Group the User belongs to. The combination r-x would means they can read it and execute it, but not write to it (the w is missing). The third set is for anybody else wandering across this file in the system. They got r-- in this example. That means they can read the file but not write to it or execute it (unless they know the trick we will see soon.)

There are several ways to set or change the permissions assigned to a file. Only the owner, a system administrator, a superuser, or a semi-talented hacker can do so. I usually use the chmod command with the octal number system described on page 52 of your text. You summarize the permissions down to three digits. Each digit represents the rights you grant one category above. Use this chart to decide what number to give each kind of person:

  • 0 - no rights
  • 1- execute only
  • 2 - write only
  • 3 - write and execute
  • 4 - read only
  • 5 - read and execute
  • 6 - read and write
  • 7 - all three: read, write and execute

Issue the command like this

	chmod 751 filename

This sets the owner's permissions to full (7), the group's permissions to read and execute (5), and common people's rights to execute only (1). You might want to do this to protect shell scripts you write.

To look at the contents of a text file, you can use the cat command followed by the filename. (It means to "concatenate" to the screen. Yeah, right... It made sense to the guy who wrote it thirty years ago. Maybe you had to experience the sixties.)

To copy files, use the cp command, followed by the path to the file to copy, followed by the path to the target directory.

To move a file from one place to another use the mv command instead of cp.

To erase files, type rm, followed by the list of files to erase, separated by spaces.

To erase a directory, use rmdir as the command.

To create a directory, use the mkdir command.

One way to create a file is to use cat and redirect output to a filename, like this:

	cat > newfile

Then whatever you type will be the contents of the new file. Close the file by pressing ctrl-d. (This is the UNIX end of file character.)

You can get information about the number of lines, words and letters in a text file with the command:

	wc filename

The output will be number of lines, number of words, number of letters, and the filename.

The diff command is used to report the differences between two text files.

The man command activates the UNIX online manual. The syntax is:

	man command

where command is the word you want to see the manual entry for.

The Ls command has several options. The -C option gives a multi-column output. The -l option gives you the detailed version (long form). Typically, setting first character of a filename (or directory name) as a period will hide it from the Ls command. You can force a display of such files with the -a option. (Sounds like the UNIX creators were trying to outdo each other, doesn't it?)

In order to view long files on your screen, you may want to use the more command. Simply type:

	more filename

and you see the file one screen at a time. Press the spacebar to go to the next screen.

Some systems have a less command, which is a newer version of more. It is called less because it can scroll backward (as well as forward.)

The head command shows the first 10 lines of a file, and can show more than ten. Say you want 15. The command is:

	head -n 15 filename

The tail command can be used with the same syntax, showing the last lines in a file.

The file command reports what type of object a filename represents.