Site Administration Dealing with Files and Filesystems |
This chapter also addresses some useful filesystem manipulations. Have you ever inadvertently blown away a portion of your directory structure? Would you like to manipulate /tmp or your swap partition? Do your Unix systems need to play nicely with Microsoft systems? Might you consider ghosting your BSD system? If so, this chapter is for you. HACK#13: Find ThingsFinding files in Unix can be an exercise in frustration for a novice user. Heres how to soften the learning curve. Remember the first time you installed a Unix system? Once you successfully booted to a command prompt, I bet your first thought was, Now what? or possibly, Okay, where is everything? Im also pretty sure your first foray intoman findwasn't all that enlightening. How can you as an administrator make it easier for your users to find things? First, introduce them to the built-in commands. Then, add a few tricks of your own to soften the learning curve. Finding Program Paths Every user should become aware of the threew's:which,whereis, andwhatis. (Personally, I'd like to see somewhyandwhencommands, but that's another story.) Usewhichto find the path to a program. Suppose youve just installedxmmsand wonder where it went: % which xmms Better yet, if you were finding out the pathname because you wanted to use it in a file, save yourself a step: % echo `which xmms` >> somefile Remember to use the backticks (`), often found on the far left of the keyboard on the same key as the tilde (~). If you instead use the single quote (') character, usually located on the right side of the keyboard on the same key as the double quote ("), your file will contain the echoed stringwhich xmmsinstead of the desired path. The user's current shell will affect howwhich's switches work. Here is an example from the C shell: % which -a xmms This is a matter of whichwhichthe user is using. Here, the user used thewhichwhich is built into the C shell and doesn't support the options used by thewhichutility. Where then is thatwhich? Try thewhereiscommand: % whereis -b which Here, I used-bto search only for the binary. Without any switches,whereiswill display the binary, the manpage path, and the path to the original sources. If your users prefer to use the realwhich command instead of the shell version and if they are only interested in seeing binary paths, consider adding these lines to /usr/share/skel/dot.cshrc [Hack #9]: alias which /usr/bin/which -a The-aswitch will list all binaries with that name, not just the first binary found. Finding Commands How do you proceed when you know what it is that you want to do, but have no clue which commands are available to do it? I know I clung to thewhatis command like a life preserver when I was first introduced to Unix. For example, when I needed to know how to set up PPP: % whatis ppp On the days I had time to satisfy my curiosity, I tried this variation: % whatis "(1)"That will show all of the commands that have a manpage in section 1. If youre rusty on your manpage sections, whatis intro should refresh your memory. Finding Words The previous commands are great for finding binaries and manpages, but what if you want to find a particular word in one of your own text files? That requires the notoriously user-unfriendlyfindcommand. Lets be realistic. Even with all of your Unix experience, you still have to dig into either the manpage or a good book whenever you need tofindsomething. Can you really expect novice users to figure it out? To start with, the regular old invocation offindwill find filenames, but not the words within those files. We need a judicious use ofgrepto accomplish that. Fortunately,finds-execswitch allows it to use other utilities, such asgrep, without forking another process. Start off with afindcommand that looks like this: % find . -type f -exec grep "word" {} \; This invocation says to start in the current directory (.), look through files, not directories (-type f), while running thegrepcommand (-exec grep) in order to search for the wordword. Note that the syntax of the -execswitch always resembles: -exec command with_its_parameters {} \; What happens if I search the files in my home directory for the wordalias? % find . -type f -exec grep "alias" {} \; While it's nice to see thatfindsuccessfully found the wordaliasin my home directory, theres one slight problem. I have no idea which file or files contained my search expression! However, adding /dev/null to that command will fix that: # find . -type f -exec grep "alias" /dev/null {} \; Why did adding nothing, /dev/null, automagically cause the name of the file to appear next to the line that contains the search expression? Is it because Unix is truly amazing? After all, it does allow even the state of nothingness to be expressed as a filename. Actually, it works becausegrep will list the filename whenever it searches multiple files. When you just use{},findwill pass each filename it finds one at a time togrep. Sincegrepis searching only one filename, it assumes you already know the name of that file. When you use/dev/null { },findactually passesgrep two files, /dev/null along with whichever filefindhappens to be working on. Sincegrepis now comparing two files, its nice enough to tell you which of the files contained the search string. We already know /dev/null wont contain anything, so we just convincedgrepto give us the name of the other file. That's pretty handy. Now let's make it friendly. Heres a very simple script calledfstring: % more ~/bin/fstring#!/bin/sh # script to find a string # replaces $1 with user's search string find . -type f -exec grep "$1" /dev/null {} \; That$1is a positional parameter. This script expects the user to give one parameter: the word the user is searching for. When the script executes, the shell will replace"$1"with the users search string. So, the script is meant to be run like this: % fstring word_to_searchIf you're planning on using this script yourself, youll probably remember to include a search string. If you want other users to benefit from the script, you may want to include an if statement to generate an error message if the user forgets the search string: #!/bin/sh Don't forget to make your script executable withchmod +xand to place it in the user's path. /usr/local/bin is a good location for other users to benefit. See Also
blog comments powered by Disqus |
|
|
|
|
|
|
|