8 Steps to Becoming A `find` Fu Master — Pt 2

February 6th, 2009

(This is the second part in a three part series. Review the Introduction Here)

Now for the second installment of your `find` fu mastery! We are going to cover four principles today:

  1. Learning to use object types
  2. Using command execution
  3. Using `find` to delete files
  4. Limiting search depth

With no further ado,

1. Using object types in `find`

In the Linux/Unix filesystems there are more things than plain old regular files. Among then are symbolic links, directories, named pipes, block devices, character devices, and sockets. `find` will let you specify these items.

For instance, if I am looking for the baseq3 directory of my Quake3 installation, I can do the following:

find / -type d -name 'baseq3'

If I am looking for all symbolic links in a directory, I can do the following:

find . -type s

If I am looking for only files, I can specify ‘-type f’. This is handy by itself, but becomes more useful as you are executing commands for each of your search results. Which brings us to…

2. Using Command Execution

This is probably one of the most useful things that `find` has to offer. This is also one of the easiest ways to screw up your system and/or your day’s work. You have to be careful with this. Fortunately, `find` puts options in to help with that. We’ll cover those in a moment after we give some basics of command execution in `find`.

I have a directory full of PHP source code and I want to issue a find/replace on every file in the entire hierarchy to change a setting. The setting’s value is 12345 and I want to change it to 67890. You use the -exec option , put your command in, substitute the filename with {}, and put \; to denote the end of the command. Let’s take a look:

find ./ -type f -exec perl -pi -e 's/12345/67890/g' {} \;

This works great, except how do I know I’m not executing something that I don’t want to? Well,`find` has this covered with the ‘ok’ option.

find ./ -type f -ok perl -pi -e 's/12345/67890/g' {} \;

This will now prompt you to OK everything that is done, line by line. Useful for if you’re going to do something where you’d like to know which files it’s operating on. However, `find` will truncate part of the command if it’s too long, so you don’t get to see exactly what it’s doing. You can always put an echo in front of your commands to see them in their entirety before you execute them.

`find` also has an -execdir and -okdir command, which operate in a different manner. -execdir operates as if you `cd` into the directory and then run the command. This can be handy if you don’t want to do the filename substitution with {}, maybe you want to run something inside every directory. If you have a directory full of source called src, and maybe you want to run the `make` command to compile everything, you could issue a `find` statement like so:

find ./src -type d -execdir make all \;

This is a bit messy but it illustrates my point.

 3. Using `find` to delete files

Speaking of dangerous operations, here’s another one you need to be very, very careful with.

But the easiest way to illustrate this would be to say maybe you downloaded a package for installation that contained a number of files that you do not want at all. Some shared packages come with .nfo files, .txt, .installs, .sfx, .subs, all sorts of unwanted garbage that you rarely want. Let’s allow `find` to delete that garbage, shall we?

I’m going to make these .sfx files disappear…

cd ~/Downloads/package/

find ./ -type f -name '*.sfx' -delete

Ta-da!

:jokerface:

4. Limiting Search Depth

So maybe you want to only go one directory deep. Maybe you don’t want to descend into any directories with your search. Maybe you want to search a filesystem without having `find` hop onto other drives in its search. `find` has got you covered like a fire team.

Descend only one directory:

find ./ -maxdepth 1

Don’t descend at all:

find ./ -maxdepth 0

Don’t search on filesystems or drives other than the current one:

find ./ -xdev

The above is particularly useful if you have a jump drive of some sort mounting in an inappropriate place.

Wrapping up.

Can you snatch the pebble from your filesystem’s hand?

We train again later, grasshopper.

-LightningCrash

8 Steps to Becoming A `find` Fu Master — Introduction

February 5th, 2009

(This is the first part in a three part series. Read part two here.)

Introduction:

Among the tools you will use on a Linux system, few are more valuable than the find utility from findutils. Forget search bars: This is a more highly tailored search system that can help you find exactly what you’re looking for with great ease. It will also permit you to perform operations upon the files that you find, delete them, or just list them. If you do more than just browse the Internet with your Linux box, you need some sort of proficiency with find.

Basics:
Find, at its simplest, will list everything in the filesystem beneath your current directory. This includes files, directories, symbolic links, FIFOs, sockets, and more other crap than you probably care to know about. Over 95% of your usage is going to be finding files and directories, so we’ll focus there.
If you open up a terminal and type
cd
find

You’ll receive a list of everything in the filesystem below your current directory.
Let’s limit that some. Let’s search for files that end in mp3.
find -name '*.mp3'

But wait, I know I had a file that ended in .MP3, why didn’t it show up? Well, the -name test is case sensitive. -iname is not. Let’s do the same thing with iname.

find -iname '*.mp3'

Ahh, there it is.
I want to search my videos directory for the video of my friend’s birthday party. I know it was named Fred-something. Now I can introduce a directory in the search.

find ~/Videos/ -name '*Fred*'

Hah. Fred is silly.

Well, that’s the most basic that you’ll get with find, grasshopper. Now you must prove your skills to the sensei!

Well, sort of. You just have to wait for the next part of the series.

-LightningCrash

(This is the first part in a three part series. Read part two here.)

HOWTO: Install Citrix ICA Client on Ubuntu 8.10 64-bit

February 2nd, 2009

First off, go ahead and install some libraries:

sudo apt-get install libxaw7 libmotif3

Go to http://www.citrix.com , click on “Downloads”, then click on “Citrix XenApp”, then select Linux clients. Download the tarball for Motif in your preferred language. Place it on your Desktop, and open a terminal.

cd ~/Desktop
mkdir citrix && mv ./en.linuxx86.tar.gz ./citrix && cd ./citrix
tar xvzf en.linuxx86.tar.gz

sudo ./setupwfc

The install prompts should accept default input.

Now we’re going to need to go grab a 32-bit copy of libmotif3. Click here and select a mirror. Download the file to your Desktop.

Open up a terminal again, we’re going to pluck out the 32-bit motif libraries that we need.
cd ~/Desktop
mkdir motif
mv libmotif* ./motif/ && cd ./motif
ar -x ./libmotif*
tar xvzf data.tar.gz
cd ./usr/lib/
cp * /usr/lib32/

Now go on and run wfcmgr!

Citrix ICA Client running on Ubuntu 8.10 64-bit

Ta-da!

-LightningCrash

Link for today: Bluetooth Proximity - Ubuntu HOWTO

August 14th, 2008

Link to Howto

I’ve used this successfully and can attest to how handy it is. Kudos to Iceni for putting that Howto together.

BT dongles are getting cheap enough today that this is very doable. Most phones have BT already, so why not try it out?

Linksys also makes a USB Bluetooth adapter with a movable antenna. I’ve not heard anything about that particular adapter, but I would wager that its range is greater than that of normal Bluetooth dongles.

That’s it for today!

-LightningCrash

basics in awk

August 13th, 2008

awk is a very, very useful command-line program that any Linux/Unix ninja should be familiar with. Awk is specifically geared towards processing text, and it was actually a combination of awk and sed that were an inspiration for Perl.

To start with, awk has three major elements that you need to be aware of when you’re working with it. These are the field separator, the pattern, and the action for the pattern.

Your fied separator is obviously what is inbetween the text elements you want to work with. If you open up a terminal and type ‘ps -elf’, you’ll see that this would just be spaces. Some files, like CSV files, have commas as the separator. Awk can be told what to look for via the -F option on the command-line, or in the program itself. For one-off piping, I prefer to do it via the -F option.

The pattern is much like an ‘if … then’ statement in other programming languages. If there isn’t a pattern, the action specified will be applied to all rows of input.

What makes awk handy is that it gives you capabilities that the `cut` command simply can’t provide. For instance, if I have a twenty-column CSV and I would like to spit out the third and eleventh column, I can execute the following:

awk -F',' '{print $3 FS $11}' file.input

The -F’,’ tells awk that the input fields will be separated by commas. The area enclosed in the braces is the action I talked about earlier. I didn’t specify a pattern before the action, so the action was applied to every line of input. “print $3 FS $11” tells awk to print to the screen the third field of input, the field separator (which we defined as a comma with the -F’,'), and the eleventh field of input.

If I wanted to do the same, but only print lines where the third field was over a number, say, 110, I could execute the following:

awk -F',' '$3 > 110 {print $3 FS $11}'" file.input

The pattern before the braces functions much like an “if … then”. If the third field is over 110, awk prints out the third field, the field separator, and the eleventh field.

There is much, much more that you can do with awk, but this should be enough to hint you in the right direction. I know I use awk daily for various tasks related to command-line mischief. A common thing I use awk for is to manipulate /etc/passwd, where some user account information is stored.

Fortunately, GNU awk is often smart enough to pick up the field separators without specifying the -F option. For instance, /etc/passwd is separated by a colon “:”, but GNU awk automatically recognizes this. It’s worth noting that on some other systems without GNU utilities, awk may behave in ways that you don’t anticipate.

That’s it for the moment, just some small tips to get you moving. I’d recommend picking up a book on AWK. I recommend you pick up a copy of “The AWK Programming Language” by Aho, Kernighan and Weinberger. It only makes sense, since they are the creators of AWK. I have also been told that the O’Reilly AWK book is very good. In addition, the GNU awk is well-documented all over the Internet, so you shouldn’t be lacking in study material if you put some effort into it.

Until next time!

-LightningCrash

comm — when diff won’t do

August 7th, 2008

Don’t get me wrong, I appreciate the utility of diff. I use diff all the time. Unfortunately, there are times when I don’t want a diff output.

Case in point: I was comparing two lists of accounts. I wanted the accounts that were present in the newer file. Diff gave me output, but the diff output wasn’t what I wanted.

comm is handy for this. You would do the following to get the list I want:
comm -1 -3 oldfile newfile

Lovely! comm is actually part of the GNU Core Utilities and was written by Richard Stallman and David MacKenzie. That confirmed my suspicions that comm subscribed to an old UNIX adage for some utilities: Do one thing very, very well.

I have nothing more to say about comm, you’ll have to wear it out yourself.

Until next time,

-LightningCrash

Is Quicken Online a blessing to Linux users? Well….

March 6th, 2008

Words passed my ears not so long ago that filled my heart with hope. “Have you looked at Quicken Online?”

A prompt buzz of the site showed some promise, much like a pre-psychotic Britney Spears. At a paltry $3/mo, who could argue with that pricing, either? My fond memories of the Quicken application under Windows led me to believe that finally, I could have my Quicken functionality back, but under Linux. Or, for that matter, independent of any operating system.

I was wrong.

You see, the real Quicken application does a mighty number of wonderful deeds, carefully crafted to the exacting specifications that you’ve come to accept from Intuit since the widespread adoption of Quicken in the early nineties. I don’t need to explain to you what the real Quicken does, though. I merely have to show you what the online version does.

In extended detail, what Quicken Online does for you:

  1. Downloads your data from your financial institutions
  2. Lets you categorize it
  3. Puts it in a pie chart

Let’s pretend for a moment that the above is actually useful. What can you do with the above? I’ve come up with some clever ideas.

  1. Spend more money in an attempt to make one part of the pie bigger than the other
  2. Point out the pie to random people in your office building
  3. Pretend that the pie chart is an actual, multi-colored pie
  4. Go buy a real pie
  5. Tell your friends you’re so utterly independent of Micro$h4ft Winblows that you don’t even need Quicken for Windows, even under WINE support
  6. Go to the liquor store and buy some real WINE support
  7. Run those damn kids off your lawn

I have to wonder what the development plan was for Quicken Online. Perhaps the business case went a bit like this:

  1. Create a quick webapp and call it Quicken Online
  2. Market the webapp
  3. ???
  4. PROFIT!!

I give this application a C for effort, and an F for execution.

Of course, F is for Fail.

-LightningCrash

watch what `watch` can do

January 8th, 2008

I’ve been running a lot of scripts recently that require me to check on things that I can’t monitor with `tail -f logname`. Mainly to keep an eye on a process and get information about the processes running that I’m interested in. Periodically, one hangs and I like to just jump back to it and kill it.

Watch to the rescue!

Watch executes a command at a specified period. For instance, if I’m extracting a file and want to monitor the progress, I can simply type:

watch 'ls -la filename.ext'

and I will get the output from `ls -la filename.ext` every 2 seconds.

If my internet connection is down and I am impatiently waiting for it to come back up, I can run:

watch -n15 'ping -c1 www.google.com'

If I’m forking processes in a Bash script and want to make sure none of them are hung:

watch -n2 'ps ax|grep [p]rocessname'

It’s very handy!

I periodically come across an issue whereby `tail -f /var/log/syslog` will hang after operating for a couple of hours. I solve this by using the following instead:

watch -n5 'tail -n20 /var/log/syslog'

`man watch` to find more uses for this program. I tried `woman watch` but apparently Linux doesn’t carry those.

and holy sweet crap, it’s been a long time since I wrote an article on this site!

Until next time,

-LightningCrash

Howto: Random E-mail Signature in Evolution

October 18th, 2007

I include Latin phrases in my e-mail signature at work. It’s a fun little thing I do just to make people interested.

So I decided to make a signature that would include a random Latin phrase in every e-mail I send. Thankfully, Evolution makes this easy.

First, I make a text file which I call sigquotes.txt , and stash it in my home directory, under a folder for personal items. I put one quote per line.

Next, I have to write a little ditty to grab a random line from that file and output the results. I used PHP for this, but almost anything could have worked. I dump the following into sigrand.php :
<?
$quoteFile = "/home/icanhaslinux/personal/sigquotes.txt";

$fp = fopen($quoteFile, "r");
$content = fread($fp, filesize($quoteFile));
$quotes = explode("\n",$content);
fclose($fp);

srand((double)microtime()*1000000);
$index = (rand(1, sizeof($quotes)) - 1);

echo "________<br>LightningCrash<br>Resident Unix Witchdoctor<br><br>";
echo $quotes[$index] . "<br>";
?>

Easy enough. I had problems getting the php itself to run as a script, so I had to make another short script to launch the PHP file. I put the following into sigrand.sh :

/usr/bin/php5-cgi -q /home/icanhaslinux/personal/sigrand.php

I chmod +x sigrand.sh and go on my way to the next step.

I fire up Evolution, go to Edit->Preferences. I go to Composer Preferences and click on Signatures. Then I click on the “Add Script” button, name it Random Latin, navigate to /home/icanhaslinux/personal, and select sigrand.sh as my signature script.

That part is done. Now to make it default for my outgoing e-mail. From the Preferences page, I click edit on my e-mail account. On this page, I have a drop-down box where I can select my signature for this account. I select Random Latin, of course. I then click OK and Close.

Now, to test it out, just fire up a new e-mail.

Until next time!
-LightningCrash

EDIT:   It’s been requested that I make clear what license this code is issued under. I consider the code snippets on this article to be public domain and not subject to licensing. Use the code in this article as you see fit.

Just for SB @ Microsoft

October 9th, 2007

steveballmershands.jpg

11-02ballmer_lg.jpg

 

You gotta love Fair Use. :)