Skip to main content

Linux simple find samples

Find stays one of the most confusing Linux commands around. Here I want to simplify the most common ways of using find.

There is many an article out there, but I think they all go into to much detail. I just want to find some files, and do something with them. Also I cannot believe that people show samples using the rm command. What the?

Do you want n00bs to delete files from there servers?

All mine, I'm using ls, so nothing should go wrong.


Also be careful not to test from your / folder, you will get weird errors and it will fall over when hitting stuff like your /dev/ and /proc/ folders. Start small.

1. Find a file:

find /mnt/some/where -type f -name "FILENAME"
and
find /mnt/some/where -type f -iname "FILENAME"

The first will match the filename exactly, and the second will match exactly ignoring the case. Both these will return the full path to the matched file(s). Adding an * into the filename will find partial filenames.

find /mnt/some/where -type f -name "FILENAM*"
and
find /mnt/some/where -type f -iname "*ILENAM*"

2. Do something with the file:

Here you have many options, and all confusing. Look at these two samples:

find /mnt/some/where -type f -iname "*ILENAM*" | xargs ls -al;
and
find /mnt/some/where -type f -iname "*ILENAM*" -exec /bin/ls -al '{}' \;

Both will yield the same result. Please note the "\;" and the end of the -exec command to show the executed ls command it ends. The xargs will add the output from find to the end of the piped (|) command. The '{}' sets where you want to put the command called in the -exec section.

In both cases the ls will run for each returned result from find.

3. Complex do something:

The result can be passed multiple times using the -exec command in find. The problem however is that sometime the execution runs incorrectly. Have a look at this sample:

find /mnt/some/where -type f -iname "*ILENAM*" -exec /bin/ls -al $(basename '{}') \;

This is summuse to return a file not found error as we are only running the ls for the filename part of the returned find. You however still get a ls on the full name.

find /mnt/some/where -type f -iname "*ILENAM*" -exec bash -c '/bin/ls -al $(basename "{}")' \;

This one works correctly. Please note the use of ' to set the bash command string.

References:

Mommy, I found it! — 15 Practical Linux Find Command Examples - http://www.thegeekstuff.com/2009/03/15-practical-linux-find-command-examples/
A Unix/Linux “find” Command Tutorial - http://content.hccfl.edu/pollock/unix/findcmd.htm

Comments

Popular posts from this blog

Double Bunk in Caravan

With our family expanding, we faced a challenge of adding another berth in our caravan, but I did not want to make it permanent.
A couple of options are available, of which one is simply to add a bunk to the single berth. We however did not want to cause any permanent markings on the interior of the caravan .

Netdata + SNMP + Mikrotik

Always wanted to see my Internet line usage as a gauge without having to log into the router.
So today I configured SNMP in Netdata to collect from my Mikrotik router.
/etc/netdata/node.d/snmp.conf:
{
"enable_autodetect": false,
"update_every": 5,
"max_request_size": 100,
"servers": [
{
"hostname": "10.1.1.1",
"community": "public",
"update_every": 5,
"max_request_size": 50,
"options": { "timeout": 20 },
"charts": {
"mikrotik1.cpu": {
"title": "CPU ",
"units": "percentage",
"type": "line",
"family": "cpu",
"dimensions": {
"used": {
&qu…

CasparCG Simple Playlist

It is possible to build a simple playlist as a rundown in the CasparCG client. It will very simply play each item after each other that are on the same layer. In this very simple post I show how to build such a playlist including the very important step of activating the OSC.

The Open Sound Control (OSC) implementation is how the client knows what the server is doing and then being able to send new command back to the server when a piece of media has ended to trigger playback of the next piece. For more details on the OCS, please see http://casparcg.com/wiki/CasparCG_OSC_Protocol