return to first page linux journal archive
keywordscontents

Linux Gazette

Two Cent Tips

Newbie Tip on Finding

As a new Linux user, I discovered that I had a hard time finding my way around the file system. I knew the find command was out there, but remembering all the options required to make it search the right places, find the right files, and print the right answers was something I couldn't do, at first. So I made up my own command, using a shell script, and called it fnd.

fnd takes one argument, the name of the file you want to locate, complete with any wildcards you may wish to include, and pipes its output to less, which then allows you to view a large list of results. What you get, on each line of output, is the complete path to any file that you're looking for. I find it amazingly useful (as is a rough familiarity with the less command). Here's my script:

!/bin/bash
find / -iname $1 -mount -print |less 
That's it. The -iname option tells find to be case insensitive, the $1 is a variable that substitutes in your command line argument, -mount tells find not to search directories on other file systems like your CD-ROM (because mine is wonky and locks up the machine if it is accessed). The -print option is required or you don't get any output. (Get used to it, it's *nix.) The | (pipe) symbol tells find to direct its output to the less command, so you can see your results in style. Don't forget the / right after the find command, or it won't know where to look. Enjoy. You won't regret the time you spend keying in this little shortcut, and don't forget to put it in a bin or sbin directory after using chmod to make it executable.
--
Jim Murphy
murphyc@cadvision.com

X Term Titlebar Function

In the mail from issue 9, Jim Murphy says that the -print option to find is necessary to get output from the find command, and follows that up with ``get used to it, it's *nix.'' Well, he's part right. Linux does require this. However, any users who work on other Unix boxes will find slight differences in some of the common CLI (Command Line Interface) commands. For example, find on Solaris does not require the -print option to get output. Just food for thought.

Second, I have an xterm title bar function that people might find useful. I'll give the code first, then explain what it does.

In your .bashrc (or .kshrc--note this only works on ksh style shells) add the following:

HOSTNAME=`uname -n` 
if [ "$TERM" = x"term" ] && [ "$0" =
"-bash"] 
then<\n> 
  ilabel () { echo -n "^[]1;$*^G"; }
  label () { echo -n ^"[]2;$*^G"; }
  alias stripe='label $HOSTNAME - ${PWD#$HOME/}'
  alias stripe2='label $HOSTNAME - vi $*'
  cds () { "cd" $*; eval stripe; }
  vis () { eval stripe2; "vi" $*; eval stripe;}
  alias cd=cds
  alias vi=vis
  eval stripe
  eval ilabel "$HOSTNAME"
fi
This does three things (as long as you're in an xterm and running bash):

  1. When the xterm is first opened, the name of the current host is displayed in the title bar.
  2. When you change directories (using cd), the current path is displayed in the xterm title bar with the user's $HOME directory stripped off the front end of the path (to save some space when you're somewhere in your own directory tree). The path is preceded by the current host's network name.
  3. When you use vi to edit a file, the name of the file is displayed in the title bar along with the current host's name. When you exit your vi session, the title bar reverts to the hostname/path format described in #2 above.
I find this very useful for all my ksh-based systems, because it removed the path from my shell prompt, thus saving me space for prompt commands. Since bash is a ksh compatible shell, this works quite well on standard Linux systems.
--
Michael J. Hammel
mjhammel@csn.net

Find and Alternatives

Saw Jim Murphy's find tip in issue #9, and thought you might like a quicker method. I don't know about other distributions, but Slackware and Red Hat come with the GNU versions of locate(1) and updatedb(1), which use an index to find the files you want. The updatedb(1) program should be run once a night from the crontab facility. To ignore certain sub-directories (like your /cdrom), use the following syntax for the crontab file:

41 5 * * * updatedb --prunepaths="/tmp /var \
	/proc /cdrom" > /dev/null 2>&1
This command would run every morning at 5:41 AM, and update the database with file names from everywhere except the subdirectories (and those below) listed.

To locate a file, just type locate file name. The file name doesn't have to be complete; locate can also do partial matching. For me, the search typically takes only a few seconds, and I have tens of thousands of files.

The locate(1) command also has regular expression matching, but I often just pipe it through agrep(1) (a faster grep) to narrow the search. Thus:

locate locate | agrep -v man
would exclude the man page, and only show me the binary and the sources, if I had them on-line. (The -v flag excludes the pattern used as an argument.) To get the binary files alone, along with a complete directory listing, use the following command:

ls -l `locate locate | agrep bin`
The find(1) command is a great ``swiss-army knife'' (and actually not that bad once you get used to it), but for the 90% of the cases where you just want to search by file name, the locate(1) command is far faster, and much easier to use.
--
Bill Duncan, VE3IED
bduncan@ve3ied.uucp