© Richard Petersen 2018
Richard PetersenBeginning Fedora Desktophttps://doi.org/10.1007/978-1-4842-3882-0_10

10. Shells

Richard Petersen1 
(1)
Alameda, California, USA
 

The shell is a command interpreter that provides a line-oriented interactive and noninteractive interface between the user and the operating system. You enter commands on a command line. They are interpreted by the shell and then sent as instructions to the operating system (interactive). The command-line interface is accessible from GNOME and KDE through a terminal window. You can also place commands in a script file, to be consecutively executed much like a program (non-interactive). This interpretive capability of the shell provides for many sophisticated features. For example, the shell has a set of file-expansion characters that can generate filenames. The shell can redirect input and output, as well as run operations in the background, freeing you to perform other tasks.

Several different types of shells have been developed for Linux: the Bourne-Again shell (BASH), the Korn shell, the TCSH shell, and the Z shell. All shells are available for your use, although the BASH shell is the default. You need only one type of shell to do your work. Fedora Linux includes all the major shells, although it installs and uses the BASH shell as the default. If you use the command-line shell, you will be using the BASH shell, unless you specify another. This chapter discusses the BASH shell, which shares many of the same features as other shells.

You can find out more about shells at their respective websites, as listed in Table 10-1. In addition, a detailed online manual is available for each installed shell. Use the man command and the shell’s keyword to access them—bash for the BASH shell, ksh for the Korn shell, tsch for the TSCH shell, and zsh for the Z shell. For example, the command man bash will access the BASH shell online manual.
Table 10-1

Linux Shells

Shell

Website

www.gnu.org/software/bash

BASH website, with online manual, FAQ, and current releases

www.gnu.org/software/bash/manual/bash.html

BASH online manual

www.zsh.org

Z shell website, with referrals to FAQ and current downloads

www.tcsh.org

TCSH website, with detailed support, including manual, tips, FAQ, and recent releases

www.kornshell.com

Korn shell site with manual, FAQ, and references

Note

You can find out more about the BASH shell at www.gnu.org/software/bash . A detailed online manual is available on your Linux system, using the man command with the bash keyword.

The Command Line

The Linux command-line interface consists of a single line into which you enter commands with any of their options and arguments. From GNOME or KDE, you can access the command-line interface by opening a terminal window. Should you start Linux with the command-line interface, you will be presented with a BASH shell command line when you log in.

By default, the BASH shell has a dollar sign ($) prompt, but Linux has several other types of shells, each with its own prompt (such as % for the C shell). The root user will have a different prompt, the #. A shell prompt, such as the one shown following, marks the beginning of the command line:
$
At the prompt, you can enter a command, along with options and arguments. For example, with an -l option, the ls command will display a line of information about each file, listing such data as its size and the date and time it was last modified. In the following example, the user enters the ls command, followed by a -l option. The dash before the -l option is required. Linux uses it to distinguish an option from an argument.
$ ls -l
If you wanted only the information displayed for a particular file, you could add that file’s name as the argument, following the -l option.
$ ls -l mydata
-rw-r--r-- 1 chris weather 207 Feb 20 11:55 mydata

Tip

Some commands can be complex and take some time to execute. If you mistakenly execute the wrong command, you can interrupt and stop it with the interrupt key (press Ctrl+c).

You can enter a command on several lines by typing a backslash () just before you press Enter. The backslash “escapes” the Enter key, effectively continuing the same command line to the next line. In the next example, the cp command is entered on three lines. The first two lines end in a backslash, effectively making all three lines one command line.
$ cp -i
mydata
/home/george/myproject/newdata
You can also enter several commands on the same line, by separating them with a semicolon (;). In effect, the semicolon operates as an execute operation. Commands will be executed in the sequence in which they are entered. The following command executes an ls command followed by a date command:
$ ls ; date
You can also conditionally run several commands on the same line with the && operator. A command is executed only if the previous command is true. This feature is useful for running several dependent scripts on the same line. In the next example, the ls command runs only if the date command is successfully executed.
$ date && ls

Tip

Commands can also be run as arguments on a command line, using their results for other commands. To run a command within a command line, you encase the command in back quotes.

Command-Line Editing

The BASH shell, which is your default shell, has special command-line editing capabilities that you may find helpful as you learn Linux (see Table 10-2). You can easily modify commands you have entered before executing them, moving anywhere on the command line and inserting or deleting characters. This is particularly helpful for complex commands.
Table 10-2

BASH Command-Line Editing Operations

Movement Command

Operation

Ctrl+f, right-arrow

Moves forward a character

Ctrl+b, left arrow

Moves backward a character

Ctrl+a or Home

Moves to beginning of line

Ctrl+e or End

Moves to end of line

Alt+f

Moves forward a word (not on terminal window)

Alt+b

Moves backward a word

Ctrl+l

Clears screen and places line at top

Editing Command

Operation

Ctrl+d or Del

Deletes character cursor is on

Ctrl+h or Backspace

Deletes character before the cursor

Ctrl+k

Cuts remainder of line from cursor position

Ctrl+u

Cuts from cursor position to beginning of line

Ctrl+w

Cuts previous word

Ctrl+c

Cuts entire line and starts a new one

Alt+d

Cuts the remainder of a word

Alt+Del

Cuts from the cursor to the beginning of a word

Ctrl+y

Pastes previous cut text

Alt+y

Pastes from set of previously cut text

Ctrl+y

Pastes previous cut text

Ctrl+v

Inserts quoted text; used for inserting control or meta (Alt) keys as text, such as Ctrl-b for backspace or Ctrl-t for tabs

Alt+t

Transposes current and previous word

Alt+l

Lowercases current word

Alt+u

Uppercases current word

Al+-c

Capitalizes current word

Ctrl+Shift+_

Undoes previous change

You can press Ctrl+f or use the right arrow key to move forward a character, or the Ctrl+b or left arrow key to move back a character. Ctrl+d or Del deletes the character the cursor is on, and Ctrl+h or Backspace deletes the character preceding the cursor. To add text, you use the arrow keys to move the cursor to where you want to insert text and type the new characters.

You can even cut words with the Ctrl+w or Alt+d keys and then press the Ctrl+y keys to paste them back in at a different position, effectively moving the words. As a rule, the Ctrl version of the command operates on characters, and the Alt version works on words, such as Ctrl+t to transpose characters, and Alt+t to transpose words. At any time, you can press Enter to execute the command. For example, if you make a spelling mistake when entering a command, rather than reentering the entire command, you can use the editing operations to correct the mistake. The actual associations of keys and their tasks, along with global settings, are specified in the /etc/inputrc file.

The editing capabilities of the BASH shell command line are provided by Readline. Readline supports numerous editing operations. You can even bind a key to a selected editing operation. Readline uses the /etc/inputrc file to configure key bindings. This file is read automatically by your /etc/profile shell configuration file when you log in. Users can customize their editing commands by creating an .inputrc file in their home directory (this is a dot file). It may be best to first copy the /etc/inputrc file as your .inputrc file and then edit it. The /etc/profile will first check for a local .inputrc file before accessing the /etc/inputrc file. You can find out more about Readline in the BASH shell reference manual at www.gnu.org/manual/bash .

Command and Filename Completion

The BASH command line has a built-in feature that performs command and filename completion. Automatic completions can be affected by pressing the Tab key. If you enter an incomplete pattern as a command or filename argument, you can press the Tab key to activate the command and filename completion feature, which completes the pattern. A directory will have a forward slash (/) attached to its name. If more than one command or file has the same prefix, the shell simply beeps and waits for you to press the Tab key again. It then displays a list of possible command completions and waits for you to add enough characters to select a unique command or filename. For situations in which you know multiple possibilities are likely, you can just press the Esc key instead of two Tabs. In the next example, the user issues a cat command with an incomplete filename. When the user presses the Tab key, the system searches for a match and, when it finds one, fills in the filename. The user can then press Enter to execute the command.
$ cat pre <tab>
$ cat preface
The automatic completions also work with the names of variables, users, and hosts. In this case, the partial text has to be preceded by a special character, indicating the type of name. A listing of possible automatic completions follows:
  • Filenames begin with any text or /.

  • Shell variable text begins with a $ sign.

  • Username text begins with a ~ sign.

  • Hostname text begins with a @.

  • Commands, aliases, and text in files begin with normal text.

Variables begin with a $ sign, so any text beginning with a dollar sign is treated as a variable to be completed. Variables are selected from previously defined variables, such as system shell variables. Usernames begin with a tilde (~). Hostnames begin with a @ sign, with possible names taken from the /etc/hosts file. For example, to complete the variable HOME given just $HOM, simply press a Tab key.
$ echo $HOM <tab>
$ echo $HOME
If you entered just an H, then you could press the Tab key twice to see all possible variables beginning with H. The command line is redisplayed, letting you complete the name.
$ echo $H <tab> <tab>
$HISTCMD $HISTFILE $HOME $HOSTTYPE HISTFILE $HISTSIZE $HISTNAME
$ echo $H

You can also specifically select the kind of text to complete, using corresponding command keys. In this case, it does not matter what kind of sign a name begins with.

For example, pressing Alts+~ will treat the current text as a username. Pressing Alt+@ will treat it as a hostname, and Alt+$, as a variable. Pressing Alt+! will treat it as a command. To display a list of possible completions, press the Ctrl+x key with the appropriate completion key, as in Ctrl+x+$ to list possible variable completions. See Table 10-3 for a complete listing.
Table 10-3

Command-Line Text Completion Commands

Command (Ctrl+r for Listing Possible Completions)

Description

Tab

Automatic completion

Tab Tab or Esc

Lists possible completions

Alt+/, Ctrl+r+/

Filename completion, normal text for automatic

Alt+$, Ctrl+r+$

Shell variable completion, $ for automatic

Alt+~, Ctrl+-r+~

Username completion, ~ for automatic

Alt+@, Ctrl+r+@

Hostname completion, @ for automatic

Alt+!, Ctrl+r+!

Command-name completion, normal text for automatic

History

The BASH shell keeps a history list of your previously entered commands. You can display each command, in turn, on your command line by pressing the up arrow key. Press the down arrow key to move down the list. You can modify and execute any of these previous commands when you display them on the command line. The list of history command is kept in your .bash_history file.

Tip

The ability to redisplay a command is helpful when you have already executed a command you had entered incorrectly. In this case, you would be presented with an error message and a new, empty command line. By pressing the up arrow key, you can redisplay the previous command, make corrections to it, and then execute it again. This way, you don’t have to enter the whole command again.

History Events

In the BASH shell, the history utility keeps a record of the most recent commands you have executed. The commands are numbered, starting at 1, and a limit exists to the number of commands remembered. The default is 500. The history utility is a kind of short-term memory, keeping track of the most recent commands you have executed. To see the set of your most recent commands, type history on the command line and press Enter. A list of your most recent commands is then displayed, preceded by a number. Table 10-4 lists the different commands for referencing the history list.
$ history
1 cp mydata today
2 vi mydata
3 mv mydata reports
4 cd reports
5 ls
Table 10-4

History Commands and History Event References

History Command

Description

Ctrl+n or down arrow

Moves down to the next event in the history list

Ctrl+p or up arrow

Moves up to the previous event in the history list

Alt+<

Moves to the beginning of the history event list

Alt+>

Moves to the end of the history event list

Alt+n

Forward search, next matching item

Alt+p

Backward search, previous matching item

Ctrl+s

Forward search history, forward incremental search

Ctrl+r

Reverse search history, reverse incremental search

fc event-reference

Edits an event with the standard editor and then executes itOptions: -l lists recent history events; same as history command -e editor event-reference invokes a specified editor to edit a specific event

History Event Reference

 

! event num

References an event with an event number

!!

References the previous command

! characters

References an event with beginning characters

!? pattern ?

References an event with a pattern in the event

!- event num

References an event with an offset from the first event

! num-num

References a range of events

Each of these commands is technically referred to as an event. An event describes an action that has been taken—a command that has been executed. The events are numbered according to their sequence of execution. The most recent event has the highest number. Each of these events can be identified by its number or beginning characters in the command.

The history utility lets you reference a former event, placing it on your command line so that you can execute it. The easiest way to do this is to use the up arrow and down arrow keys to place history events on the command line, one at a time. You need not display the list first with history. Pressing the up arrow key once places the last history event on the command line. Pressing it again places the next history event on the command line. Pressing the down arrow key places the previous event on the command line.

You can use certain control and meta keys to perform other history operations, such as searching the history list. A meta key is the Alt key, and the Esc key on keyboards that have no Alt key. The Alt key is used here. Pressing Alt+< will move you to the beginning of the history list; Alt+n will search it. Ctrl+s and Ctrl+r will perform incremental searches, displaying matching commands as you type in a search string.

Tip

If more than one history event matches what you have entered, you will hear a beep, and you can then enter more characters to help uniquely identify the event.

You can also reference and execute history events using the ! history command . The ! is followed by a reference that identifies the command. The reference can be either the number of the event or a beginning set of characters in the event. In the next example, the third command in the history list is referenced first by number and then by the beginning characters:
$ !3
mv mydata reports
$ !mv my
mv mydata reports
You can also reference an event using an offset from the end of the list. A negative number will offset from the end of the list to that event, thereby referencing it. In the next example, the fourth command, vi mydata, is referenced using a negative offset and then executed. Remember that you are offsetting from the end of the list—in this case, event five—up toward the beginning of the list, event one. An offset of 4 beginning from event five places you at event two.
$ !-4
vi mydata
To reference the last event, you follow it with an !, as in !!. In the following example, the command !! executes the last command the user executed—in this case, ls:
$ !!
ls
mydata today reports
You can also perform a search of the listing of previous commands by entering Ctrl-r on the command line. You are prompted to enter a pattern for a search.
$ (revrse-isarch) 'vi' : vi mydata

Filename Expansion: *, ?, [ ]

Filenames are the most common arguments used in a command. Often, you will know only part of the filename, or you will want to reference several filenames that have the same extension or begin with the same characters. The shell provides a set of special characters that search out, match, and generate a list of filenames. These are the asterisk, the question mark, and brackets (*, ?, and []). Given a partial filename, the shell uses these matching operators to search for files and expand to a list of filenames found. The shell replaces the partial filename argument with the expanded list of matched filenames. This list of filenames can then become the arguments for commands such as ls, which can operate on many files. Table 10-5 lists the shell’s file-expansion characters.
Table 10-5

Shell Symbols

Common Shell Symbol

Execution

Enter

Executes a command line.

;

Separates commands on the same command line.

` command `

Executes a command as part of another command, using the command's output as a parameter to the primary command.

$( command )

Executes a command and uses the output of that command in whatever statement or command it is used in.

[]

Matches on a class of possible characters in filenames.

Quotes the following character; used to quote special characters.

|

Pipes the standard output of one command as input for another command.

&

Executes a command in the background.

!

References a history command.

File-Expansion Symbol

Execution

*

Matches on any set of characters in filenames.

?

Matches on any single character in filenames.

[]

Matches on a class of characters in filenames.

Redirection Symbol

Execution

>

Redirects the standard output to a file or device, creating the file if it does not exist and overwriting the file if it does exist.

>!

The exclamation point forces the overwriting of a file if it already exists .

<

Redirects the standard input from a file or device to a program.

>>

Redirects the standard output to a file or device, appending the output to the end of the file.

Standard Error-Redirection Symbol

Execution

2>

Redirects the standard error to a file or device.

2>>

Redirects and appends the standard error to a file or device.

2>&1

Redirects the standard error to the standard output .

Matching Multiple Characters

The asterisk (*) references files beginning or ending with a specific set of characters. You place the asterisk before or after a set of characters that form a pattern to be searched for in filenames.

If the asterisk is placed before the pattern, filenames that end in that pattern are searched for. If the asterisk is placed after the pattern, filenames that begin with that pattern are searched for. Any matching filename is copied into a list of filenames generated by this operation.

In the next example, all filenames beginning with the pattern doc are searched for and a list is generated. Then all filenames ending with the pattern day are searched for and a list is generated. The last example shows how the * can be used in any combination of characters.
$ ls
doc1 doc2 document docs mydoc monday Tuesday
$ ls doc*
doc1 doc2 document docs
$ ls *day
monday tuesday
$ ls m*d*
monday
$
Filenames often include an extension specified with a period and followed by a string denoting the file type, such as .c for C files, .cpp for C++ files, or even .jpg for JPEG image files. The extension has no special status and is only part of the characters making up the filename. Using the asterisk makes it easy to select files with a given extension. In the next example, the asterisk is used to list only those files with a .c extension. The asterisk placed before the .c constitutes the argument for ls.
$ ls *.c
calc.c main.c
You can use * with the rm command to erase several files at once. The asterisk first selects a list of files with a given extension, or beginning or ending with a given set of characters, and then it presents this list of files to the rm command to be erased. In the following example, the rm command erases all files beginning with the pattern doc:
$ rm doc*

Use the * file-expansion character carefully and sparingly with the rm command. The combination can be dangerous. A misplaced * in an rm command without the -i option could easily erase all the files in your current directory . The -i option will first prompt you to confirm whether the file should be deleted.

Matching Single Characters

The question mark (?) matches only a single incomplete character in filenames . Suppose you want to match the files doc1 and docA, but not the file called document. Whereas the asterisk will match filenames of any length, the question mark limits the match to one extra character. The following example matches files that begin with the word doc, followed by a single differing letter:
$ ls
doc1 docA document
$ ls doc?
doc1 docA

Matching a Range of Characters

Whereas the * and ? file-expansion characters specify incomplete portions of a filename, the brackets ([]) enable you to specify a set of valid characters to search for. Any character placed in the brackets will be matched in the filename. Suppose you want to list files beginning with doc but ending only in 1 or A. You are not interested in filenames ending in 2 or B, or any other character. Here is how it is done:
$ ls
doc1 doc2 doc3 docA docB docD document
$ ls doc[1A]
doc1 docA
You can also specify a set of characters as a range, rather than listing them one by one. A dash placed between the upper and lower bounds of a range of characters selects all characters in that range. The range is usually determined by the character set in use. In an ASCII character set, the range “a–g” will select all lowercase alphabetic characters from a through g, inclusive. In the next example, files beginning with the pattern doc and ending in characters 1 through 3 are selected. Then, those ending in characters B through E are matched.
$ ls doc[1-3]
doc1 doc2 doc3
$ ls doc[B-E]
docB docD
You can combine the brackets with other file-expansion characters to form flexible matching operators. Suppose you want to list only filenames ending in either a .c or .o extension but no other extension. You can use a combination of the asterisk and brackets: * [co]. The asterisk matches all filenames, and the brackets match only filenames with extension .c or .o.
$ ls *.[co]
main.c  main.o  calc.c

Matching Shell Symbols

At times, a file-expansion character is actually part of a filename. In these cases, you have to quote the character by preceding it with a backslash () to reference the file. In the next example, the user must reference a file that ends with the ? character, called answers?. The ? is, however, a file-expansion character and would match any filename beginning with “answers” that has one or more characters. In this case, the user quotes the ? with a preceding backslash to reference the filename.
$ ls answers?
answers?
Placing the filename in double quotes will also quote the character.
$ ls "answers?"
answers?
This is also true for filenames or directories that have whitespace characters such as the space character. In this case, you can either use the backslash to quote the space character in the file or directory name or place the entire name in double quotes.
$ ls My Documents
My Documents
$ ls "My Documents"
My Documents

Generating Patterns

Although not a file-expansion operation, {} is often useful for generating names that you can use to create or modify files and directories. The braces operation only generates a list of names. It does not match on existing filenames. Patterns are placed in the braces and separated with commas. Any pattern placed in the braces will be used to generate a version of the pattern, using either the preceding or following pattern, or both. Suppose you want to generate a list of names beginning with doc, but ending only in the patterns ument, final, and draft. Here is how it is done:
$ echo doc{ument,final,draft}
document docfinal docdraft
Because the names generated do not have to exist, you could use the {} operation in a command to create directories, as follows:
$ mkdir {fall,winter,spring}report
$ ls
fallreport springreport winterreport

Standard Input/Output and Redirection

The data in input and output operations is organized like a file. Data input at the keyboard is placed in a data stream arranged as a continuous set of bytes. Data output from a command or program is also placed in a data stream and arranged as a continuous set of bytes. This input data stream is referred to in Linux as the standard input, while the output data stream is called the standard output. A separate output data stream is reserved solely for error messages; it is called the standard error.

Because the standard input and standard output have the same organization as that of a file, they can easily interact with files. Linux has a redirection capability that lets you easily move data in and out of files. You can redirect the standard output so that instead of displaying the output on a screen, you can save it in a file. You can also redirect the standard input away from the keyboard to a file, so that input is read from a file instead of from your keyboard.

When a Linux command is executed that produces output, this output is placed in the standard output data stream. The default destination for the standard output data stream is a device—in this case, the screen. Devices, such as the keyboard and screen, are treated as files. They receive and send out streams of bytes with the same organization as that of a byte-stream file. The screen is a device that displays a continuous stream of bytes. By default, the standard output will send its data to the screen device, which will then display the data.

For example, the ls command generates a list of all filenames and outputs this list to the standard output. Next, this stream of bytes in the standard output is directed to the screen device. The list of filenames is then printed on the screen. The cat command also sends output to the standard output. The contents of a file are copied to the standard output, whose default destination is the screen. The contents of the file are then displayed on the screen. Table 10-6 lists the different ways you can use the redirection operators.
Table 10-6

The Shell Operations

Command

Execution

Enter

Executes a command line.

;

Separates commands on the same command line.

command opts args

Enters a backslash before a carriage return, to continue entering a command on the next line.

` command `

Executes a command.

Special Characters for Filename Expansion

Execution

*

Matches on any set of characters.

?

Matches on any single characters .

[]

Matches on a class of possible characters.

Quotes the following character; used to quote special characters.

Redirection

Execution

command > filename

Redirects the standard output to a file or device, creating the file if it does not exist and overwriting the file if it does exist.

command < filename

Redirects the standard input from a file or device to a program.

command >> filename

Redirects the standard output to a file or device, appending the output to the end of the file.

command 2> filename

Redirects the standard error to a file or device.

command 2>> filename

Redirects and appends the standard error to a file or device .

command 2>&1

Redirects the standard error to the standard output in the Bourne shell.

command >& filename

Redirects the standard error to a file or device in the C shell.

Pipe

Execution

command | command

Pipes the standard output of one command as input for another command.

Redirecting the Standard Output: > and >>

Suppose that instead of displaying a list of files on the screen, you would like to save this list in a file. In other words, you would like to direct the standard output to a file rather than the screen. To do this, you place the output redirection operator, the greater-than sign (>), followed by the name of a file on the command line, after the Linux command. In the following example, the output of the ls command is redirected from the screen device to a file:
$ ls -l *.c > programlist
The redirection operation creates the new destination file. If the file already exists, it will be overwritten with the data in the standard output. You can set the noclobber feature to prevent overwriting an existing file with the redirection operation. In this case, the redirection operation on an existing file will fail. You can overcome the noclobber feature by placing an exclamation point after the redirection operator. You can place the noclobber command in a shell configuration file to make it an automatic default operation. The following example sets the noclobber feature for the BASH shell and then forces the overwriting of the oldarticle file if it already exists:
$ set -o noclobber
$ cat myarticle >! oldarticle

Although the redirection operator and the filename are placed after the command, the redirection operation is not executed after the command. In fact, it is executed before the command. The redirection operation creates the file and sets up the redirection before it receives any data from the standard output. If the file already exists, it will be destroyed and replaced by a file of the same name. In effect, the command generating the output is executed only after the redirected file has been created.

In the next example, the output of the ls command is redirected from the screen device to a file. First the ls command lists files, and in the next command, ls redirects its file list to the listf file. Then the cat command displays the list of files saved in listf. Notice that the list of files in listf includes the listf filename. The list of filenames generated by the ls command includes the name of the file created by the redirection operation—in this case, listf. The listf file is first created by the redirection operation, then the ls command lists it along with other files. This file list output by ls is then redirected to the listf file, instead of being printed on the screen.
$ ls
mydata intro preface
$ ls > listf
$ cat listf
mydata intro listf preface

Errors occur when you try to use the same filename for an input file for the command and for the redirected destination file. In this case, because the redirection operation is executed first, the input file, because it exists, is destroyed and replaced by a file of the same name. When the command is executed, it finds an input file that is empty.

You can also append the standard output to an existing file using the >> redirection operator. Instead of overwriting the file, the data in the standard output is added at the end of the file. In the next example, the myarticle and oldarticle files are appended to the allarticles file. The allarticles file will then contain the contents of both myarticle and oldarticle.
$ cat myarticle >> allarticles
$ cat oldarticle >> allarticles

The Standard Input

Many Linux commands can receive data from the standard input. The standard input itself receives data from a device or a file. The default device for the standard input is the keyboard. Characters typed on the keyboard are placed in the standard input, which is then directed to the Linux command. Just as with the standard output, you can also redirect the standard input, receiving input from a file rather than the keyboard. The operator for redirecting the standard input is the less-than sign (<) . In the next example, the standard input is redirected to receive input from the myarticle file, rather than the keyboard device (use Ctrl+d to end the typed input). The contents of myarticle are read into the standard input by the redirection operation. Then the cat command reads the standard input and displays the contents of myarticle.
$ cat < myarticle
hello Christopher
How are you today
$
You can combine the redirection operations for both standard input and standard output. In the next example, the cat command has no filename arguments. Without filename arguments, the cat command receives input from the standard input and sends output to the standard output. However, the standard input has been redirected to receive its data from a file, while the standard output has been redirected to place its data in a file.
$ cat < myarticle > newarticle

Pipes: |

You may encounter situations in which you have to send data from one command to another. In other words, you may want to send the standard output of a command to another command, rather than to a destination file. Suppose you want to send a list of your filenames to the printer to be printed. You need two commands to do this: the ls command to generate a list of filenames and the lpr command to send the list to the printer. In effect, you have to take the output of the ls command and use it as input for the lpr command. You can think of the data as flowing from one command to another. To form such a connection in Linux, you use what is called a pipe . The pipe operator (|, the vertical bar character) placed between two commands forms a connection between them. The standard output of one command becomes the standard input for the other. The pipe operation receives output from the command placed before the pipe and sends this data as input to the command placed after the pipe. As shown in the next example, you can connect the ls command and the lpr command with a pipe. The list of filenames output by the ls command is piped into the lpr command.
$ ls | lpr
You can combine the pipe operation with other shell features, such as file-expansion characters, to perform specialized operations. The next example prints only files with a .c extension. The ls command is used with the asterisk and .c to generate a list of filenames with the .c extension. Then this list is piped to the lpr command.
$ ls *.c | lpr
In the preceding example, a list of filenames was used as input. What is important to note is that pipes operate on the standard output of a command, whatever that might be. The contents of whole files or even several files can be piped from one command to another. In the following example, the cat command reads and outputs the contents of the mydata file, which are then piped to the lpr command:
$ cat mydata | lpr
Linux has many commands that generate modified output. For example, the sort command takes the contents of a file and generates a version with each line sorted in alphabetic order. The sort command works best with files that are lists of items. Commands such as sort that output a modified version of its input are referred to as filters. Filters are often used with pipes. In the next example, a sorted version of mylist is generated and piped into the more command for display on the screen. The original file, mylist, has not been changed and is not sorted. Only the output of sort in the standard output is sorted.
$ sort mylist | more

The standard input piped into a command can be more carefully controlled with the standard input argument (-). When you use the dash as an argument for a command, it represents the standard input.

Values from UNIX Commands: Back Quotes

Although you can create variable values by typing in characters or character strings, you can also obtain values from other commands. To assign the result of command to a variable, you first need to execute the command. If you place a command in back quotes on the command line, that command is first executed and its result becomes an argument on the command line. In the case of assignments, the result of a command can be assigned to a variable by placing the command within back quotes to first execute it. The back quotes can be thought of as a kind of expression consisting of a command to be executed whose result is then assigned to the variable. The characters making up the command itself are not assigned. In the next example, the command ls *.c is executed and its result is then assigned to the variable listc. ls *.c generates a list of all files with an .c extension. This list of files will then be assigned to the listc variable.
$ listc=`ls *.c`
$ echo $listc
main.c prog.c lib.cs
You could also use the $(command) operation to the same effect.
$ listc=$(ls *.c)
$ echo $listc
main.c prog.c lib.cs
You need to keep in mind the difference between single quotes and back quotes. Single quotes treat a command as a set of characters. Back quotes force execution of the UNIX command. There may be times when you accidentally enter single quotes, when you mean to use back quotes. In the following first example, the assignment for the lscc variable has single quotes, not back quotes, placed around the ls *.c command. In this case, ls *.c are just characters to be assigned to the variable lscc. In the second example, back quotes are placed around the ls *.c command, forcing evaluation of the command. A list of filenames ending in .c is generated and assigned as the value of lscc .
$ lscc='ls *.c'
$ echo $lscc
ls *.c
$ lscc=`ls *.c`
$ echo $lscc
main.c  prog.c

Linux Files

You can name a file using any letters, underscores, and numbers. You can also include periods and commas. Except in certain special cases, you should never begin a filename with a period. Other characters, such as slashes, question marks, or asterisks, are reserved for use as special characters by the system and should not be part of a filename. Filenames can be as long as 256 characters. Filenames can also include spaces, although to reference such filenames from the command line, be sure to encase them in quotes. On a desktop such as GNOME or KDE, you do not have to use quotes. A filename cannot have the same name as a subdirectory within the same directory. Directories are treated as type of file.

You can include an extension as part of a filename. A period is used to distinguish the filename proper from the extension. Extensions can be useful for categorizing your files. You are probably familiar with certain standard extensions that have been adopted by convention. For example, C source code files always have a .c extension. Files that contain compiled object code have an .o extension. You can make up your own file extensions. The following examples are all valid Linux filenames. Keep in mind that to reference the name with spaces on the command line, you have to encase it in quotes, for example, “New book review.”
preface
chapter2
9700info
New_Revisions
calc.c
intro.bk1
New book review

Special initialization files are also used to hold shell configuration commands. These are the hidden, or dot, files, which begin with a period. Dot files used by commands and applications have predetermined names, such as the .mozilla directory used to hold your Mozilla data and configuration files. Recall that when you use ls to display your filenames, the dot files will not be displayed. To include the dot files, you must use ls with the -a option.

The ls -l command displays detailed information about a file. First, the permissions are displayed, followed by the number of links, the owner of the file, the name of the group to which the user belongs to, the file size in bytes, the date and time the file was last modified, and the name of the file. Permissions indicate who can access the file: the user, members of a group, or all other users. The group name indicates the group permitted to access the file object. The file type for mydata is that of an ordinary file. Only one link exists, indicating the file has no other names and no other links. The owner’s name is chris, the same as the login name, and the group name is weather. Other users probably also belong to the weather group. The size of the file is 207 bytes, and it was last modified on February 20 at 11:55 a.m. The name of the file is mydata.

If you want to display this detailed information for all the files in a directory, simply use the ls -l command without an argument.
$ ls -l
-rw-r--r-- 1 chris weather 207 Feb 20 11:55 mydata
-rw-rw-r-- 1 chris weather 568 Feb 14 10:30 today
-rw-rw-r-- 1 chris weather 308 Feb 17 12:40 monday

All files in Linux have one physical format, a byte stream, which is simply a sequence of bytes. This allows Linux to apply the file concept to every data component in the system. Directories are classified as files, as are devices. Treating everything as a file allows Linux to organize and exchange data more easily. The data in a file can be sent directly to a device, such as a screen, because a device interfaces with the system using the same byte-stream file format used by regular files.

This same file format is used to implement other operating system components. The interface to a device, such as the screen or keyboard, is designated as a file. Other components, such as directories, are themselves byte-stream files, but they have a special internal organization. A directory file contains information about a directory, organized in a special directory format. Because these different components are treated as files, they can be said to constitute different file types. A character device is one file type. A directory is another file type . The number of these file types may vary according to your specific implementation of Linux. Five common types of files exist, however: ordinary files, directory files, first-in first-out (FIFO) pipes, character device files, and block device files. Although you may rarely reference a file’s type, it can be useful to know when searching for directories or devices.

Although all ordinary files have a byte-stream format, they may be used in different ways. The most significant difference is between binary and text files. Compiled programs are examples of binary files. However, even text files can be classified according to their different uses. You can have files that contain C programming source code or shell commands, or even a file that is empty. The file could be an executable program or a directory file. The Linux file command helps you determine what a file is used for. It examines the first few lines of a file and tries to determine a classification for it. The file command looks for special keywords or special numbers in those first few lines, but it is not always accurate. In the following example, the file command examines the contents of two files and determines a classification for them:
$ file monday reports
monday: text
reports: directory

If you have to examine the entire file byte by byte, you can do so with the od (octal dump) command , which performs a dump of a file. By default, it prints every byte in its octal representation. However, you can also specify a character, decimal, or hexadecimal representation. The od command is helpful when you have to detect any special character in your file or if you want to display a binary file.

The File Structure

Linux organizes files into a hierarchically connected set of directories. Each directory may contain either files or other directories. In this respect, directories perform two important functions. A directory holds files, much like files held in a file drawer, and a directory connects to other directories, like a branch in a tree is connected to other branches. Because of the similarities to a tree, such a structure is often referred to as a tree structure .

The Linux file structure branches into several directories, beginning with a root directory, /. Within the root directory, several system directories contain files and programs that are features of the Linux system. The root directory also contains a directory called home that contains the home directories of all the users in the system. Each user’s home directory, in turn, contains the directories the users have made for their own use. Each of these can also contain directories. Such nested directories branch out from the user’s home directory. Table 10-7 lists the basic system directories.
Table 10-7

Standard System Directories in Linux

Directory

Function

/

Begins the file system structure, called the root.

/home

Contains users’ home directories.

/bin

Holds all the standard commands and utility programs.

/usr

Holds those files and commands used by the system; this directory breaks down into several subdirectories.

/usr/bin

Holds user-oriented commands and utility programs.

/usr/sbin

Holds system administration commands.

/usr/lib

Holds libraries for programming languages.

/usr/share/doc

Holds Linux documentation.

/usr/share/man

Holds the online man files.

/var/spool

Holds spooled files, such as those generated for printing jobs and network transfers.

/sbin

Holds system administration commands in the system.

/var

Holds files that vary, such as mailbox files.

/dev

Holds file interfaces for devices such as the terminals and printers (dynamically generated by udev; do not edit).

/etc

Holds system configuration files and any other system files.

/sys

The sysfs file system listing configuration information for all the devices on your system .

/proc

An older process file system listing kernel information, including device information.

Home Directories

When you log in to the system, you are placed within your home directory . The name given to this directory by the system is the same as your login name. Any files you create when you first log in are organized within your home directory. Within your home directory, you can create more directories. You can then change to these directories and store files in them. The same is true for other users on the system. Each user has a home directory, identified by the appropriate login name. Users, in turn, can create their own directories.

You can access a directory either through its name or by making it your working directory. Each directory is given a name when it is created. You can use this name in file operations to access files in that directory. You can also make the directory your working directory. If you do not use any directory names in a file operation, the working directory will be accessed. The working directory is the one from which you are currently working. When you log in, the working directory is your home directory, which usually has the same name as your login name. You can change the working directory by using the cd command to move to another directory.

Pathnames

The name you give to a directory or file when you create it is not its full name. The full name of a directory is its pathname . The hierarchically nested relationship among directories forms paths, and these paths can be used to identify and reference any directory or file uniquely or absolutely. Each directory in the file structure can be said to have its own unique path. The actual name by which the system identifies a directory always begins with the root directory and consists of all directories nested below that directory.

In Linux, you write a pathname by listing each directory in the path, separated from the last by a forward slash. A slash preceding the first directory in the path represents the root. The pathname for the chris directory is /home/chris. If the chris directory has a subdirectory called reports, then the entire pathname for the reports directory would be /home/chris/reports. Pathnames also apply to files. When you create a file within a directory, you give the file a name. The actual name by which the system identifies the file, however, is the filename combined with the path of directories from the root to the file’s directory. As an example, the pathname for monday is /home/chris/reports/monday (the root directory is represented by the first slash). The path for the monday file consists of the root, home, chris, and reports directories and the filename monday.

Pathnames may be absolute or relative. An absolute pathname is the complete pathname of a file or directory beginning with the root directory. A relative pathname begins from your working directory; it is the path of a file relative to your working directory. The working directory is the one you are currently operating in. Using the previous example, if chris is your working directory, the relative pathname for the file monday is reports/monday. The absolute pathname for monday is /home/chris/reports/monday.

The absolute pathname from the root to your home directory can be especially complex and, at times, even subject to change by the system administrator. To make it easier to reference, you can use the tilde (~) character, which represents the absolute pathname of your home directory. You must specify the rest of the path from your home directory. In the next example, the user references the monday file in the reports directory. The tilde represents the path to the user’s home directory, /home/chris, then the rest of the path to the monday file is specified, as follows :
$ cat ~/reports/monday

System Directories

The root directory that begins the Linux file structure contains several system directories that contain files and programs used to run and maintain the system. Many also contain other subdirectories with programs for executing specific features of Linux. For example, the directory /usr/bin contains the various Linux commands that users execute, such as lpr. The directory /bin holds system level commands.

Managing Directories: mkdir, rmdir, ls, cd, and pwd

You can create and remove your own directories , as well as change your working directory, with the mkdir, rmdir, and cd commands. Each of these commands can take as its argument the pathname for a directory. The pwd command displays the absolute pathname of your working directory. In addition to these commands, the special characters represented by a single dot, a double dot, and a tilde can be used to reference the working directory, the parent of the working directory, and the home directory, respectively. Taken together, these commands enable you to manage your directories. You can create nested directories, move from one directory to another, and use pathnames to reference any of your directories. Those commands commonly used to manage directories are listed in Table 10-8.
Table 10-8

Directory Commands

Command

Execution

mkdir directory

Creates a directory.

rmdir directory

Erases a directory.

ls -F

Lists the directory name with a preceding slash.

ls -R

Lists the working directory as well as all subdirectories.

cd directory

Changes to the specified directory, making it the working directory. cd without a directory name changes back to the home directory:$ cd reports

pwd

Displays the pathname of the working directory.

directory / filename

A slash is used in pathnames to separate each directory name. In the case of pathnames for files, a slash separates the preceding directory names from the filename.

..

References the parent directory. You can use it as an argument or as part of a pathname:$ cd ..$ mv ../larisa oldarticles

.

References the working directory. You can use it as an argument or as part of a pathname :$ ls .

~/ pathname

The tilde is a special character that represents the pathname for the home directory. It is useful when you have to use an absolute pathname for a file or directory:$ cp monday ~/today

j pattern

Use the autojump command to quickly jump to a frequently used directory using a matching pattern.

jumpstat

Statistics showing frequently used directories accessible with autojump.

Creating and Deleting Directories

You create and remove directories with the mkdir and rmdir commands. In either case, you can also use pathnames for the directories. In the next example, the user creates the directory reports. Then the user creates the directory articles, using a pathname.
$ mkdir reports
$ mkdir /home/chris/articles
If the intervening parent directories in a pathname do not yet exist, you can have them created automatically with the -p option. In the following example, the intervening parent subdirectories project/newproject, indicated by the pathname, do not yet exist. Using the -p option creates them automatically, creating the project, newproject, and proposals subdirectories. Without the -p option, you would receive an error stating the directories do not exist.
$ mkdir -p  /home/chris/project/newproject/proposal
You can remove a directory with the rmdir command followed by the directory name. In the following example, the user removes the directory reports with the rmdir command:
$ rmdir reports
To remove a directory and all its subdirectories, you use the rm command with the -r option. This is a very powerful command and could be used to erase all your files. You will be prompted for each file. To remove all files and subdirectories without prompts, add the -f option. The following example deletes the reports directory and all its subdirectories:
rm -rf reports

Displaying Directory Contents

You have seen how to use the ls command to list the files and directories in your working directory. To distinguish between file and directory names, however, you must use the ls command with the -F option. A slash is then placed after each directory name in the list.
$ ls
weather reports articles
$ ls -F
weather reports/ articles/
The ls command also takes as an argument any directory name or directory pathname. This enables you to list the files in any directory without first having to change to that directory. In the next example, the ls command takes as its argument the name of a directory, reports. Then the ls command is executed again, only this time, the absolute pathname of reports is used.
$ ls reports
monday tuesday
$ ls /home/chris/reports
monday tuesday
$

Moving Through Directories

The cd command takes as its argument the name of the directory to which you want to move . The name of the directory can be the name of a subdirectory in your working directory or the full pathname of any directory on the system. If you want to change back to your home directory, you need to enter only the cd command by itself, without a filename argument.
$ cd reports
$ pwd
/home/chris/reports
As a complement to the cd command , you can use the autojump command. Install the autojump package. autojump keeps a record of your frequently accessed directories. You can then use the j command with a partial-matching pattern to quickly jump (cd) to that directory.
$ j rep
$ pwd
/home/chris/reports

Jumpstart keeps a database of your cd operations and uses it to determine the directory you want. The jumpstat command displays the ranked directories.

Referencing the Parent Directory

A directory always has a parent (except, of course, for the root). For example, in the preceding listing, the parent for reports is the chris directory. When a directory is created, two entries are made: one represented with a dot (.), and the other with double dots (..). The dot represents the pathnames of the directory, and the double dots represent the pathname of its parent directory. Double dots, used as an argument in a command, reference a parent directory. The single dot references the directory itself.

You can use the single dot to reference your working directory, instead of using its pathname. For example, to copy a file to the working directory retaining the same name, the dot can be used in place of the working directory’s pathname. In this sense, the dot is another name for the working directory. In the next example, the user copies the weather file from the chris directory to the reports directory. The reports directory is the working directory and can be represented with a single dot.
$ cd reports
$ cp /home/chris/weather .
The .. symbol is often used to reference files in the parent directory. In the next example, the cat command displays the weather file in the parent directory. The pathname for the file is the .. symbol (for the parent directory), followed by a slash and the filename.
$ cat ../weather
raining and warm

You can use the cd command with the .. symbol to step back through successive parent directories of the directory tree from a lower directory.

Listing, Displaying, and Printing Files: ls, cat, more, less, and lpr

One of the primary functions of an operating system is the management of files. You may be required to perform certain basic output operations on your files, such as displaying them on your screen or printing them. The Linux system provides a set of commands that perform basic file-management operations, such as listing, displaying, and printing files, as well as copying, renaming, and erasing files. These commands are usually made up of abbreviated versions of words. For example, the ls command is a shortened form of “list” and lists the files in your directory. The lpr command is an abbreviated form of “line print” and will print a file. The cat, less, and more commands display the contents of a file on the screen. Table 10-9 lists these commands with their different options. When you log in to your Linux system, you may want a list of the files in your home directory. The ls command, which outputs a list of your file and directory names, is useful for this. The ls command has many possible options for displaying filenames according to specific features.

Displaying Files: cat, less, and more

You may also have to look at the contents of a file. The cat and more commands display the contents of a file on the screen. The name cat stands for “concatenate.”
$ cat mydata
computers
The cat command outputs the entire text of a file to the screen at once. This presents a problem when the file is large, because its text quickly speeds past on the screen. The more and less commands are designed to overcome this limitation, by displaying one screen of text at a time. You can then move forward or backward in the text at your leisure. You invoke the more or less command by entering the command name, followed by the name of the file you want to view (less is a more powerful and configurable display utility).
$ less mydata
When more or less invoke a file, the first screen of text is displayed. To continue to the next screen, you press the f key, the spacebar, or PageUp keys. To move back in the text, you press the b or PageDown keys. You can quit at any time by pressing the q key.
Table 10-9

Listing, Displaying, and Printing Files

Command or Option

Execution

ls

Lists file and directory names.

cat filenames

Used to display a file. It can take filenames for its arguments. It outputs the contents of those files directly to the standard output, which, by default, is directed to the screen.

more filenames

Displays a file screen by screen. Press the spacebar to continue to the next screen and the q key to quit.

less filenames

Also displays a file screen by screen. Press the spacebar to continue to the next screen and the q key to quit.

lpr filenames

Sends a file to the line printer to be printed; a list of files may be used as arguments. Use the -P option to specify a printer.

lpq

Lists the print queue for printing jobs .

lprm

Removes a printing job from the print queue.

Printing Files: lpr, lpq, and lprm

With the printer commands, such as lpr and lprm, you can perform printing operations such as printing files or canceling print jobs (see Table 10-9). When you have to print files, use the lpr command to send files to the printer connected to your system. In the following example, the user prints the mydata file:
$ lpr mydata
If you want to print several files at once, you can specify more than one file on the command line after the lpr command. In the following example, the user prints the mydata and preface files:
$ lpr mydata preface

Printing jobs are placed in a queue and printed one at a time in the background. You can continue with other work as your files print. You can see the position of a particular printing job at any given time with the lpq command , which gives the owner of the printing job (the login name of the user who sent the job), the print job ID, the size in bytes, and the temporary file in which it is currently held.

If you have to cancel an unwanted printing job, you can do so with the lprm command , which takes as its argument either the ID number of the printing job or the owner’s name. It then removes the print job from the print queue. For this task, lpq is helpful, as it provides you with the ID number and owner of the printing job you must use with lprm.

File and Directory Operations: find, cp, mv, rm, and ln

As you create more and more files, you may want to back them up, change their names, erase some of them, or even give them added names. Linux provides several file commands that you can use to search for, copy, rename, or remove files (see Table 10-11). If you have a large number of files, you can also search them to locate a specific one. The commands are shortened forms of full words, consisting of only two characters. The cp command stands for “copy” and copies a file; mv stands for “move” and renames or moves a file; rm stands for “remove” and erases a file; and ln stands for “link” and adds another name for a file, often used as a shortcut to the original. One exception to the two-character rule is the find command, which performs searches of your filenames to find a file. All these operations can be handled by the GUI desktops, such as GNOME and KDE. To quickly search for a file, you can use the locate command. The locate command reads from a database of file locations compiled daily by updatedb.

Searching Directories: find

Once a large number of files has been stored in many different directories, you may have to search them to locate a specific file, or files, of a certain type, as well as directories. The find command enables you to perform such a search from the command line. The find command takes as its arguments directory names, followed by several possible options that specify the type of search and the criteria for the search. It then searches within the directories listed and their subdirectories for files that meet these criteria. The find command can search for a file by name, type, owner, and even the time of the last update (see Table 10-10).
$ find directory-list -option criteria
Table 10-10

The find Command

Command or Option

Execution

find

Searches directories for files according to search criteria. This command has several options that specify the type of criteria and actions to be taken.

-name pattern

Searches for files with the pattern in the name.

-lname pattern

Searches for symbolic link files.

-group name

Searches for files belonging to the group name.

-gid name

Searches for files belonging to a group according to group ID.

-user name

Searches for files belonging to a user.

-uid name

Searches for files belonging to a user according to user ID.

-mtime num

Searches for files last modified num days ago.

-context scontext

Searches for files according to security context (SELinux).

-print

Outputs the result of the search to the standard output. The result is usually a list of filenames, including their full pathnames.

-type filetype

Searches for files with the specified file type. File type can be b for block device, c for character device, d for directory, f for file, or l for symbolic link.

-perm permission

Searches for files with certain permissions set. Use octal or symbolic format for permissions.

-ls

Provides a detailed listing of each file, with owner, permission, size, and date information.

-exec command

Executes command when files are found .

The -name option has as its criteria a pattern and instructs find to search for the filename that matches that pattern. To search for a file by name, you use the find command with the directory name, followed by the -name option and the name of the file.
$ find directory-list -name filename
The find command also has options that merely perform actions, such as outputting the results of a search. If you want find to display the filenames it has located, you simply include the -print option on the command line, along with any other options. The -print option instructs find to write to the standard output the names of all the files it locates. (You can also use the -ls option instead to list files in the long format.) In the next example, the user searches for all the files in the reports directory with the name monday. Once located, the file, with its relative pathname, is printed.
$ find reports -name monday -print
reports/monday
The find command prints the filenames using the directory name specified in the directory list. If you specify an absolute pathname, the absolute path of the found directories will be output. If you specify a relative pathname, only the relative pathname is output. In the preceding example, the user specified a relative pathname, reports, in the directory list. Located filenames were output beginning with this relative pathname. In the next example, the user specifies an absolute pathname in the directory list. Located filenames are then output using this absolute pathname.
$ find /home/chris -name monday -print
/home/chris/reports/monday

Should you need to find the location of a specific program or configuration file, you could use find to search for the file from the root directory. Log in as the root user and use / as the directory. This command searches for the location of the more command and files on the entire file system: find / -name more -print.

Searching the Working Directory

If you want to search your working directory, you can use the dot in the directory pathname to represent it. The double dots represent the parent directory. The next example searches all files and subdirectories in the working directory, using the dot to represent the working directory. If your working directory is your home directory, this is a convenient way to search through all your own directories. Notice that the located filenames that are output begin with a dot.
$ find . -name weather -print
./weather
You can use shell wildcard characters as part of the pattern criteria for searching files. The special character must be quoted, however, to avoid evaluation by the shell. In the next example, all files (indicated by the asterisk, *) with the .c extension in the programs directory are searched for and then displayed in the long format, using the -ls action:
$ find programs -name '*.c' -ls

Locating Directories

You can also use the find command to locate other directories . In Linux, a directory is officially classified as a special type of file. Although all files have a byte-stream format, some files, such as directories, are used in special ways. In this sense, a file can be said to have a file type. The find command has an option called -type that searches for a file of a given type. The -type option takes a one-character modifier that represents the file type. The modifier that represents a directory is a d. In the following example, both the directory name and the directory file type are used to search for the directory called travel:
$ find /home/chris -name travel -type d -print
/home/chris/articles/travel
$

File types are not so much different types of files as they are the file format applied to other components of the operating system, such as devices. In this sense, a device is treated as a type of file, and you can use find to search for devices and directories, as well as ordinary files. Table 10-10 lists the different types available for the find command’s -type option.

You can also use the find operation to search for files by ownership or security criteria, like those belonging to a specific user or those with a certain security context. The -user option lets you locate all files belonging to a certain user. The following example lists all files that the user chris has created or owns on the entire system. To list those only in users’ home directories, you use /home for the starting search directory. This would find all those in a user’s home directory , as well as any owned by that user in other user directories.
$ find / -user chris -print

Copying Files

To make a copy of a file, you simply give cp two filenames as its arguments (see Table 10-11). The first filename is the name of the file to be copied—the one that already exists. This is often referred to as the source file. The second filename is the name you want for the copy. This will be a new file containing a copy of all the data in the source file. This second argument is often referred to as the destination file. The syntax for the cp command follows:
$ cp source-file destination-file
In the following example, the user copies a file called proposal to a new file called oldprop:
$ cp proposal oldprop
You could unintentionally destroy another file with the cp command . The cp command generates a copy by first creating a file and then copying data into it. If another file has the same name as the destination file, that file is destroyed, and a new file with that name is created. By default, Fedora configures your system to check for an existing copy by the same name (cp is aliased with the -i option). To copy a file from your working directory to another directory, you have to use that directory name as the second argument in the cp command. In the next example, the proposal file is overwritten by the newprop file. The proposal file already exists.
$ cp newprop proposal
You can use any of the wildcard characters to generate a list of filenames to use with cp or mv. For example, suppose you need to copy all your C source code files to a given directory. Instead of listing each one individually on the command line, you could use an * character with the .c extension to match on and generate a list of C source code files (all files with a .c extension). In the following example, the user copies all source code files in the current directory to the sourcebks directory:
$ cp *.c sourcebks
Table 10-11

File Operations

Command

Execution

cp filename filename

Copies a file. cp takes two arguments: the original file and the name of the new copy. You can use pathnames for the files to copy across directories.

cp -r directory directory

Copies a subdirectory from one directory to another. The copied directory includes all its own subdirectories.

mv filename filename

Moves (renames) a file. The mv command takes two arguments: the first is the file to be moved. The second argument can be the new filename or the pathname of a directory. If it is the name of a directory, then the file is moved to that directory, changing the file’s pathname.

mv directory directory

Moves directories. In this case, the first and last arguments are directories.

ln filename filename

Creates added names for files referred to as links. A link can be created in one directory that references a file in another directory.

rm filenames

Removes (erases) a file. Can take any number of filenames as its arguments. Literally removes links to a file. If a file has more than one link, you must remove all of them to erase a file.

If you want to copy all the files in a given directory to another directory, you could use * to match on and generate a list of all those files in a cp command . In the next example, the user copies all the files in the props directory to the oldprop directory. Notice the use of a props pathname preceding the * special characters. In this context, props is a pathname that will be appended before each file in the list that * generates.
$ cp props/* oldprop
You can, of course, use any of the other special characters, such as ., ?, or []. In the following example, the user copies both source code and object code files (.c and .o) to the projbk directory:
$ cp *.[oc] projbk
When you copy a file, you can give the copy a name that is different from the original. To do so, place the new filename after the directory name, separated by a slash.
$ cp filename directory-name/new-filename

Moving Files

You can use the mv command either to rename a file or move a file from one directory to another. When using mv to rename a file, you simply use the new filename as the second argument. The first argument is the current name of the file you are renaming. If you want to rename a file when you move it, you can specify the new name of the file after the directory name. In the following example, the proposal file is renamed with the name version1:
$ mv proposal version1

As with cp, it is easy for mv to erase a file accidentally. When renaming a file, you might accidentally choose a filename already used by another file. In this case, that other file will be erased. The mv command also has an -i option that checks first to see if a file with that name already exists.

You can also use any of the special characters to generate a list of filenames to use with mv. In the following example, the user moves all source code files in the current directory to the newproj directory:
$ mv *.c newproj
If you want to move all the files in a given directory to another directory, you can use * to match on and generate a list of all those files. In the following example, the user moves all the files in the reports directory to the repbks directory:
$ mv reports/* repbks

Copying and Moving Directories

You can also copy or move whole directories at once. Both cp and mv can take as their first argument a directory name, enabling you to copy or move subdirectories from one directory into another (see Table 10-11). The first argument is the name of the directory to be moved or copied, and the second argument is the name of the directory within which it is to be placed. The same pathname structure used for files applies to moving or copying directories.

You can just as easily copy subdirectories from one directory to another. To copy a directory, the cp command requires you to use the -r option, which stands for “recursive.” It directs the cp command to copy a directory, as well as any subdirectories it may contain. In other words, the entire directory subtree, from that directory on, will be copied. In the next example, the travel directory is copied to the oldarticles directory. Now two travel subdirectories exist, one in articles and one in oldarticles.
$ cp -r articles/travel oldarticles
$ ls -F articles
/travel
$ ls -F oldarticles
/travel

Erasing Files and Directories: The rm Command

As you use Linux, you will find the number of files you use increases rapidly. Generating files in Linux is easy. Applications such as editors, and commands such as cp, can easily be used to create files. Eventually, many of these files may become outdated and useless. You can then remove them with the rm command. The rm command can take any number of arguments, enabling you to list several filenames and erase them all at the same time. In the next example, the file oldprop is erased:
$ rm oldprop
Be careful when using the rm command, because it is irrevocable. Once a file is removed, it cannot be restored (there is no undo). With the -i option, you are prompted separately for each file and asked whether you really want to remove it. If you enter y, the file will be removed. If you enter anything else, the file is not removed. In the next example, the rm command is instructed to erase the files proposal and oldprop. The rm command then asks for confirmation for each file. The user decides to remove oldprop but not proposal.
$ rm  -i proposal oldprop
Remove proposal? n
Remove oldprop? y
$

Links: The ln Command

You can give a file more than one name using the ln command . You might do this because you want to reference a file using different filenames to access it from different directories. The added names are often referred to as links . Linux supports two different types of links, hard and symbolic. Hard links are literally another name for the same file, whereas symbolic links function like shortcuts referencing another file. Symbolic links are much more flexible and can work over many different file systems, while hard links are limited to your local file system. Furthermore, hard links introduce security concerns, as they allow direct access from a link that may have public access to an original file that you may want protected. Links are usually implemented as symbolic links.

Symbolic Links

To set up a symbolic link, use the ln command with the -s option and two arguments: the name of the original file and the new, added filename. The ls operation lists both filenames, but only one physical file will exist.
$ ln -s original-filename added-filename
In the next example, the today file is given the additional name weather. It is just another name for the today file.
$ ls
today
$ ln -s today weather
$ ls
today weather
You can give the same file several names by using the ln command on the same file many times. In the next example, the file today is assigned the names weather and weekend:
$ ln -s today weather
$ ln -s today weekend
$ ls
today weather weekend
If you list the full information about a symbolic link and its file, you will find that the information displayed is different. In the next example, the user lists the full information for both lunch and /home/george/veglist, using the ls command with the -l option. The first character in the line specifies the file type. Symbolic links have their own file type, represented by an l. The file type for lunch is l, indicating it is a symbolic link, not an ordinary file. The number after the term “group” is the size of the file. Notice that the sizes differ. The size of the lunch file is only 4 bytes. This is because lunch is only a symbolic link—a file that holds the pathname of another file—and a pathname takes up only a few bytes. It is not a direct hard link to the veglist file.
$ ls -l lunch /home/george/veglist
lrw-rw-r-- 1 chris group 4 Feb 14 10:30 lunch
-rw-rw-r-- 1 george group 793 Feb 14 10:30 veglist

To erase a file, you have to remove only its original name (and any hard links to it). If any symbolic links are left over, they will be unable to access the file. In this case, a symbolic link would hold the pathname of a file that no longer exists.

Hard Links

You can give the same file several names by using the ln command on the same file many times. To set up a hard link, you use the ln command with no -s option and two arguments: the name of the original file and the new, added filename. The ls operation lists both filenames, but only one physical file will exist.
$ ln original-filename added-filename
In the next example, the monday file is given the additional name storm. It is just another name for the monday file.
$ ls
today
$ ln monday storm
$ ls
monday storm

To erase a file that has hard links, you must remove all its hard links. The name of a file is actually considered a link to that file; hence, the command rm removes the link to the file. If you have several links to the file and remove only one of them, the others stay in place, and you can reference the file through them. The same is true even if you remove the original link—the original name of the file.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.141.28.107