Sams Teach Yourself Shell Programming in 24 Hours
(Publisher: Macmillan Computer Publishing)
Author(s): Sriranga Veeraraghavan
ISBN: 0672314819
Publication Date: 01/01/99

Previous Table of Contents Next


Common Errors

The most common errors are

  Specifying more than one argument
  Trying to cd to a file
  Trying to cd to a directory that does not exist

An example of the first case is

$ cd /home /tmp /var
$ pwd
/home

As you can see, cd uses only its first argument. The other arguments are ignored. Sometimes in shell programming, this becomes an issue. When you issue a cd command in a shell script, make sure that you end up in the directory you intended to reach.

An example of the second case is

$ pwd
/home/ranga
$ cd docs/ch5.doc
cd: docs/ch5.doc: Not a directory
$ pwd
/home/ranga

Here, you tried to change to a location that was not a directory, and cd reported an error. If this error occurs, the working directory does not change. The final pwd command in this example illustrates this.

An example of the third case is

$ pwd
/home/ranga
$ cd final_exam_answers
cd: final_exam_answers: No such file or directory
$ pwd
/home/ranga

Here, I tried to change into the directory final_exam_answers, but because this directory did not exist, cd reported an error. The final pwd command shows that the working directory did not change.

The problem in this last example occurs because none of my professors were kind enough to make a copy of the directory final_exam_answers for me.

Listing Files and Directories

In Chapter 3, “Working with Files,” you looked at using the ls command to list the files in the current directory. Now look at using the ls command to list the files in any directory.

Listing Directories

To list the files in a directory you can use the following syntax:

ls directory

Here, directory is the absolute or relative pathname of the directory whose contents you want listed.

For example, both of the following commands list the contents of the directory /usr/local (assuming the working directory is /home/ranga):

$ ls /usr/local
$ ls ../../usr/local

On my system the listing looks like

X11           bin           gimp          jikes         sbin
ace           doc           include       lib           share
atalk         etc           info          man           turboj-1.1.0

The listing on your system might look quite different.

You can use any of the options you covered in Chapter 3 to change the output. For example, the command

$ ls -aF /usr/local

produces the following output

./             atalk/         gimp/          lib/           turboj-1.1.0/
../            bin/           include/       man/
X11/           doc/           info/          sbin/
ace/           etc/           jikes/         share/

You can specify more than one directory as an argument. For example

$ ls /home /usr/local

produces the following output on my system:

/home:
amma    ftp     httpd   ranga   vathsa

/usr/local:
X11           bin           gimp          jikes         sbin
ace           doc           include       lib           share
atalk         etc           info          man           turboj-1.1.0

A blank line separates the contents of each directory.

Listing Files

If you specify the name of a file instead of a directory, ls lists only that one file. For example

$ ls .profile
.profile

You can intermix files and directories as arguments to ls:

$ ls .profile docs/ /usr/local /bin/sh

This produces a listing of the specified files and the contents of the directories.

If you don’t want the contents of the directory listed, specify the -d option to ls. This forces ls to display only the name of the directory, not its contents:

$ ls -d /home/ranga
/home/ranga

You can combine the -d option with any of the other ls options you have covered. An example of this is

$ ls -aFd /usr/local /home/ranga /bin/sh
/bin/sh*      /home/ranga/  /usr/local/

Common Errors

If the file or directory you specify does not exist, ls reports an error. For example

$ ls tomorrows_stock_prices.txt
tomorrows_stock_prices.txt: No such file or directory

If you specify several arguments instead of one, ls reports errors only for those files or directories that do not exist. It correctly lists the others. For example

$ ls tomorrows_stock_prices.txt /usr/local .profile

produces an error message

tomorrows_stock_prices.txt: No such file or directory
/usr/local:
X11           bin           gimp          jikes         sbin
ace           doc           include       lib           share
atalk         etc           info          man           turboj-1.1.0

.profile

Manipulating Directories

Now that you have covered using directories, look at manipulating them. The most common manipulations are

  Creating directories
  Copying directories
  Moving directories
  Removing directories

Creating Directories

You can create directories with the mkdir command. Its syntax is

mkdir directory

Here, directory is the absolute or relative pathname of the directory you want to create. For example, the command

$ mkdir hw1

creates the directory hw1 in the current directory. Here is another example:

$ mkdir /tmp/test-dir

This command creates the directory test-dir in the /tmp directory. The mkdir command produces no output if it successfully creates the requested directory.

If you give more than one directory on the command line, mkdir creates each of the directories. For example

$ mkdir docs pub

creates the directories docs and pub under the current directory.


Previous Table of Contents Next