Commanding the Shell - Linux Desktops - Linux All-in-One For Dummies, 5th Edition (2014)

Linux All-in-One For Dummies, 5th Edition (2014)

Book II. Linux Desktops

Chapter 3. Commanding the Shell

In This Chapter

arrow Opening a terminal window or a virtual console

arrow Discovering the bash shell

arrow Using Linux commands

arrow Writing a shell script

Sometimes things just don’t work. What do you do if the GUI desktop stops responding to your mouse clicks? What if the GUI doesn’t start at all? You can still tell your Linux system what to do, but you have to do it by typing commands into a text screen. In these situations, you work with the shell — the Linux command interpreter. This chapter introduces the bash shell, the default shell in most Linux distributions.

After you figure out how to work with the shell, you may even begin to like the simplicity and power of the Linux commands. And then, even if you’re a GUI aficionado, someday soon you may find yourself firing up a terminal window and making the system sing and dance with two- or three-letter commands strung together by strange punctuation characters. (Hey, I can dream, can’t I?)

Opening Terminal Windows and Virtual Consoles

First things first. If you’re working in a GUI desktop, such as GNOME or KDE, where do you type commands for the shell? Good question.

The easiest way to get to the shell is to open a terminal (also called console) window. The GNOME and KDE GUIs in most distributions include an icon (or a Main Menu option) to open a terminal window. Click that icon or choose the menu option to get a terminal window. If you don’t see such an icon in GNOME, choose Applications⇒Accessories⇒Terminal. Now you can type commands to your heart’s content.

If, for some reason, the GUI seems to be hung (you click and type but nothing happens), you can turn to the virtual consoles. (The physical console is the monitor-and-keyboard combination.) The idea of virtual consoles is to give you the ability to switch among several text consoles even though you have only one physical console. Whether you’re running a GUI or not, you can then use different text consoles to type different commands.

To get to the first virtual console from the GNOME or KDE desktop, press Ctrl+Alt+F1. Press Ctrl+Alt+F2 for the second virtual console, and so on. Each of these virtual consoles is a text screen where you can log in and type Linux commands to perform various tasks. When you’re finished, type exit to log out.

tip.eps You can use up to six virtual consoles. In most distributions, the seventh one is used for the GUI desktop. To get back to the GUI desktop, press Ctrl+Alt+F7.

Using the bash Shell

If you’ve used MS-DOS, you may be familiar with COMMAND.COM, the DOS command interpreter. That program displays the infamous C:\> prompt. In Windows, you can see this prompt if you open a command window. (To open a command window in Microsoft Windows, choose Start⇒Run, type command in the text box, and then click OK.)

Linux comes with a command interpreter that resembles COMMAND.COM in DOS, but it can do a whole lot more. The Linux command interpreter is called a shell.

The default shell in many Linux distributions is bash. When you open a terminal window or log in at a text console, the bash shell is what prompts you for commands. Then, when you type a command, the shell executes your command.

technicalstuff.eps Just as there are multiple GUIs (GNOME or KDE) for Linux, you have a choice of shells besides bash. For example, some people prefer the C shell. You can easily change your default shell by using the chsh command.

In addition to the standard Linux commands, bash can execute any computer program. So you can type the name of an application (the name is usually more cryptic than what you see in GNOME or KDE menus) at the shell prompt, and the shell starts that application.

Understanding the syntax of shell commands

Because a shell interprets what you type, knowing how the shell processes the text you enter is important. All shell commands have the following general format. (Some commands have no options.)

command [option1] [option2] . . . [optionN]

Issuing such a command is a process commonly referred to as a command line. On a command line, you enter a command, followed by zero or more options (or arguments). These strings of options — the command-line options (or command-line arguments) — modify the way the command works so that you can get it to do specific tasks.

The shell uses a blank space or a tab to distinguish between the command and options. This means you must use a space or a tab to separate the command from the options and the options from one another.

If an option contains spaces, you put that option inside quotation marks. For example, to search for my name in the password file, I enter the following grep command (grep is used for searching for text in files):

grep "Emmett Dulaney" /etc/passwd

When grep prints the line with my name, it looks like this:

edulaney:x:1000:100:Emmett Dulaney:/home/edulaney:/bin/bash

If you create a user account with your username, type the grep command with your username as an argument to look for that username in the /etc/passwd file.

technicalstuff.eps In the output from the grep command, you can see the name of the shell (/bin/bash) following the last colon (:). Because the bash shell is an executable file, it resides in the /bin directory; you must provide the full path to it.

The number of command-line options and their format depend on the actual command. Typically, these options look like -X, where X is a single character. For example, you can use the -l option with the ls command. The command lists the contents of a directory, and the option provides additional details. Here is a result of typing ls -l in a user’s home directory:

total 0
drwxr-xr-x 2 edulaney users 48 2014-09-08 21:11 bin
drwx------ 2 edulaney users 320 2014-09-08 21:16 Desktop
drwx------ 2 edulaney users 80 2014-09-08 21:11 Documents
drwxr-xr-x 2 edulaney users 80 2014-09-08 21:11 public_html
drwxr-xr-x 2 edulaney users 464 2014-09-17 18:21 sdump

If a command is too long to fit on a single line, you can press the backslash key (\) followed by Enter. Then continue typing the command on the next line. For example, type the following command. (Press Enter after each line.)

cat \
/etc/passwd

The cat command then displays the contents of the /etc/passwd file.

You can concatenate (that is, string together) several shorter commands on a single line by separating the commands by semicolons (;). For example, the following command

cd; ls -l; pwd

changes the current directory to your home directory, lists the contents of that directory, and then shows the name of that directory.

Combining shell commands

You can combine simple shell commands to create a more sophisticated command. For example, suppose that you want to find out whether a device file named sbpcd resides in your system’s /dev directory because some documentation says you need that device file for your CD-ROM drive. You can use the ls /dev command to get a directory listing of the /dev directory and then browse through it to see whether that listing contains sbpcd.

Unfortunately, the /dev directory has a great many entries, so you may find it hard to find any item that has sbpcd in its name. You can, however, combine the ls command with grep and come up with a command line that does exactly what you want. Here’s that command line:

ls /dev | grep sbpcd

The shell sends the output of the ls command (the directory listing) to the grep command, which searches for the string sbpcd. That vertical bar (|) is known as a pipe because it acts as a conduit (think of a water pipe) between the two programs — the output of the first command is fed into the input of the second one.

Controlling command input and output

Most Linux commands have a common feature — they always read from the standard input (usually, the keyboard) and write to the standard output (usually, the screen). Error messages are sent to the standard error (usually to the screen as well). These three devices often are referred to as stdin, stdout, and stderr.

You can make a command get its input from a file and then send its output to another file. Just so you know, the highfalutin term for this feature is input and output redirection or I/O redirection.

Table 3-1 shows the syntax of common I/O redirection commands, and the next few sections explain how to use some of these commands.

Table 3-1 Common Standard I/O Redirections

Task

Command Syntax

Send stdout to a file

command > file

Send stderr to file

command 2> file

Send stdout and stderr to file

command > file 2>&1

Read stdin from a file

command < file

Read stdin from file.in and send stdout to file.out

command < file.in > file.out

Append stdout to the end of a file

command >> file

Append stderr to the end of a file

command 2>> file

Append stdout and stderr to the end of a file

command >> file 2>&1

Read stdin from the keyboard until the character c

command <<c

Pipe stdout to command2

command | command2

Pipe stdout and stderr to command2

command 2>&1 | command2

Getting command input from a file

If you want a command to get its instructions by reading from a file, you can redirect the standard input to come from that file instead of from the keyboard. For example, type the following command:

sort < /etc/passwd

This command displays a sorted list of the lines in the /etc/passwd file. In this case, the less-than sign (<) redirects stdin so that the sort command reads its input from the /etc/passwd file.

Saving command output in a file

To save the output of a command in a file, redirect the standard output to a file. For example, type cd to change to your home directory, and then type the following command:

grep typedef /usr/include/* > typedef.out

This command searches through all files in the /usr/include directory for the occurrence of the text typedef — and then saves the output in a file called typedef.out. The greater-than sign (>) redirects stdout to a file. This command also illustrates another feature of bash: When you use an asterisk (*), bash replaces the asterisk with a list of all filenames in the specified directory. Thus /usr/include/* means all the files in the /usr/include directory.

tip.eps If you want to append a command’s output to the end of an existing file instead of saving the output in a new file, use two greater-than signs (>>) like this:

command >> filename

tip.eps Another interesting use of sending stdout to a file is the use of the cat command to quickly prepare small text files. For example, suppose that you want to create a new text file to store lines of text you type until you type ZZ and press Enter. Here is how you can accomplish that task:

cat <<ZZ > input.txt

After you type this command, you can keep typing lines and then type ZZ on a line when you are finished. Everything you type is saved in the file input.txt.

Saving error messages in a file

Sometimes you type a command, and it generates a lot of error messages that scroll by so fast you can’t tell what’s going on. One way to see all the error messages is to save them in a file so that you can see what the heck happened. You can do that by redirecting stderr to a file.

For example, type the following command:

find / -name COPYING -print 2> finderr

This command looks through the file system for files named COPYING and saves all the error messages (if there are any) in the finderr file. The number 2 followed by the greater-than sign (2>) redirects stderr to a file.

tip.eps If you want to simply discard the error messages instead of saving them in a file, use /dev/null as the filename, like this:

find / -name COPYING -print 2> /dev/null

technicalstuff.eps That /dev/null is a special file — often called the bit bucket and sometimes glorified as the Great Bit Bucket in the Sky — that simply discards whatever it receives. So now you know what they mean when you hear phrases, such as “Your mail probably ended up in the bit bucket.”

Typing less with automatic command completion

Many commands take a filename as an argument. To view the contents of the /etc/modprobe.conf text file, for example, type the following command:

cat /etc/modprobe.conf

The cat command displays the /etc/modprobe.conf file. For any command that takes a filename as an argument, you can use a bash feature to avoid having to type the entire filename. All you have to type is enough characters to uniquely identify the file in its directory.

To see an example, type cat /etc/mod but don’t press Enter; press Tab instead. bash automatically completes the filename, so the command becomes cat /etc/modprobe.conf. Now press Enter to run the command.

tip.eps Whenever you type a filename, press Tab after the first few characters of the filename. bash probably can complete the filename so that you don’t have to type the entire name. If you don’t enter enough characters to uniquely identify the file, bash beeps. Just type a few more characters and press Tab again.

Going wild with asterisks and question marks

You can avoid typing long filenames another way. (After all, making less work for users is why we use computers, isn’t it?)

This particular trick involves using the asterisk (*) and question mark (?). These special characters are wildcards because they match zero or more characters in a line of text.

If you know MS-DOS, you may have used commands such as COPY *.* A: to copy all files from the current directory to the A: drive. bash accepts similar wildcards in filenames. As you expect, bash provides many more wildcard options than the MS-DOS command interpreter does. Of course, newer computers (particularly notebook computers and especially netbooks) don't have A and B drives anymore. That deprives an entire generation of the fun of trying to copy a large file onto floppy disks!

You can use three types of wildcards in bash:

· Asterisk (*): Matches zero or more characters in a filename. That means * denotes all files in a directory.

· Question mark (?): Matches any single character. If you type test?, that matches any five-character text that begins with test.

· Set of characters in brackets: Matches any single character from that set. The string [aB], for example, matches only files named a or B, The string [aB]*, though, matches any filename that starts with a or B.

Wildcards are handy when you want to do something to many files. For example, to copy all the files from the /media/cdrom directory to the current directory, type the following:

cp /media/cdrom/* .

bash replaces the wildcard character * with the names of all the files in the /media/cdrom directory. The period at the end of the command represents the current directory.

You can use the asterisk with other parts of a filename to select a more specific group of files. Suppose you want to use the grep command to search for the text typedef struct in all files of the /usr/include directory that meet the following criteria:

· The filename starts with s

· The filename ends with .h

The wildcard specification s*.h denotes all filenames that meet these criteria. Thus you can perform the search with the following command:

grep "typedef struct" /usr/include/s*.h

The string contains a space that you want the grep command to find, so you have to enclose that string in quotation marks. That way, bash doesn’t try to interpret each word in that text as a separate command-line argument.

The question mark (?) matches a single character. Suppose you have four files — image1.pcx, image2.pcx, image3.pcx, and image4.pcx — in the current directory. To copy these files to the /media/floppy directory, use the following command:

cp image?.pcx /media/floppy

bash replaces the single question mark with any single character and copies the four files to /media.

The third wildcard format — [ … ] — matches a single character from a specific set of characters enclosed in square brackets. You may want to combine this format with other wildcards to narrow the matching filenames to a smaller set. To see a list of all filenames in the/etc/X11/xdm directory that start with x or X, type the following command:

ls /etc/X11/xdm/[xX]*

Repeating previously typed commands

To make repeating long commands easy for you, bash stores up to 500 old commands as part of a command history (basically just a list of old commands). To see the command history, type history. bash displays a numbered list of the old commands, including those that you entered during previous logins.

If the command list is too long, you can limit the number of old commands that you want to see. For example, to see only the 10 most recent commands, type this command:

history 10

To repeat a command from the list that the history command shows, simply type an exclamation point (!), followed by that command’s number. To repeat command number 3, type !3.

You can repeat a command without knowing its command number. Suppose you typed more /usr/lib/X11/xdm/xdm-config a few minutes ago and now you want to look at that file again. To repeat the previous more command, type the following:

!more

Often you may want to repeat the last command that you just typed, perhaps with a slight change. For example, you may have displayed the contents of the directory by using the ls -l command. To repeat that command, type two exclamation points as follows:

!!

Sometimes you may want to repeat the previous command but add extra arguments to it. Suppose that ls -l shows too many files. Simply repeat that command but pipe the output through the more command as follows:

!! | more

bash replaces the two exclamation points with the previous command and then appends | more to that command.

tip.eps Here’s the easiest way to recall previous commands: Just press the up-arrow key, and bash keeps going backward through the history of commands you previously typed. To move forward in the command history, press the down-arrow key.

Discovering and Using Linux Commands

You type Linux commands at the shell prompt. By Linux commands, I mean some of the commands that the bash shell understands as well as the command-line utilities that come with Linux. In this section, I introduce you to a few major categories of Linux commands.

I can’t cover every single Linux command in this chapter, but I want to give you a feel for the breadth of the commands by showing you common Linux commands. Table 3-2 lists common Linux commands by category. Before you start memorizing any Linux commands, browse this table.

Table 3-2 Essential Linux Commands

Command Name

Action

Help and Abbreviations

apropos

Find online manual pages for a specified keyword.

info

Display online help information about a specified command.

man

Display online help information.

whatis

Search for complete words only and find the online manual pages.

alias

Define an abbreviation for a long command.

type

Show the type and location of a command.

unalias

Delete an abbreviation defined using alias.

Managing Files and Directories

cd

Change the current directory.

chmod

Change file permissions.

chown

Change the file owner and group.

cp

Copy files.

ln

Create symbolic links to files and directories.

ls

Display the contents of a directory.

mkdir

Create a directory.

mv

Rename a file and move the file from one directory to another.

rm

Delete files.

rmdir

Delete directories.

pwd

Display the current directory.

touch

Update a file’s timestamp.

Finding Files

find

Find files based on specified criteria, such as name and size.

locate

Find files using a periodically updated filename database. (The database is created by the updatedb program.)

whereis

Find files based in the typical directories where executable (also known as binary) files are located.

which

Find files in the directories listed in the PATH environment variable.

Processing Files

cat

Display a file on standard output (can be used to concatenate several files into one big file).

cut

Extract specified sections from each line of text in a file.

dd

Copy blocks of data from one file to another (used to copy data from devices).

diff

Compare two text files and find any differences.

expand

Convert all tabs to spaces.

file

Display the type of data in a file.

fold

Wrap each line of text to fit a specified width.

grep

Search for regular expressions in a text file.

less

Display a text file one page at a time (go backward by pressing b).

lpr

Print files.

more

Display a text file, one page at a time (goes forward only).

nl

Number all nonblank lines in a text file and print the lines to standard output.

paste

Concatenate corresponding lines from several files.

patch

Update a text file using the differences between the original and revised copy of the file.

sed

Copy a file to standard output while applying specified editing commands.

sort

Sort lines in a text file.

split

Break up a file into several smaller files with specified size.

tac

Reverse a file (last line first and so on).

tail

Display the last few lines of a file.

tr

Substitute one group of characters for another throughout a file.

uniq

Eliminate duplicate lines from a text file.

wc

Count the number of lines, words, and characters in a text file.

zcat

Display a compressed file (after decompressing).

zless

Display a compressed file one page at a time (go backward by pressing b).

zmore

Display a compressed file one page at a time.

Archiving and Compressing Files

compress

Compress files.

cpio

Copy files to and from an archive.

gunzip

Decompress files compressed with GNU Zip (gzip).

gzip

Compress files using GNU Zip.

tar

Create an archive of files in one or more directories (originally meant for archiving on tape).

uncompress

Decompress files compressed with compress.

Managing Files

bg

Run an interrupted process in the background.

fg

Run a process in the foreground.

free

Display the amount of free and used memory in the system.

halt

Shut down Linux and halt the computer.

kill

Send a signal to a process (usually used to terminate a process).

ldd

Display the shared libraries needed to run a program.

nice

Run a process with a lower priority (referred to as nice mode).

ps

Display a list of currently running processes.

printenv

Display the current environment variables.

pstree

Show parent-child process relationships.

reboot

Stop Linux and then restart the computer.

shutdown

Shut down Linux.

top

Display a list of most processor- and memory-intensive processes.

uname

Display information about the system and the Linux kernel.

Managing Users

chsh

Change the shell (command interpreter).

groups

Print the list of groups that include a specified user.

id

Display the user and group ID for a specified username.

passwd

Change the password.

su

Start a new shell as another user (the other user is assumed to be root when the command is invoked without any argument).

Managing the File System

df

Summarize free and available space in all mounted storage devices.

du

Display disk usage information.

fdformat

Format a floppy disk.

fdisk

Partition a hard drive.

fsck

Check and repair a file system.

mkfs

Create a new file system.

mknod

Create a device file.

mkswap

Create a swap space for Linux in a file or a hard drive partition.

mount

Mount a device (for example, the CD-ROM) on a directory in the file system.

swapoff

Deactivate a swap space.

swapon

Activate a swap space.

sync

Write buffered (saved in memory) data to files.

tty

Display the device name for the current terminal.

umount

Unmount a device from the file system.

Dates and Times

cal

Display a calendar for a specified month or year.

date

Display the current date and time or set a new date and time.

Becoming root (superuser)

When you want to do anything that requires a high privilege level (for example, administering your system), you have to become root. Normally, you log in as a regular user with your everyday username. When you need the privileges of the superuser, though, use the following command to become root:

su -

That’s su followed by a space and the minus sign (or hyphen). The shell then prompts you for the root password. Type the password and press Enter.

After you’ve finished with whatever you want to do as root (and you have the privilege to do anything as root), type exit to return to your normal username.

technicalstuff.eps Instead of becoming root by using the su - command, you can also type sudo followed by the command that you want to run as root. In Ubuntu, you must use the sudo command because you don’t get to set up a root user when you install Ubuntu. If you’re listed as an authorized user in the /etc/sudoers file, sudo executes the command as if you were logged in as root. Type man sudoers to read more about the /etc/sudoers file.

Managing processes

Every time the shell executes a command that you type, it starts a process. The shell itself is a process, as are any scripts or programs that the shell runs.

Use the ps ax command to see a list of processes. When you type ps ax, bash shows you the current set of processes. Here are a few lines of output when I type ps ax --cols 132. (I included the -cols 132 option to ensure that you can see each command in its entirety.)

PID TTY STAT TIME COMMAND
1 ? S 0:01 init [5]
2 ? SN 0:00 [ksoftirqd/0]
3 ? S< 0:00 [events/0]
4 ? S< 0:00 [khelper]
9 ? S< 0:00 [kthread]
19 ? S< 0:00 [kacpid]
75 ? S< 0:00 [kblockd/0]
115 ? S 0:00 [pdflush]
116 ? S 0:01 [pdflush]
118 ? S< 0:00 [aio/0]
117 ? S 0:00 [kswapd0]
711 ? S 0:00 [kseriod]
1075 ? S< 0:00 [reiserfs/0]
2086 ? S 0:00 [kjournald]
2239 ? S<s 0:00 /sbin/udevd -d
. . . lines deleted . . .
6374 ? S 1:51 /usr/X11R6/bin/X :0 -audit 0 -auth /var/lib/gdm/:0.Xauth 
-nolisten tcp vt7
6460 ? Ss 0:02 /opt/gnome/bin/gdmgreeter
6671 ? Ss 0:00 sshd: edulaney [priv]
6675 ? S 0:00 sshd: edulaney@pts/0
6676 pts/0 Ss 0:00 -bash
6712 pts/0 S 0:00 vsftpd
14702 ? S 0:00 pickup -l -t fifo -u
14752 pts/0 R+ 0:00 ps ax --cols 132

tip.eps In this listing, the first column has the heading PID and shows a number for each process. PID stands for process ID (identification), which is a sequential number assigned by the Linux kernel. If you look through the output of the ps ax command, you see that the initcommand is the first process and has a PID of 1. That’s why init is referred to as the mother of all processes.

The COMMAND column shows the command that created each process, and the TIME column shows the cumulative CPU time used by the process. The STAT column shows the state of a process — S means the process is sleeping, and R means it’s running. The symbols following the status letter have further meanings; for example < indicates a high-priority process, and + means that the process is running in the foreground. The TTY column shows the terminal, if any, associated with the process.

tip.eps The process ID, or process number, is useful when you have to forcibly stop an errant process. Look at the output of the ps ax command and note the PID of the offending process. Then, use the kill command with that process number to stop the process. For example, to stop process number 8550, start by typing the following command:

kill 8550

If the process doesn’t stop after five seconds, repeat the command. The next step in stopping a stubborn process is to type kill -INT pid, where pid is the process number. If that doesn’t work, try the following command as a last resort:

kill -9 8550

technicalstuff.eps The -9 option means send signal number 9 to the process. Signal number 9 is the KILL signal, which should cause the process to exit. You could also type this command as kill -KILL pid, where pid is the process ID.

Working with date and time

You can use the date command to display the current date and time or set a new date and time. Type date at the shell prompt and you get a result similar to the following:

Fri Mar 14 15:10:07 EST 2014

As you can see, issuing the date command alone displays the current date and time.

To set the date, log in as root and then type date followed by the date and time in the MMDDhhmmYYYY format, where each character is a digit. For example, to set the date and time to December 31, 2014 and 9:30 p.m., you type

date 123121302014

The MMDDhhmmYYYY date and time format is similar to the 24-hour military clock and has the following meaning:

· MM is a two-digit number for the month (01 through 12).

· DD is a two-digit number for the day of the month (01 through 31).

· hh is a two-digit hour in 24-hour format (00 is midnight and 23 is 11 p.m.).

· mm is a two-digit number for the minute (00 through 59).

· YYYY is the four-digit year (such as 2014).

The other interesting date-related command is cal. If you type cal without any options, it prints a calendar for the current month. If you type cal followed by a number, cal treats the number as the year and prints the calendar for that year. To view the calendar for a specific month in a specific year, provide the month number (1 = January, 2 = February, and so on) followed by the year. Thus, type cal 6 2014 and you get the calendar for June 2014, as follows:

June 2014
Su Mo Tu We Th Fr Sa
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30

Processing files

You can search through a text file with grep and view a text file, a screen at a time, with more. For example, to search for my username in the /etc/passwd file, I use

grep edulaney /etc/passwd

To view the /etc/inittab file a screen at a time, I type

more /etc/inittab

As each screen pauses, I press the spacebar to go to the next page.

Many more Linux commands work on files — mostly on text files, but some commands also work on any file. The sections that follow describe a few of the file-processing tools.

Counting words and lines in a text file

I am always curious about the size of files. For text files, the number of characters is basically the size of the file in bytes (because each character takes up a byte of storage space). What about words and the number of lines, though?

The Linux wc command comes to the rescue. The wc command displays the total number of lines, words, and characters in a text file. For example, type wc /etc/inittab and you see output similar to the following:

97 395 2926 /etc/inittab

In this case, wc reports that 97 lines, 395 words, and 2,926 characters are in the /etc/inittab file. If you simply want to see the number of lines in a file, use the -l option and type wc -l /etc/inittab. The resulting output should be similar to the following:

97 /etc/inittab

As you can see, with the -l option, wc simply displays the line count.

If you don’t specify a filename, the wc command expects input from the standard input. You can use the pipe feature (|) of the shell to feed the output of another command to wc, which can be handy sometimes.

tip.eps Suppose you want a rough count of the processes running on your system. You can get a list of all processes with the ps ax command, but instead of counting lines manually, just pipe the output of ps to wc and you get a rough count automatically:

ps ax | wc -l
86

Here the ps command produces 86 lines of output. Because the first line simply shows the headings for the tabular columns, you can estimate that about 85 processes are running on your system. (This count probably includes the processes used to run the ps and wc commands as well.)

Sorting text files

You can sort the lines in a text file by using the sort command. To see how the sort command works, first type more /etc/passwd to see the current contents of the /etc/passwd file. Now type sort /etc/passwd to see the lines sorted alphabetically. If you want to sort a file and save the sorted version in another file, you have to use the bash shell’s output redirection feature, like this:

sort /etc/passwd > ~/sorted.text

This command sorts the lines in the /etc/passwd file and saves the output in a file named sorted.text in your home directory.

Substituting or deleting characters from a file

Another interesting command is tr, which substitutes one group of characters for another (or deletes a selected character) throughout a file. Suppose that you have to occasionally use MS-DOS text files on your Linux system. Although you may expect to use a text file on any system without any problems, you find one catch: DOS uses a carriage return followed by a line feed to mark the end of each line whereas Linux uses only a line feed.

tip.eps On your Linux system, you can get rid of the extra carriage returns in the DOS text file by using the tr command with the -d option. Essentially, to convert the DOS text file named filename.dos to a Linux text file named filename.linux, type the following:

tr -d '\015' < filename.dos > filename.linux

In this command, '\015' denotes the code for the carriage-return character in octal notation.

Splitting a file into several smaller files

The split command is handy for those times when you want to copy a file but the file is too large to send as one e-mail attachment. You can then use the split command to break up the file into multiple smaller files.

By default, split puts 1,000 lines into each file. The new, split files are named by groups of letters such as aa, ab, ac, and so on. You can specify a prefix for the filenames. For example, to split a large file called hugefile.tar into smaller files, use split as follows:

split -b 1440k hugefile.tar part.

This command splits the hugefile.tar file into 1440K chunks. The command creates files named part.aa, part.ab, part.ac, and so on.

To combine the split files back into a single file, use the cat command as follows:

cat part.?? > hugefile.tar

In this case, the two question marks (??) match any two-character extension in the filename. In other words, the filename part.?? matches all filenames such as part.12, part.aa, part.ab, and part.2b.

Writing Shell Scripts

If you’ve ever used MS-DOS, you may remember MS-DOS batch files, which are text files with MS-DOS commands. Similarly, shell scripts are also text files with a bunch of shell commands.

If you aren’t a programmer, you may feel apprehensive about programming, but shell programming can be as simple as storing a few commands in a file. Right now, you might not be up to writing complex shell scripts, but you can certainly try out a simple shell script.

To try your hand at a little shell programming, type the following text at the shell prompt exactly as shown and then press Ctrl+D when you’re finished:

cd
cat > simple
#!/bin/sh
echo "This script's name is: $0"
echo Argument 1: $1
echo Argument 2: $2

Press Ctrl+D. The cd command changes the current directory to your home directory. Then the cat command displays the next line and any other lines you type before pressing Ctrl+D. In this case, I use > simple to send the output to a file named simple. After you press Ctrl+D, the cat command ends, and you see the shell prompt again. You created a file named simple that contains the following shell script:

#!/bin/sh
echo "This script's name is: $0"
echo Argument 1: $1
echo Argument 2: $2

The first line causes Linux to run the bash shell program (of the name /bin/bash). The shell then reads the rest of the lines in the script.

Just as most Linux commands accept command-line options, a bash script also accepts command-line options. Inside the script, you can refer to the options as $1, $2, and so on. The special name $0 refers to the name of the script itself.

To run this shell script, first you have to make the file executable (that is, turn it into a program) with the following command:

chmod +x simple

Now type ./simple one two to run the script, and it displays the following output:

This script's name is: ./simple
Argument 1: one
Argument 2: two

The ./ prefix to the script’s name indicates that the simple file is in the current directory.

This script simply prints the script’s name and the first two command-line options that the user types after the script’s name.

Next, try running the script with a few arguments, as follows:

./simple "This is one argument" second-argument third
This script's name is: ./simple
Argument 1: This is one argument
Argument 2: second-argument

The shell treats the entire string in the double quotation marks as a single argument. Otherwise, the shell uses spaces as separators between arguments on the command line.

Most useful shell scripts are more complicated than this simple script, but this easy exercise gives you a rough idea of how to write shell scripts.

remember.eps Place Linux commands in a file and use the chmod command to make the file executable. Voilà! You created a shell script!