Sorry auto markdown to html script did not work as expected. Here is them complete post.
Locating file using locate
If you know the name of the file (or a pattern of it), searching is as easy as
the command `locate filename`. One has to install `mlocate` or equivalent
program (on most of system it is already install).
This is very helpful when you want to figure out if a library or header file is
available on your system. You should update your database once in a while by
using `sudo updatedb` command. You can automate it using crontab.
For example if
you want to update your database everyday at 1400Hrs, following would do the job
Now you can search quickly for a filename using locate command in updated
database. If database is not up-to-date then this command might not give you the
location of file.
# Using grep
Let's say I want to check a particular boost header is installed on my machine.
If I say `locate boost`. This usually prints a lot of names, we can "pipe" its
output to `grep`. Command grep is a powerful command. It filters those files
which does not match with grep input.
locate boost | grep graph
(i.e. search for boost but drop all names that do not contain the word
graph).
Try it with without grep and then with grep.
# Finding files
On server machines where the admin may or may not have installed/updated locate
database, one can use the powerful `find` command. Command `info find` or `man
find` will open up its info and manual page in terminal. This command can take
many arguments and they are not always easy to remember. It is always useful to
create small scripts for specific purposes. Store those scripts on some online
repositories so that they can be downloaded easily later anytime. I keep mine on
github (
http://github.com/dilawar/Scripts).
A typical example of find: I want to search a header file which I want to
include in my C program. I am planning to search it in `/usr/include` folder, if
not found I'll try to search in `/usr` directory.
find /usr/local -name "*graph.h"
One can also pass `-type f` or `-type d` to search only files or directory
respectively.
One often uses find command with `xargs` command. Files searched by `find`
passed to another command. One example, let's say I want to delete all those
files which are updated in previous 10 minutes.
find /home/dilawar/.config -mmin 10
This command will print name of files inside directory `/home/dilawar/.config`
which are updated in last 10 minutes. We can pipe the names of these files to
some other command e.g. `rm`.
find /home/dilawar/.config -mmin 10 | xargs -I file rm -f file
Each line of first command is copied into variable file (-I file) and passed
onto the `rm -f file` command which removes the file. (See what -print0 options
does).
#
Using find with vim[This script](
http://github.com/dilawar/Scripts/v) uses find and git to search
for a file, creates its backup and open the file in vim. The default backup
directory is `~/.backup` (it is a local git repository). Each change is
committed to this directory. This is sometimes useful if you are working outside
a version control system. Let's say I have source code in a directory `A` and I
know there should be a file which has word `srcinfo` in its name. I do the
following
v *srcinfo*
This will search for files recursively and opens 3 best matches in separate
tabs. One can modify it (change vim to emacs or nano or whatever).
#
References
- Use `info` command to read the info pages. They are great.
- Checkout The Linux Documentation Project (TLDP) where great many tutorials are
written. Various stackexchange sites are great places also for specific
question.
-
Dilawar