Linux #4: Difference between find, locate and whereis

Difference between find, locate and whereis linux commands

find: finds files by filename. see Linux: Search for a File by Filename for more details and example usages.

locate: also finds files by filename but does not search the directory structure itself but only a database prepared by updatedb. Because of that locate is faster than find but less accurate. Examples:

locate firefox
# ->    /etc/firefox
#       /etc/firefox-3.0
#       /etc/firefox-3.5
#       /etc/alternatives/firefox-homepage
#       /etc/alternatives/firefox-homepage-locales
#       [ ... and many more results ... ]

locate a_file_that_i_just_created
# -> no results

whereis: finds source, binary and manual files by name. Examples:

whereis find
# -> find: /usr/bin/find /usr/share/man/man1/find.1.gz

whereis firefox
# -> firefox: /usr/bin/firefox /etc/firefox /usr/lib/firefox /usr/lib64/firefox /usr/share/firefox /usr/share/man/man1/firefox.1.gz

Which to use: find, locate or whereis

It really depends on what you want to do:
If you want to find a linux program (or its source or documentation) use whereis.
If you want to find an often-used file or want to perform a quick but not too accurate first search for a file use locate.
If you want to find one of your personally created files use find if you know approximately where to look or have a lot of time (searching for a file by name using find from the root takes some time). If you do not, use locate first as it is quicker. If it did not find the file you can still use find afterwards.

Symfony2 Flash Message

Flash Messages make it possible to show a string on the next request, for example after a redirect.
This is useful when you want to inform a user about the success or failure of form-requests.

Symfony2 changed how flash messages work from version 2.0 to 2.1, so below you can read about how to use flash messages in both versions.

Symfony2 flash message for Symfony 2.0 and below

Setting the flash message

To add a flash message – which lasts exactly one request – to a session use this code in the controller:

$this->get('session')->setFlash('notice', 'Form successfully executed!');
First we retrieve the Session Object and then set a flash message. notice is the name which you can set to anything you want and Form successfully executed! is the content of the message.

Retrieving and displaying the flash message

To display a message in the view, use the following twig code:

{% if app.session.hasFlash('notice') %}
    <div class="flash-notice">
        {{ app.session.flash('notice') }}
    </div>
{% endif %}
First we check if there is a flash message and if it exists we display it.

Flash messages for Symfony 2.1 and above

Setting the flash messages

In version 2.1 of Symfony2 not too much changed about flash messages. They are now stored in a so called flashbag which allows to store more than one message under a name:

$this->get('session')->getFlashBag()->add('notice', 'Form successfully executed!');
$this->get('session')->getFlashBag()->add('notice', 'Yes, really!');

Retrieving and displaying the flash messages by name

you can now display all flash messages that you added under the name notice:

{% for flashMessage in app.session.flashbag.get('notice') %}
    <div class="flash-notice">
        {{ flashMessage }}
    </div>
{% endfor %}

Retrieving and displaying all flash messages

It is also possible to iterate over all flash messages (stored under all names) and display them:

{% for type, flashMessages in app.session.flashbag.all() %}
    {% for flashMessage in flashMessages %}
        <div class="flash-{{ type }}">
            {{ flashMessage }}
        </div>
    {% endfor %}
{% endfor %}

Domain Name Tools: Name suggestions, Price comparison and more

Here are a couple of tools that will help you choose and register a domain name:

domain naming tipps: A list of things to consider when choosing a domain name.

domain name suggestions generator: In case you need a good domain name idea this tool will generate a list of possible domain names on the basis of keywords provided by you and check if they are available.

compare domain name prices: Compare the prices of the top domain name registrars and filter it by price, top level domain, available payment methods and by whether or not anonymous domain name registration is possible. This will help you find the best domain name registrar for you.

domain name availability check: Test if your desired domain name is available.

If for some reason you do not like my domain name tools, check out this tool list or this one. Of course, (productive) criticism in the comments is always appreciated.

Linux #3: Search and Replace

Search and Replace text in one File

To search a string in a file and replace all occurrences of it with a different string use sed:

sed -i 's/string/replace/g' file

With this command all occurrences of the text string in the file file will be replaced with replace.

Search and Replace text in all Files in a directory

To replace all occurrences of a string in all files in a directory recursively use sed in combination with find:
find /directory -type f -exec sed -i 's/search/replace/g' {} \;

This post is only meant as a quick reference for those that sometimes forget the syntax for search and replace (me for example. Especially the combination of sed and find is quite long and I never bothered to remember it as I do not use it that often). If you want to know more about sed read this in depth sed tutorial.

Low Adsense link unit coverage

My adsense link unit coverage is low. A lot lower than the coverage for the other adsense ad units so I tried to figure out what is going on.

Google has this to say:
For link units, coverage reflects the number of link units that showed ads after the user clicked on a term in the link unit. Coverage for pages displaying link units is typically much lower than for the other kinds of ad units, because it involves user clicking. This also means you’ll see a higher number for unmatched ad requests.

Well, that is not very concrete at all. What they means is:

link unit coverage = actual coverage * first click through rate of link unit
where
actual coverage = Ad requests that returned ads / total ad requests
(how coverage is defined for all other adsense ads)
and
first click through rate of link unit = rate of first click on link unit on your website

So if you assume that the actual coverage of your adsense link units is about the same as the coverage of all your other adsense ads you can easily calculate how often visitors clicked on adsense link units.

This is important to know because you can see how often users only clicked once, but did not click on any ads after that. If this happens too often for some adsense link units think about replacing them with actual ad units to increase revenue. Although expect a slight drop in clicks when doing this as users tend to click more often on adsense link units than on ad units.