UNIX For Dummies [Electronic resources] نسخه متنی

اینجــــا یک کتابخانه دیجیتالی است

با بیش از 100000 منبع الکترونیکی رایگان به زبان فارسی ، عربی و انگلیسی

UNIX For Dummies [Electronic resources] - نسخه متنی

John Levine, Margaret Levine Young

| نمايش فراداده ، افزودن یک نقد و بررسی
افزودن به کتابخانه شخصی
ارسال به دوستان
جستجو در متن کتاب
بیشتر
تنظیمات قلم

فونت

اندازه قلم

+ - پیش فرض

حالت نمایش

روز نیمروز شب
جستجو در لغت نامه
بیشتر
لیست موضوعات
توضیحات
افزودن یادداشت جدید






The Search Is On

UNIX systems have lots of files. Lots and lots. Tens of thousands, to be more specific. So where’s the memo you wrote last week?








Technical Stuff Links to shadow files


You may run into a situation in which a file seems to be in several directories at one time (Twilight Zone music here, please). Mac users ought to be thinking of aliases here; Windows users ought to be thinking of shortcuts. UNIX has its own way of letting you keep a file in several places at the same time. To avoid excessive clarity, the file can even have several different names. Seriously, having a file in, for example, the home directories of several people at one time so that they all can easily share it is useful.

To achieve this magical feat, you use links. We discuss links in the section “A File by Any Other Name,” later in this chapter. In the meantime, don’t panic if you see a file lurking around in one place when you’re sure that it belongs somewhere else.












Peering into every directory


The first approach to finding a lost file is to use the brute-force method. Starting in your home directory, use ls to search through each of your directories. In every directory, type this line:


ls important.file

Replace important.file with the name of the file you’re looking for. If the file is in the current directory, ls lists it. If the file isn’t there, ls complains that it can’t find the file. This approach can take awhile if you have a large number of directories. An additional drawback is that you won’t find the missing file if it has wandered off to someone else’s directory.

If you know — or think that you know — that your file is nearby, you can use * (asterisk) wildcards in directory names. (Wildcards are covered in Chapter 7. They enable you to work with lots of files or directories at one time.) To find important.file in any of the subdirectories in the working directory, type this line:


ls */important.file

This technique doesn’t work if you have directories within directories: It looks only one level down.


“Hey, I know the filename!”


With luck, you know the name of the file you have lost. If so, you can use the find program to find it. When you use find , you tell it the name of the file and the place to start looking. The find program looks in the directory you indicate and in all that directory’s subdirectories.

Suppose that you’re working in your home directory. You think that a file named tiramisu is in there somewhere. Type this line:


find . -name tiramisu -print

That is, you type these elements:



find (just like you see it here).



A space.



The directory in which you want the program to begin looking. If it’s the working directory, you can type just a period (which means “right here”).



Another space.



-name (to mean that you will specify a filename).



Another space.



The name of the file you want to find (tiramisu, in this case).



Another space.



-print to tell UNIX to print (on-screen) the full name, including the directory name, to let you know where UNIX finds the file. If you omit this step and find finds the file, it doesn’t tell you. (We know that this situation is stupid, but computers are like that.) If you use UNIX SVR4 or Solaris, you’ll notice the find command is fixed so that it warns you rather than runs the command pointlessly.



Tip The find program uses a brute-force approach to locate your file. It checks every file in all your directories. This process can take quite a while. After find finds the file, it prints the name and keeps going. If the program finds more than one file with that name, find finds them and reports them all. After find prints a found file, you usually will want to stop the program (unless you think that it will find more than one match). You stop find by pressing Ctrl+C or the Delete key.
If the find command doesn’t work and you think that the file may be in some other user’s directory, type the same find command and replace the . (dot) with a / (slash). This version tells find to start looking in the root directory and to search every directory on the hard drive. As you can imagine, this process can take some time, so try other things first.


“I know where to search (sort of)”


Rather than use a period to tell find to begin looking in the working directory, you can use a pathname. You can type this line, for example:


find /usr/margy -name tiramisu -print

This command searches Margy’s home directory and all its subdirectories. (Her home directory name may be something different; see Chapter 6 to find out about home directories.) To search the entire disk, use the slash ( / ) to represent the root of the directory tree:


find / -name tiramisu -print

If your disk is large and full of files, a search from the root directory down can take a long time — as long as half an hour on a very large and busy system.

You can even type several directories. To search both Margy’s and John’s home directories for files named white.chocolate.mousse , for example, type this line:


find /usr/margy /usr/johnl -name white.chocolate.mousse -print

Tip If you use the BASH or C shell, rather than type the home directory name, you can type a tilde (~) and the username; the shell puts in the correct directory name for you:


find ~margy ~johnl -name white.chocolate.mousse -print


“At least I know part of the filename”


You can use wildcard characters in the filename if you know only part of the filename. (Remember the * and ? characters that act as wildcards in filenames?) Use ? to stand for any single character; use * to stand for any bunch of characters. There’s a trick to using wildcards, however: If you use * or ? in the filename, you have to put quotation marks around the filename to keep the shell from thinking that you want it to find matching names in only the current directory.
You can search the entire disk for files that start with budget , for example, by typing

find / -name "budget*" -print

If you leave out the quotation marks, the search may look like it worked, although find probably hasn’t done the job correctly.


Remote searches


Network If your system uses NFS (Network File System, as described in Chapter 16), some or all the directories and files on your machine may really be on other computers. The find command doesn’t care where files are and cheerfully searches its way into any directory it can get to. Because getting to files over a network is about half as fast as getting to files stored locally, telling find to look through a large number of files stored on a network can take a long time. Consider having a long lunch while find does its thing.

Suppose that you’re looking for Dave’s famous stuffed-squid recipe. The obvious way to look for it is with this line:


find ~dave -name stuffed-squid -print

If you know that Dave’s files are stored on machine xuxa , however, this command can be much faster:


ssh xuxa "find ~dave -name stuffed-squid -print"

See Chapter 16 for details about the ssh command.


It’s what’s inside that counts


“Hmm . . . I don’t remember what the file is called, but I’m looking for a letter I wrote to Tonia, so it should contain her mailing address in the heading. That’s 1471 Arcadia. How do I find it?”

This situation is made for grep — a great program with a terrible name. It stands for, if you can believe it, global regular expression and print, or some such thing. The grep command looks inside files and searches for a series of characters. Every time it finds a line that contains the specified characters, it displays the line on-screen. If it’s looking in more than one file, grep also tells you the name of the file in which the characters occur. You control which files it looks in and which characters it looks for.
Three grep programs exist:

grep , egrep , and fgrep . They are similar, so we talk just about grep . (Fgrep is faster but more limited, and egrep is more powerful and more confusing.) To look in all the files in the working directory (but not in its subdirectories) for the characters 1471 Arcadia , type this line:


grep "1471 Arcadia" *

That is, type these elements:



grep (just as you see it here).



A space.



The series of characters to look for (also called the search string). If the string consists of several words, enclose it in quotation marks so that grep doesn’t get confused.



A space.



The names of the files to look in. If you type * here, grep looks in all the files in the current directory.



The grep program responds with a list of the lines in which it finds the search string:

ts.doc: 1471 Arcadia Lane
tonia.letter: 1471 Arcadia La.


The program lists the name of the file and then the entire line in which it finds the search string.

You can do lots of things with grep other than look for files. In fact, one could write entire (small) books about using grep . For our purposes, however, here are some useful options you can use when you use grep to look for files.

If you want to see just the filenames and you don’t want grep to show you the lines it found, use the -l (for list) option. (That’s a small letter l, not a number one.) Suppose that you type this line:


grep -l "1471 Arcadia" *

The grep program responds with just a list of filenames:

ts.doc
tonia.letter

It may be a good idea to tell grep not to worry about uppercase and lowercase letters. If you use the -i (for ignore case) option, grep doesn’t distinguish between uppercase and lowercase letters, as shown in this example:


grep -i DOS *

With this command, grep , which is extremely literal-minded, finds both references to DOS and some "false hits":

fruit.study: salads; in Brazil, avocados are used in desserts.
chapter.26: DOS vs. UNIX
chapter.30: Dos and Don’ts
Finally, if you don’t know the exact characters that occur in the file, you can use grep ’s flexible and highly powerful (that is, cryptic and totally confusing) expression-recognition capabilities, known in nerdspeak as regular expressions. The grep program has its own set of wildcard characters, sort of but not much like the ones the shell uses to enable you to specify all kinds of amazing search strings. If you’re a programmer, this feature is useful because you frequently need to find occurrences of rather strange-looking stuff.

The reason we mention this subject is that grep ’s wildcard characters include most punctuation characters — namely:

. * [ ] ^ $








Tip Quick ‘n’ dirty database


You can use grep to treat a text file like a quick and dirty database. Using a text editor, for example, you can create a file named 411 that contains the names and phone numbers of your friends and associates, with one entry per line:

Jordan Young, 555-4673
Meg Young, 555-5485
Zac Young, 554-8649
To look up someone’s phone number, you just type
grep Meg 411

The grep program displays the line or lines of the file containing the name or names you asked for.

The grep program is widely used by UNIX enthusiasts for searching all kinds of files for all kinds of information. As long as each item fits on one line, you can keep all sorts of data in this kind of cheap database file. One of our favorite files is called restaurants , which has lines that look like this:

Chef Chung’s Cheap 555-3864
If you’re in the mood for something cheap, you can say
grep -i cheap restaurants


















Directory assistance


You can look for lost directories in addition to lost files. Give the find command the option -type d :


find / -name "Budget*" -type d -print

This command searches the entire hard drive for directories that begin with Budget .











If you include any of these characters in a search string, grep doesn’t do what you expect. To type any of these characters in a search string, precede them with a backslash ( \ ). To search for files containing C.I.A., for example, type this line:


grep "C\.I\.A\." *

The period (. ) is grep ’s wildcard character, like the question mark (? ) in the shell. In this example, if you don’t precede the periods with backslashes, grep matches not only C.I.A . but also CHIFAS (a Peruvian dialect word meaning Chinese restaurants, in case you are wondering) and lots of other things. Don’t press your luck — use the backslashes with punctuation marks to be safe.

/ 213