Using grep

This is the nanobanshee evil dirty searching howto. It will teach you how to
find what you are looking for to make linux run.

Most of this howto consists of commands that stream tons of text to the
terminal.  Add a "| less" to the end of a command if you want to be able to
scroll the output and search it. Also, if the output scrolls on the terminal
but doesn't show up in less, that is because it is going on the error channel,
so you need to add a "2>&1 | less" intead.  For more information on this, type
"man bash" and use / to search the manual page.  The / command in less uses
the same syntax as grep.

First, the main tools: grep, find and google.

Find In Files:
find / -type f -exec grep -i pattern '{}' \; -print    or    grep -r "STRING THAT YOU WANT" *
An example would be:
find / -type f -exec grep -i "my string" '{}' \; -print

The google interface is easy: you type what you want into the box. (I will
extend this section as I think of how to explain the fine art of googling)
grep is easy also. It matches strings in lines of text. All that you need to
know about it for now is contained in this example:

"^.*foo.*bar.*$"  will match "foo" anywhere in the line followed by "bar"
anywhere in the line.

".*" matches anything.

Characters and numbers match themselves.

"^" matches the beginning of the line and

"$" matches the end.

Special characters sometimes need to be preceded by \ and sometimes not
(use trial and error to find out which ones).

More examples:

^foo will match "foo" only if it is at the beginning of the line.

foo$ will match "foo" only if it is at the end of the line.


The command-line use of grep is as follows:
grep -n -r thing filename(s)


The -n and -r are optional.
-n will tell you which line numbers match.
-r will dive into directories and search all the files inside.

In addition, grep can be used in a stream by omitting the filename
and adding a | to the front, like this:

    | grep thing | less

       or

       2>&1 | grep thing | less

The find command has a totally different syntax and usage. To find a file with
the string "foo" in the name located somewhere in the /bar directory, do:

find /bar -name "*foo*"

That's it.  Now it is time to explain where information is located on linux
and how to access it and search it to fix things.  A good place to get a
general overview of where things are is the filesystem heirarchy
standard, located at:

      http://www.pathname.com/fhs/pub/fhs-2.3.html


First, we will start with hardware and the kernel, and then move to other
areas and tasks.  The interface that software uses to interact with the
hardware has been standardized so that any piece of software can run on any
computer. The one special piece of software that mediates the interaction
between all other software and the hardware is the kernel.  If you have a
hardware device like a webcam or external usb hard disk that you want to
use with linux, the kernel is the place to start.

First, you need to know what hardware you have:

      cat /proc/cpuinfo

      cat /proc/asound/cards

      cat /proc/bus/usb/*

      lsusb

      lspci

      lshw

      hdparm -iI /dev/hda


All of these are greppable. To find what firewire device you have:

      lspci | grep 1394

The kernel configuration file can be found in a number of places:

      /proc/config.gz

      /boot/config* on debian

      /usr/src/linux/.config if you made your own.

The format of the file is lots of lines containing CONFIGfoobar and either "not
set", "y" or "m".  "not set" and "y" mean that kernel support for that piece
of hardware is either enabled or not. "m" stands for module and means that
it has been enabled, but needs to be turned on manually to work.

To find out what filesystems your kernel supports:

      zgrep FS /proc/config.gz | less

      To find out if ppp support is enabled:

      zgrep PPP /proc/config.gz | less

Also, while building the kernel, in make menuconfig, you can search by hitting "/".

While it is running, the kernel spits out lots of messages to either

      /var/log/dmesg or /var/log/kmsg-kern.log

To find out what ethernet cards were detected at boot:

      grep eth /var/log/dmesg

To watch the kernel detect a usb thing as you plug it in:

      watch "dmesg | tail"

To see what IDE disks were detected:

      dmesg | grep hd

To see what SCSI or usb disks were detected:

      dmesg | grep sd

      To see what kernel modules are loaded:

      lsmod

For setting up system services such as networking interfaces, web servers, usb
hotplugging support, etc, the two places to look are the /etc scripts and the
system log file /var/log/messages or /var/log/syslog.

To look for configuration things related to bluetooth:

      grep -r bluetooth /etc

      find /etc -name *bluetooth*

Things related to ethernet networking:

      find /etc -name "*eth*"

      grep -r eth0 /etc | less

To fix a dhcp problem:

      grep dhcp /var/log/syslog

To fix a login problem:

      grep pam /var/log/syslog

Another big problem area is setting up Xorg.

For framebuffer kernel support:

      zgrep FB /proc/config.gz

AGP and DRM support for direct rendering:

      zgrep AGP /proc/config.gz

      zgrep DRM /proc/config.gz

To check what Xorg modules are loaded by the X server:

      grep Load /etc/X11/xorg.conf

To check font paths:

      grep FontPath /etc/X11/xorg.conf

To list all the drivers used:

      grep Driver /etc/X11/xorg.conf

To look at all the options:

      grep Option /etc/X11/xorg.conf

To see if direct rendering is working:

      glxinfo | grep irect

And the most useful of all, to check for Xorg errors:

      grep EE /var/log/Xorg.0.log

For sound:

      grep -r snd /etc

      find /etc -name "*alsa*"

Other useful commands are alsaconf, amixer, alsamixer.
Also, to use a foo OSS app on an ALSA system, get the alsa-oss package and type
"aoss foo".

Lots and lots of greppable stuff is in /proc/asound:

      /proc/asound/version

      /proc/asound/pcm

      /proc/asound/cards

      /proc/asound/devices

For file errors, strace and lsof are the two commands to use. If you know the
file, but not the application with the problem, use lsof.

      To list every shared object file currently in use:

      lsof | grep "\.so" | less

To find out which application is using /dev/dsp:

      lsof | grep /dev/dsp

If you know the command, but not the files it accesses, use strace to find the
files that a command foo uses:

      strace foo 2>&1 | grep ^open

Try it on this example:

      strace touch a 2>&1 | grep ^open

For "can't find library" errors, use:

      ldd `which foo`| less

to get a list of the needed libraries.  The config files to edit are:

      ld.so.conf and ld.so.conf.d/*

Use those files to set the search paths for libraries, then run:

      ldconfig -v | less

and make sure it finds what it needs. If you don't know where it is, find it
with:

      find / -name libfoo.so*

      and add the path to ld.so.conf.

Networking stuff:

To see if a network interface is up and has an ip address:


      watch "ifconfig | grep addr"

To see what applications have open TCP/IP connections:

      netstat -n -p | grep ^tcp

Building:
      When compiling, to see the first error that needs to be fixed:

      cat gcclog.txt | grep ": error: " | head -n1