fredrik.eriksson

Coffee and a keyboard

Unix files

In UNIX, all data is organized in files. An ordinary file is a memo, source code program or shell script. A shell script or program source code can be viewed or edited from your terminal. Other files contain binary data, like programs for the kernel; these files cannot be viewed or edited on the terminal.

Peripheral devices such as disk, tapes driver, printers and terminals are also assigned file names. Device files are considered to be special files. They have ‘special’ characteristics. Although input and output can be redirected to and from a special file, do not attempt to display the contents of a special file on your terminal.

File access mode are the protections that can be assigned to files. This protection can protect your files from unauthorized reading or writing. You can even protect your files from yourself.

There are three access modes for files:

  • r (read) read, examine, copy data in a file
  • w (write) modify, delete a file
  • x (execute) use the file as a command

Users with access to a file fall into one of three groups:

  • u (user) the file’s owner
  • g (group) users in the same group
  • o (other) everybody else

The first output field of the ls -s command is a ten character field. Characters two through ten describe the file access mode. A typical access mode listing look like:

rwxr-xr-x

Of the nine columns, the first three describe modes for the file’s owner, the next three for his group, and the last three for everyone else. Within each group of three, the first column describes read access mode, the second write, and the third execute. A letter in a column indeicates access granted, a dash (-) indicates access denied.

Using the previous example, the user has r (read), w (write), and x (execute) permissions. Members of the user’s logical group can read (r) or execute (x). Everyone else has read (r) and execute (x) permissions, too. The effect of these permissions is that the file’s owner is the only one who can modify the file; but everyone can examine, copy, or execute the file.

To change access modes on a file or directory, use the chmod command. $chmod Access can be expressed in either of two formats:

  1. With letters: {ugo},{+-=}{rwx}
  2. With numbers: {0-7}{0-7}{0-7}

Let’s look at the method of changing the file permissions with letters. The letters u, g, and o represent user, group, and others, respectively. The + sign means to add the permission and the – sign means to remove the permission. The = sign means to set the permissions as shown. Of course, r,w, and x are read, write, and execute.

If, for illustration purposes, we created a file named file1 that had the following permissions:

rw-rwxrwx

and you want to give yourself (user) execute permission and take away other’s (meaning groups and everyone else) write permissions.

$chmod u+x,g-w,o-w file1

Now if we use the ls -la command, and look at the file permission for file1, they will look like this:

rwxr-xr-x

If you want to set several protections at once use the equal sign. The following example will set the permissions for the user to read and execute.

chmod u=rx file

The second method of changing the permissions is to use the octal digits (0-7). The octal digits 0 through 7 are represented in binary in the following manner.

Octal Binary Permissions
0 000
1 001 –x
2 010 -w-
3 011 -wx
4 100 r–
5 101 r-x
6 110 rw-
7 111 rwx

Notice that every time a one digit (1) occurs in the binary number the corresponding permission are also set. Every time a zero (0) occurs, the corresponding permission is denied. So to change the file permissions in the previous example, this is the command to enter:

$chmod 755 file1

The first octal digit assigns user permissions of read, write and execute. The second digit assigns the group permission to read and execute. The last digit sets the other permission to read and execute to.

The ls command is used to display file names and their characteristics. Since file names are stored in directories, ls actually reads directory files. Executing ls with no flags or arguments simply lists the name of the files that exists in your current working directory. The initialization files will not be listed.

ls [options] [dir1[dirn]]

The -a flag will cause the hidden (initialization) and all other filenames to be displayed.

The -C flag causes the output to be change from single-colum to multi-column display.

The -F flag adds a character to the end of each display filename:

  • / indicates a directory
  • indicates the file is executable
  • black indicates a plain or ordinary file

The -l flag causes detailed information to be printed for files in the directory. This information includes:

  • file type
  • access modes
  • nuber of links
  • ownership
  • group affiliation
  • size in bytes
  • date and time of last modification
  • filename

Without a filename argument, ls displays information about the current working directory. The output is automatically sorted alphabetically by default.

The file command will classify files according to their contents.

file [options]

A few of the classifications that the file command displays are shown below. The results of using the file command are not always correct.

$ file t.ct.c: ASCII C program text

$ file unix-files.txtunix-files.txt: ASCII English text

The cat command displays the content of a file. The command cat is and abbreviation for catenate. This command will read each file in sequence and write it to the monitor screen.

cat [options] [file1[filen]]

If no filename is given, or the argument – is encountered, cat reads from standard input.

The pg command displays the content of a file one screen at a time. It allows the user to perform string searches and to scroll backwards.

pg [options] [file1[filen]]

A : (colon) prompt will appear on the last line. To get the help screen, simply press h followed by return.

The following UNIX command is useful for viewing the end of a file without having to display the entire file.

tail [options] [file1]

The tail command displays the last 10 lines of file by default. The tail command accepts a -N flag to display the last N lines.

The rm command will remove the entries for one or more files from a directory. If an entry was the last link to the file, the file will be destroyed. Removal of a file requires write permission to the directory itself, but neither read nor write permission to the file itself. The format for the rm command is:

rm [options]

BDD the evolutionary path of TDD

Behavior Driven Development (BDD) is (for me) the natural evolution of Test Driven Development (TDD), where instead of talking about test we talk about behavior and specification.

Test cases will never be tightly coupled to the code just the behavior. Making refactoring easier and safer. One important thing to remember is that BDD does not do anything revolutionary but instead evolutionary.

Meaning that BDD is TDD only easier to understand and easier to use correctly. So if you have done TDD correctly then you are already doing BDD. The text of the specification I feel should be expressed in some easy to parse format e.g. YAML this format should then generate stub code for the language the developer is developing in. Here is a short example of what the YAML code could look like:

An empty stack:- should be empty.- should not be empty after push.- should throw when pop't

An full stack:- should throw on push.- should not be full after pop.

I think this will be really cool Open-mouthed smile