Two Cent Tips

January 1st, 1997 by Marjorie Richardson in

Newbie Tip on Finding
Your rating: None

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 Murphymurphyc@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. Hammelmjhammel@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, VE3IEDbduncan@ve3ied.uucp

__________________________


Special Magazine Offer -- Free Gift with Subscription
Receive a free digital copy of Linux Journal's System Administration Special Edition as well as instant online access to current and past issues. CLICK HERE for offer

Linux Journal: delivering readers the advice and inspiration they need to get the most out of their Linux systems since 1994.

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
Anonymous's picture

bash/ksh using less command

On July 2nd, 2008 Anonymous (not verified) says:

!/bin/bash
find / -iname $1 -mount -print |less

doesn't works on both my suse sles 9 and 10:
it displays the following message:
<<>>

(the same if I replace "less" with "more")

amanda lear enigma mp3's picture

linux

On October 2nd, 2007 amanda lear enigma mp3 (not verified) says:

ways Web on other Are search information particular to Linux issue? a the there for kernel . Bye.

Post new comment

Please note that comments may not appear immediately, so there is no need to repost your comment.
The content of this field is kept private and will not be shown publicly.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <pre> <ul> <ol> <li> <dl> <dt> <dd> <i> <b>
  • Lines and paragraphs break automatically.

More information about formatting options

Newsletter

Each week Linux Journal editors will tell you what's hot in the world of Linux. You will receive late breaking news, technical tips and tricks, and links to in-depth stories featured on www.linuxjournal.com.
Sign up for our Email Newsletter

Featured Videos

Setting up an https server in Apache is easy. This tutorial covers how to create and sign your ssl certificate as well as how to configure the web server.

From the Magazine

January 2009, #177

It's a battle as old as time: good vs. evil. Fortunately, Linux and FOSS are on our side as we wage the battle against those who try to steal our secrets and invade our systems.

Checking your system's security is best done sooner rather than later. Test the locks with our article on security verification; find out how to use PAM to help secure your systems; use MinorFS and AppArmor to implement discretionary access control; learn more about Samba security in part III of our series; use Darknet to help detect bots and secure your systems; use the Yubikey to increase your site's security; and don't forget to lock the doors, because a cold boot attack could render your security useless if somebody has physical access to your computer.

But, we're not just about sowing the seeds of fear. We also show you how to use memcached in Rails, how to manage multiple servers efficiently, how to deploy applications easily with Capistrano, how to manage your videos with MythVideo, how to mix it up a bit (your audio that is), and even play a few games.

Read this issue