Odds and Ends
This section includes a few tidbits that I wasn't sure where to put.
You can get the shell
to help you debug your script. If you place set -x in
your script, each command with its corresponding arguments is printed as it is
executed. If you want to just show a section of your script, include the set -x
before that section, then another set +x at the end. The set +x turns off the
If you want, you can capture output into another file, without having it go
to the screen. This is done using the fact that output generated as a result of
the set -x is going to stderr
and not stdout.
If you redirect stdout somewhere,
the output from set -x still goes to the screen. On the other hand, if you
still goes to your screen. To redirect sterr to a file
start, the script like this:
This says to send file descriptor
2 (stderr) to the file /tmp/output.
To create a directory that is several levels deep, you do not have to change
directories to the parent and then run mkdir from there. The mkdir command takes
as an argument
the path name of the directory you want to create. It doesn't
matter if it is a subdirectory,
or absolute path. The system will
do that for you. Also, if you want to create several levels of directories, you
don't have to make each parent directory
before you make the subdirectories.
Instead, you can use the -p option to mkdir, which will automatically create all
the necessary directories.
For example, imagine that we want to create the subdirectory
./letters/personal/john, but the subdirectory
letters does not exist yet. This also means that the subdirectory
personal doesn't exist, either. If we run mkdir like this:
then the system will create ./letters, then ./letters/personal, and then
Assume that you want to remove a file that has multiple links; for example,
assume that ls, lc, lx, lf, etc., are links to the same file. The system keeps
track of how many names reference the file through the link count
(more on this
concept later). Such links are called hard links. If you remove one of them, the
file still exists as there are other names that reference it. Only when we
remove the last link (and with that, the link count
goes to zero) will the file
There is also the issue of symbolic links. A symbolic link
(also called a
soft link) is nothing more than a path name that points to some other file, or
even to some directory. It is not until the link is accessed that the path is
translated into the "real" file. This has some interesting effects. For example,
if we create a link like this
you would see the symbolic link
as something like this:
drw-r--r-- 1 jimmo support 29 Sep 15 10:06 letter.john-> /home/jimmo/letter.john
Then,the file /home/jimmo/text/letter.john is a symbolic link
to /home/jimmo/letter.john. Note that the link count
on /home/jimmo/letter.john doesn't change, because the system sees these as two separate files. It is easier to think of the file /home/jimmo/text/letter.john as a text
file that contains
the path to /home/jimmo/letter.john. If we remove /home/jimmo/letter.john,
/home/jimmo/text/letter.john will still exist. However, it will point to
something that doesn't exist. Even if there are other hard links that point to
the same file like /home/jimmo/letter.john, that doesn't matter. The symbolic
link, /home/jimmo/text/letter.john, points to the path
/home/jimmo/letter.john. Because the path no longer exists, the file can no
longer be accessed via the symbolic link.
It is also possible for you to create a symbolic link
to a file that does not exist, as the system does not check
until you access the file.
Another important aspect is that symbolic links can extend across file systems.
A regular or hard link
is nothing more than a different name for the same
physical file and used the same inode
number. Therefore it must be on the same
filesystem. Symbolic links contain a path, so the destination can be on another
filesystem (and in some cases on another machine).
For more on inodes,
see the section on filesystems.
The file command can be
used to tell you the type of file. With DOS
and Windows, it's fairly obvious by
looking at the file's extension to determine the files type. For example, files
ending in .exe are executables (programs), files ending in .txt are text
files, and files ending in .doc are documents (usually from some word processor).
However, a program in UNIX
can just as easily have the ending .doc or .exe, or
no ending at all.
The file command uses the file /etc/magic to make an
assumption about the contents of a file. The file command reads the header
(first part of the file) and uses the information in /etc/magic to make its
guess. Executables of a specific type (a.out, ELF) all have the same basic
format, so file can easily recognize them. However, there are certain
similarities between C source code, shell scripts, and even text
files that could confuse file.
For a list of some of the more commonly used commands, take a look