Shell Commands

Shell Commands

The following commands are commonly used to get the most from the shell. Help for each of these commands is usually available through the man facility or the GNU info command.

Source (.)

The source command reads in and executes the lines of a shell script. The format of this command is

. file

where file is a valid filename that contains a Bourne shell script. The first line should contain a directive that points to the absolute location of the shell:

#!/bin/sh

Or, you can execute Bourne shell scripts by calling them with a new shell invocation, or calling them directly if the executable bit is set for the executing user. For example, the following three commands would each execute the script file myscript.sh:

$ . myscript.sh
$ sh myscript.sh
$ ./myscript.sh

However, only the source command (.) preserves any environment variable settings made in the script.

basename

The basename command strips a filename of its extension. The format of this command is

basename filename.ext

where filename.ext is a valid filename like mydata.dat. The basename command parses mydata.dat, and extracts mydata. Because file extensions are not mandatory in Solaris, this command is very useful for processing files copied from Windows or MS-DOS.

cat

The cat command prints out the contents of the file, without any special screen control features like scrolling backwards or forwards in a file. The format of this command is as follows:

cat filename

To display the groups database, for example, you could run this command:

$ cat /etc/group
root::0:root
other::1:
bin::2:root,bin,daemon
sys::3:root,bin,sys,adm
adm::4:root,adm,daemon
uucp::5:root,uucp
mail::6:root
tty::7:root,tty,adm
lp::8:root,lp,adm
nuucp::9:root,nuucp
staff::10:

cd

The cd command changes the current working directory to a new directory location, which you can specify in both absolute or relative terms. The format of this command is as follows:

cd directory

For example, if the current working directory is /usr/local, and you type the command

cd bin

the new working directory would be /usr/local/bin. However, if you type the command

cd /bin

the new working directory would be /bin. For interactive use, relative directory names are often used; however, scripts should always contain absolute directory references.

chgrp

The chgrp command modifies the default group membership of a file. The format of this command is

chgrp group file

where group is a valid group name, defined in the groups database (/etc/groups), and file is a valid filename. Because permissions can be assigned to individual users or groups of users, assigning a nondefault group membership can be useful for users who need to exchange data with members of different organizational units (for example, the web master who swaps configuration files with the database administrator, and also exchanges HTML files with web developers).

Tip 

Only the file owner or the superuser can modify the group membership of a file.

date

This command prints the current system date and time. The format of this command is as follows:

date

The default output for the command output is of this form:

Tuesday February  12 13:43:23 EST 2002

You can also modify the output format by using a number of parameters corresponding to days, months, hours, minutes, and so on. For example, the command

date '+Current Date: %d/%m/%y%nCurrent Time:%H:%M:%S'

produces the following output:

Current Date: 06/09/00
Current Time:13:45:43

grep

The grep command searches a file for a string (specified by string) and prints the line wherever a match is found. The format of this command is as follows:

grep string file

The grep command is very useful for interpreting log files, where you just want to display a line that contains a particular code (for example, a web server log file can be grepped for the string 404, which indicates a page not found).

head

The head command displays the first page of a file. The format of this command is as follows:

head filename

The head command is very useful for examining the first few lines of a very long file. For example, to display the first page of the name service switch configuration file (/etc/nsswitch.conf), you could use this command:

$ head /etc/nsswitch.conf
# /etc/nsswitch.nisplus:
# An example file that could be copied over to /etc/nsswitch.conf; it
# uses NIS+ (NIS Version 3) in conjunction with files.
# "hosts:" and "services:" in this file are used only if the
# /etc/netconfig file has a "-" for nametoaddr_libs of "inet" transports.
# the following two lines obviate the "+" entry in /etc/passwd and /etc/group.

less

The less command prints a file "file" on the screen, and it allows searching backwards and forwards through the file. The format of this command is as follows:

less filename

To scroll through the contents of the system log configuration file (/etc/syslog.conf), you would use the following command:

less /etc/syslog.conf
#ident  "@(#)syslog.conf        1.4     96/10/11 SMI"   /* SunOS 5.0 */
# Copyright (c) 1991-1993, by Sun Microsystems, Inc.
# syslog configuration file.
# This file is processed by m4 so be careful to quote (`') names
# that match m4 reserved words.  Also, within ifdef's, arguments
# containing commas must be quoted.
*.notice                                        @loghost
*.err;kern.notice;auth.notice                   /dev/console
*.err;kern.debug;daemon.notice;mail.crit;daemon.info    /var/adm/messages
*.alert;kern.err;daemon.err                     operator
*.alert                                         root

The less command has a number of commands that can be issued interactively. For example, to move forward one window, just type F, or to move back one window just type B. less also supports searching with the /pattern command.

ls

The ls command prints the names of files contained in the directory dir (by default, the contents of the current working directory are displayed). The format of the command is

ls directory

where directory is the name of the directory whose contents you wish to list. For example, to list the contents of the /var/adm directory, which contains a number of system logs, you could use this command:

$ ls /var/adm
aculog       log          messages.1   passwd      utmp     wtmp
ftpmessages  messages     messages.2   spellhist   utmpx    wtmpx
lastlog      messages.0   messages.3   sulog       vold.log

mkdir

The mkdir command makes new directory entries. The format of this command is as follows:

mkdir directory

For example, if the current working directory is /sbin, and you type the command

mkdir oracle

the new directory would be /sbin/oracle. However, if you type the command

mkdir /oracle

the new directory would be /oracle. For interactive use, relative directory names are often used; however, scripts should always contain absolute directory references.

more

The more command prints the contents of a file, like the less command, but just permits scrolling forward through a file. The format of this command is as follows:

more filename

To scroll through the contents of the disk device configuration file (/etc/format.dat), you would use the following command:

more /etc/format.dat
#pragma ident   "@(#)format.dat 1.21    98/01/24 SMI"
# Copyright (c) 1991,1998 by Sun Microsystems, Inc.
# All rights reserved.
# Data file for the 'format' program.  This file defines the known
# disks, disk types, and partition maps.
# This is the list of supported disks for the Emulex MD21 controller.
disk_type = "Micropolis 1355" \
        : ctlr = MD21 \
        : ncyl = 1018 : acyl = 2 : pcyl = 1024 : nhead = 8 : nsect = 34 \
        : rpm = 3600 : bpt = 20832

The more command has a number of commands that can be issued interactively. For example, to move forward one window, just press the SPACEBAR, or to move forward one line, just press ENTER. more also supports searching with the /pattern command.

pwd

The pwd command prints the current working directory in absolute terms. The format of the command is as follows:

pwd

For example, if you change the directory to /etc and issue the pwd command, you would see the following result:

$ cd /etc
 $ pwd
/etc

rmdir

The rmdir command deletes a directory. However, the directory concerned must be empty for the rmdir command to be successful. The format of this command is as follows:

rmdir directory

For example, if the current working directory is /usr/local, and you want to remove the directory oldstuff, you would use this command:

rmdir oldstuff

However, you could use the command

rmdir /usr/local/oldstuff

to remove the directory as well. For interactive use, relative directory names are often used; however, scripts should always contain absolute directory references.

tail

The tail command displays the last page of a file. The format of this command is as follows:

tail filename

The tail command is very useful for examining the last few lines of a very long file. For example, to display the first page of a web log file (/usr/local/apache/logs/access_log), you could use this command:

$ tail /usr/local/apache/logs/access_log
192.168.205.238 - - [12/Feb/2002:09:35:59 +1000]
  "GET /images/picture10.gif HTTP/1.1" 200 53
192.168.205.238 - - [12/Feb/2002:09:35:59 +1000]

  "GET /images/ picture1.gif HTTP/1.1" 200 712
192.168.205.238 - - [12/Feb/2002:09:35:59 +1000]

  "GET /images/ picture5.gif HTTP/1.1" 200 7090
192.168.205.238 - - [12/Feb/2002:09:35:59 +1000]

  "GET /images/ picture66.gif HTTP/1.1" 200 997
192.168.205.238 - - [12/Feb/2002:09:35:59 +1000]

  "GET /images/ picture49.gif HTTP/1.1" 200 2386
192.168.205.238 - - [12/Feb/2002:09:36:09 +1000]

  "GET /servlet/SimpleServlet HTTP/1.1" 200 10497

The tail command also has an option that allows you to continuously monitor all new entries made to a file. This is very useful for monitoring a live service such as Apache, where you need to observe any error made in real time. The format for this command is as follows:

tail –f filename


Part I: Solaris 9 Operating Environment, Exam I