This chapter offers some practical commands that can be used with the file system knowledge you gained from previous chapters. As noted in the objectives on page 157 (and those listed above), you should be able to manipulate files from a UNIX/Linux command line by the end of this lesson.
In the last chapter, the text wanted to cluster all files into
two types: ASCII files and binary files. The concept is expanded a bit
in chapter 4. We are told that text files and binary files are both
classified by the file system as regular
files, which is what the initial dash
in their permissions lists means when viewing the output of an ls -l
command. The text informs us that some of the files it mentioned in the
last chapter, device special files,
are further categorized as character
special files and block
special files, each of which will
have different type tags.
The text goes on for a couple of pages describing ASCII files, database file structures, and other concepts that are not useful to the objectives of the chapter. Let's move on to the Processing Files section that begins on page 160.
In this section, the text introduces a concept that relates to
several programming languages and to operating system environments. Streams are communication
channels between a program (or and OS) and its hardware/software
environment. They exist for multiple programming languages and for the
operating systems we are talking about. We generally care about three
C, UNIX, and Linux treat all these streams like files. They send data that is meant for a stream into it the same way they send data to any other file. The operating system you are running defines what the streams connect to.
The text explains that we can redirect the output of a process to a file (including the three streams) by using greater than symbols. Most processes take their input from a default location, such as stdin, but we can tell a process to take input from a file using less than symbols.
The text describes using the less than sign to use a file as an input source to a command. This is more useful in testing commands and scripts that in real life. You will practice this briefly in Project 4-1.
Creating a file is most properly done by using a process or application meant to do so. However, if you really want to create a new, empty file, the text offers two ways to do so on page 162:
Deleting a file was mentioned in the last chapter. A summary of the rm (remove) command appears on page 163. The simple notation is just rm filename.
You already know that the rmdir command can be used to delete an empty directory. The text explains that the rm command may be used to delete a directory, but it will not work unless the command is issued as rm -r directoryname.
Without the -r switch, UNIX and Linux typically refuse to delete a
directory through the rm command, empty or not. If you use rm -r, you
are telling the system to delete all the files (and subdirectories, and
their files, if they exist) inside the directory first. This makes rm
-r a very powerful command. Use it with caution.
The text reviews the cp (copy) command on pages 164 and 165. It reminds us, if we already know, that directories are not allowed to contain two (or more) files with the same name. A copy sent to the same folder must have a new name. Several variations are mentioned:
On page 166, the text discusses the mv (move) command. Think of a move as being like a cut and paste operation, taking files from one folder and placing them in another. The syntax is like the cp command described above.
We have used the ls command to show us information about files
whose location we know. The find command is used to locate files. The
text explains the syntax like this:
The text describes another use for the cat command on pages 167 and 168, It proposes a situation in which you want to combine two text files into one. You could do this with the >> redirection operator, as you should remember, but the text suggests another way:
The paste command is described as a utility that reads two files, line by line. It combines the first lines from each file, the second lines from each file, and so on, putting the resulting longer lines into a new file. As you can see from the example in the text, this would be best done with two files having the same number of lines, each line having information that is meant to be viewed in a column. This is one way to create a file that resembles a table in a database.
The cut command is explained as working best on tables of data. It can be thought of as a way to undo the creation of the table that you made in the previous example. As with the command above, it is a little hard to imagine wanting to do this kind of thing. You may want to think of it as the ancestor of similar functions found in actual database management programs. This is a way to do database reporting if you have data files but don't have the database software.
The sort command is
used, as you might imagine, to sort the contents of a file in some
meaningful way: alphabetic, numeric, or the reverse of either of those
orders. Its options provide more choices about what data elements to
sort by, assuming that we are dealing with a table of data.
The fourth objective for this lesson is about scripts,
which I have mentioned to you several times. Most of our recent work
has been about operations from the command line, using commands found
in most shells. A shell script is a file in which you have saved one or more commands that you would like to run regularly, frequently, or reliably.
If you find there is a sequence of commands that are useful to you, you
may want to create a shell script holding those commands. If you use
commands that are intricate and hard to remember, you may want a script
for those commands so you can run them the same way each time, without
having to worry about syntax errors. A script file would be useful
enough if you could only use it for reference. It is more useful than
that, however. Once a script is finished, the commands stored in it can
be run in sequence simply by entering the name of the script as a
command. We will hit some material about this in chapter 6. The text
promises some practice in project 4-15, however, that project requires
that you do several other projects first to create the data files it
uses. A little much for this chapter. The lnk above will take you to a
series of short lessons about creating, granting rights to, and using
shell scripts. Take a look at them if you have time.
Let's skip ahead to the introduction to awk on page 176 to finish this chapter. Awk
is more than a command. It is a programming language that can be used
in UNIX and Linux inside script files. The text provides a two page
introduction to several features, and mentions that your version of
Linux may come with gawk, an
improved version of awk. For this lesson, I have placed a
handout/download about awk in the Week 4 folder on Blackboard. The
lessons in this file were developed by an instructor in New Zealand,
Professor Brian Brown. We are grateful for his work.