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 you with several filecommands that enable you to search for files, copy files, rename files, or remove files. If you have a large number of files, you can also search themto locate a specific one. The command names shortened forms of full words, consisting ofonly 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 lnstands 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 ofyour filenames to find a file. All these operations can be handled by the GUI desktops suchas GNOME and KDE.
Searching Directories: findOnce you have a large number of files in many different directories, you may need to searchthem to locate a specific file, or files, of a certain type. The find command enables you toperform such a search from the command line. The find command takes as its argumentsdirectory names followed by several possible options that specify the type of search and thecriteria for the search; it then searches within the directories listed and their subdirectoriesfor 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.$ find directory-list -option criteria
The find command also has options that merely perform actions, such as outputtingthe results of a search. If you want find to display the filenames it has found, you simplyinclude the -print option on the command line along with any other options. The -printoption is an action that instructs find to write to the standard output the names of all thefiles it locates (you can also use the -ls option instead to list files in the long format). In thenext example, the user searches for all the files in the reports directory with the namemonday. Once located, the file, with its relative pathname, is printed.$ find reports -name monday -printreports/mondayThe find command prints out the filenames using the directory name specified in thedirectory list. If you specify an absolute pathname, the absolute path of the founddirectories will be output. If you specify a relative pathname, only the relative pathnamewill be output. In the preceding example, the user specified a relative pathname, reports, inthe directory list. Located filenames were output beginning with this relative pathname. Inthe next example, the user specifies an absolute pathname in the directory list. Locatedfilenames are then output using this absolute pathname.$ find /home/chris -name monday -print/home/chris/reports/monday
Locating DirectoriesYou can also use the find command to locate other directories. In Linux, a directory isofficially 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 tohave a file type. The find command has an option called -type that searches for a file of agiven 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 next example, both the directory nameand the directory file type are used to search for the directory called thankyou:$ find /home/chris -name thankyou -type d -print/home/chris/letters/thankyou$File types are not so much different types of files as they are the file format applied toother components of the operating system, such as devices. In this sense, a device is treatedas a type of file, and you can use find to search for devices and directories, as well asordinary files. Table 6-4 lists the different types available for the find command’s -typeoption.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 useroption lets you locate all files belonging to a certain user. The following example lists allfiles that the user chris has created or owns on the entire system. To list those just in theusers’ home directories, you use /home for the starting search directory. This finds all filesin a user's home directory as well as any owned by that user in other user directories.$ find / -user chris -print
Introduction
A shell is a command line interpretor. It takes commands and executes them. As such, it implements a programming language. The Bourne shell is used to create shell scripts -- ie. programs that are interpreted/executed by the shell. You can write shell scripts with the C-shell; however, this is not covered here.
Necessity of Structured Shell Programming
Many shells, including Bash, have developed a mixture of features with specialized or arcane applications. Like the autopilot, it is not always clear how these features should be
used nor which features are provided primarily for compatibility.Too often, poorly designed scripts are all-but illegible to another programmer, or even to the same programmer a few months after a script was hastily assembled.
This presents a problem for serious business scripts.The cost of developing a highlevel language solution is high, but a disorganized shell script can be expensive to maintain
over time. As the needs of the business change, and the shell script is modified by successive generations of programmers, the problem escalates.
As a rule of thumb, business programs never go away.At one place I recently worked,what started off as a simple shell script used to print reports gradually evolved into a Web-based reporting system complete with personal customizations and secure access.
The original programmer had no idea what his script would become. Because of the essential openness and complex syntax of Bash, it’s to the benefit of any serious project to adapt some kind of standard. Bash is very forgiving, but the costs are not. For example, there are many ways to add 2 + 2 in Bash. Like the autopilot anecdote, it’s not practical to expect the programmers who maintain the scripts to have to deal with this kind of redundancy. Likewise, shell word quoting is sometimes optional, sometimes required, depending on the context. A command that works with one form of quotation can suddenly stop working when different data appears in the quotes. Issues like shell word quoting, specialized capabilities, or portability features, when abused, can lead to increased maintenance and long-term development costs.
The Origin of Linux
Linux uses GNU General Public License (GPL) and its source code is freely available to everyone. Linux distributions, CD-ROMs with the Linux kernel and other software ready for installation, do not have to be free, but the Linux source code must remain available. Making source code available is known as distributing open source. The word “Linux” is properly pronounced using a Swedish accent, making it difficult to say in North America. It is most often pronounced with a short “i” and with the firstsyllable stressed, as in LIH-nicks, but it is sometimes pronounced LYE-nicks (the anglicized “Linus’ Unix”) or LEE-nucks. Strictly speaking, Linux refers to the operating system kernel that starts and manages other programs and provides access to system resources.The various open source shells, compilers, standard libraries, and commands are a part of another project called GNU. The GNU project was started by the Free Software Foundation (FSF) as an attempt to create a free version of Unix.The main Linux C compiler, gcc, is a part of the GNU project. There is also a GNU kernel project, but the Linux kernel has largely superseded thiseffort.
X Windows is also not strictly a part of Linux. Xfree86, the open source version of X Windows, was adapted to the Linux operating system and was released under a different license.
find, cp, mv, rm, and ln
मानगे Directories
A directory always has a parent (except, of course, for the root). For example, in thepreceding listing, the parent for props is the dylan 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 pathname of the directory, and the double dots represent thepathname 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 itspathname. 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 isanother name for the working directory. In the next example, the user copies the weatherfile from the chris directory to the reports directory. The reports directory is the workingdirectory and can be represented with the single dot.$ cd reports$ cp /home/chris/weather .The .. symbol is often used to reference files in the parent directory. In the nextexample, the cat command displays the weather file in the parent directory. The pathnamefor the file is the .. symbol followed by a slash and the filename.
मानगे Directories
A directory always has a parent (except, of course, for the root). For example, in thepreceding listing, the parent for props is the dylan 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 pathname of the directory, and the double dots represent thepathname 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 itspathname. 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 isanother name for the working directory. In the next example, the user copies the weatherfile from the chris directory to the reports directory. The reports directory is the workingdirectory and can be represented with the single dot.$ cd reports$ cp /home/chris/weather .The .. symbol is often used to reference files in the parent directory. In the nextexample, the cat command displays the weather file in the parent directory. The pathnamefor the file is the .. symbol followed by a slash and the filename.
Printing Files
With the printer commands such as lpr and lprm, you can perform printing operationssuch अस printing files or canceling print jobs (see Table 6-2). When you need to print files,use the lpr command to send files to the printer connected to your system. In the nextexample, the user prints the mydata file:$ lpr mydataIf you want to print several files at once, you can specify more than one file on thecommand line after the lpr command. In the next example, the user prints out both themydata and preface files:$ lpr mydata prefacePrinting jobs are placed in a queue and printed one at a time in the background. You cancontinue with other work as your files print. You can see the position of a particular printingjob at any given time with the lpq command, which gives the owner of the printing job (thelogin name of the user who sent the job), the print job ID, the size in bytes, and the temporaryfile in which it is currently held.If you need 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. Itthen removes the print job from the print queue. For this task, lpq is helpful, for it providesyou with the ID number and owner of the printing job you need to use with lprm.
Displaying Directory ContentsYou have seen how to use the ls command to list the files and directories within yourworking directory. To distinguish between file and directory names, however, you need touse the ls command with the -F option. A slash is then placed after each directory namein the list.$ lsweather reports letters$ ls -Fweather reports/ letters/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 thatdirectory. In the next example, the ls command takes as its argument the name of adirectory, reports. Then the ls command is executed again, only this time the absolutepathname of reports is used.$ ls reportsmonday tuesday$ ls /home/chris/reportsmonday tuesday$
History Events
$ history
1 cp mydata today
2 vi mydata
3 mv mydata reports
4 cd reports
5 ls
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 largest number. Each of these events can be identified by its number or beginning characters in the command.
The history utility enables you to reference a former event, placing it on your command line and enabling you to execute it. The easiest way to do this is to use the UP ARROW and DOWN ARROW keys to place history events on your command line, one at a time. You needn’t display the list first with history. Pressing the UP ARROW key once places the last history event on your command line. Pressing it again places the next history event on your command. Pressing the DOWN ARROW key places the next event on the command line. You can use certain control and meta keys to perform other history operations like searching the history list. A meta key is the ALT key, or the ESC key on keyboards that have no ALT key. The ALT key is used here. 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.
Command Line Editing
Movement Commands Operation
CTRL-F, RIGHT-ARROW Move forward a character.
CTRL-B, LEFT-ARROW Move backward a character.
CTRL-A or HOME Move to beginning of line.
CTRL-E or END Move to end of line.
ALT-F Move forward a word.
ALT-B Move backward a word.
CTRL-L Clear screen and place line at top.
Editing Commands Operation
CTRL-D or DEL Delete character cursor is on.
CTRL-H or BACKSPACE Delete character before the cursor.
CTRL-K Cut remainder of line from cursor position.
CTRL-U Cut from cursor position to beginning of line.
CTRL-W Cut previous word.
CTRL-C Cut entire line.
ALT-D Cut the remainder of a word.
ALT-DEL Cut from the cursor to the beginning of a word.
CTRL-Y Paste previous cut text.
ALT-Y Paste from set of previously cut text.
CTRL-Y Paste previous cut text.
CTRL-V Insert 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 Transpose current and previous word.
ALT-L Lowercase current word.
ALT-U Uppercase current word.
ALT-C Capitalize current word.
CTRL-SHIFT-_ Undo previous change.
TABLE 3-2 Command Line Editing Operations
Running Windows Software on Linux: Wine
Once installed, a Wine menu will appear in the Applications menu. The Wine menu holds entries for Wine configuration, the Wine software uninstaller, and the Wine file browser, as well as a regedit registry editor, a notepad, and a Wine help tool. To set up Wine, a user starts the Wine Configuration tool. This opens a window with panels for Applications, Libraries (DLL selection), Audio (sound drivers), Drives, Desktop Integration, and Graphics. On the Applications panel you can select which version of Windows an application is designed for. The Drives panel will list your detected partitions, as well as your Windows-emulated drives, such as drive C:. The C: drive is really just a directory, .wine/drive_c, not a partition of a fixed size. Your actual Linux file system will be listed as the Z: drive. Once configured, Wine will set up a .wine directory on the user’s home directory (the directory is hidden, so enable Show Hidden Files in the file browser View menu to display it). Within that directory will be the drive_c directory, which functions as the C: drive, holding your Windows system files and program files in the Windows and Program File subdirectories. The System and System32 directories are located in the Windows directory. Here is where you place any needed DLL files. The Program Files directory will hold your installed Windows programs, just as they would be installed on a Windows Program Files directory. To install a Windows application with Wine, you can either use the Wine configuration tool or open a Terminal window and run the wine command with the Windows application as an argument. The following example installs the popular newsbin program: $ wine newsbin.exe To install with the Windows Configuration tool, select the Applications panel and then click Add. Some applications, such as newsbin, will also require that you use certain DLL files from a working Windows operating system. The DLL files are normally copied to the user’s .wine/drive_c/Windows/system32 directory. Icons for installed Windows software will appear on your desktop. Just double-click an icon to start up the application. It will run normally within a Linux window, as would any Linux application. Installing Windows fonts on Wine is a simple matter of copying fonts from a Windows font directory to your Wine .wine/drive_c/Windows/fonts directory. You can just copy any Windows .ttf file to this directory to install a font. You can also use the Microsoft common web fonts available from fontconfig.org (this will require cabextract to extract them). Wine will use a stripped-down window style for features like buttons and the title bar. If you want to use the XP style, download and install the Royal theme from Microsoft. Keep in mind, however, that supporting this theme is very resource intensive and will likely slow down your system.
Open Source Software
Pipe and Socket Files
A similar kind of file is called a Unix domain socket file.A socket file acts like a pipe but works using network sockets.
Files and File Systems
The standard file system is the second extended file system, or ext2.This is the second revision of the Minix file system with support for large disk sizes and filenames. ext2 permits partitions up to 4TB (terabytes), files up to 2GB (gigabytes), and 255-character filenames. Newer distributions use ext3, a version of ext2 with special features for error recovery.
Support for other file systems might be available depending on your distribution and
installation options.They might include Microsoft Windows NT, Apple HFS, or journalingfile systems.
The ext2 file system uses caching to increase performance. If an ext2 disk is not
properly shut down, files can be corrupted or lost. It is vitally important that a Linux computer is shut down properly or is protected by some kind of uninterruptible power supply.To save space, ext2 files that contain large amounts of zeros (called sparse files) are not actually stored on a disk. Certain shell commands provide options for creating and handling sparse files. Each file is identified by a name, and the allowed names are determined by the file system. For practicality, the names seldom exceed 32 characters and usually consist of lowercase characters, underscores, minus signs, and periods. Spaces and punctuation symbols, for example, are permitted, but can cause problems in shell scripts that do not expect them. Filenames do not require a suffix to identify their contents, but they are often used to avoid confusion about what data is contained in files. Some common suffix codes include:
> .sh—A Bash shell script
> .txt—A generic text file
> .log—A log file
> .html—A HTML Web page
> .tgz (or .tar.gz)—Compressed file archive
Commands usually have no suffix.
find . -name file -print
and you'd rather type a simple command, say
sfind file
Create a shell script
% cd ~/bin
% emacs sfind
% page sfind
find . -name $1 -print
% chmod a+x sfind
% rehash
% cd /usr/local/bin
% sfind tcsh
./shells/tcsh
Observations
This quick example is far from adequate but some observations:
1. Shell scripts are simple text files created with an editor.
2. Shell scripts are marked as executeable
3. %chmod a+x sfind
4. Should be located in your search path and ~/bin should be in your search path.
5. You likely need to rehash if you're a Csh (tcsh) user (but not again when you login).
6. Arguments are passed from the command line and referenced. For example, as $1.
#!/bin/sh
All Bourne Shell scripts should begin with the sequence
#!/bin/sh
"On the first line of an interpreter script, following the "#!", is the name of a program which should be used to interpret the contents of the file. For instance, if the first line contains "#! /bin/sh", then the con- tents of the file are executed as a shell script."
You can get away without this, but you shouldn't. All good scripts state the interpretor explicitly. Long ago there was just one (the Bourne Shell) but these days there are many interpretors -- Csh, Ksh, Bash, and others.
Comments
Comments are any text beginning with the pound (#) sign. A comment can start anywhere on a line and continue until the end of the line.
Creating a Script
find . -name file -print
and you'd rather type a simple command, say
sfind file
Create a shell script
% cd ~/bin
% emacs sfind
% page sfind
find . -name $1 -print
% chmod a+x sfind
% rehash
% cd /usr/local/bin
% sfind tcsh
./shells/tcsh
Observations
This quick example is far from adequate but some observations:
1. Shell scripts are simple text files created with an editor.
2. Shell scripts are marked as executeable
3. %chmod a+x sfind
4. Should be located in your search path and ~/bin should be in your search path.
5. You likely need to rehash if you're a Csh (tcsh) user (but not again when you login).
6. Arguments are passed from the command line and referenced. For example, as $1.
#!/bin/sh
All Bourne Shell scripts should begin with the sequence
#!/bin/sh
"On the first line of an interpreter script, following the "#!", is the name of a program which should be used to interpret the contents of the file. For instance, if the first line contains "#! /bin/sh", then the con- tents of the file are executed as a shell script."
You can get away without this, but you shouldn't. All good scripts state the interpretor explicitly. Long ago there was just one (the Bourne Shell) but these days there are many interpretors -- Csh, Ksh, Bash, and others.
Comments
Comments are any text beginning with the pound (#) sign. A comment can start anywhere on a line and continue until the end of the line.