Saturday, October 3, 2009

Finding in Linux (bash shell)

I'd like to put in the list some useful commands for finding things in Linux environment.

  1. apropos keywords - searches through manual descriptions for the keywords. Say, we are interested in commands to find anything:

    $ apropos find search look locate match

    what gives out a list of related commands with short helpful description. On my Mepis 8.0 virtual machine it shows the following lines among others:

    apropos (1) - search the manual page names and descr...
    look (1) - display lines beginning with a given string
    find (1) - search for files in a directory hierarchy
    grep (1) - print lines matching a pattern
    kfind (1) - KDE find tool
    locate (1) - list files in databases that match a pat...
    rgrep (1) - print lines matching a pattern
    whereis (1) - locate the binary, source, and manual pa...
    which (1) - locate a command

    apropos will work only if manuals are installed - usually they are, but sometimes for the sake of resource saving manuals can be omitted.

  2. find is a very powerful command for searching files in directories:

    $ find startdir -name filename

    Different options allow to search for files by name, size, type, permission, owner, etc. - check manual and help:

    $ man find
    $ find --help

  3. grep searches for a pattern inside text file:

    grep "pattern" filename

    Example of finding files and then find a pattern inside the files:

    $ find /home/svn -name "test*txt"|xargs grep "first" -i
    The first line of the test file.

    There are some variations of grep as fgrep, rgrep, egrep, bzgrep, etc. - check it with "apropos grep".

  4. kfind is a GUI version of find with grep - it can search for files and pattern inside files.

  5. look prints out strings beginning with a pattern from a file:
    $ look pattern filename

  6. strings prints out all strings from a text or binary file:
    $ strings filename

  7. whereis locates different type of files. For instance:
    $ whereis find
    find: /usr/bin/find /usr/share/man/man1/find.1.gz

  8. locate searches for files in databases (it gives out all files with names containing given keyword):
    $ locate find

  9. which search for executables and shows up the whole path to directories where the files are found:
    $ which find


Post a Comment