Evolution

The Find Command

Yesterday I dabbled a bit with the find command. Today, I mastered it. Mastered may be a strong term, but I do now feel very comfortable using it. So without further delay...

My Adventure With Find
My need to use the find command arose because of all the pictures I have on my computer and the way that I organize them. I would say I have somewhere around 10,000 pictures on my computer (I am quite the busy little amateur photographer). I keep them in a folder called events and then below that there are folders for each date that I loaded my camera. So my file system looks something like this:

Pictures
--Random
----Bills Pics
----What are these
--Projects
----Couch Project
----Time Lapse
--Events
----08-01-10 New Years and the Aftermath
----08-02-16 Valentines Day Sucks
----08-03-21 Beer Pong Party

My first order of business was to get rid of all files under the directory Pictures named Picassa.ini. A while back I was going to try out Picassa as a picture organizer on my windows box. What I later found out is that Picassa put a little invisible ini file in every single one of my folders.

First let's find every file Picassa.ini (all of these commands are done in the Pictures directory):
find . -name "Picassa.ini"
What does that do you ask? Basically it finds all files named Picassa.ini. Let's break it down a little further.
find - the command that initiates a find.
. - tells where to look. In this case we are using a dot which indicates the current directory and everything below.
-name "Picassa.ini" - This says "Look for files named Picassa.ini"

After that little endeavor, I began to wonder what other junk files windows had put in with all my pictures. With a little wild card I did a search for any .ini file.
find . -name "*.ini"
That turned up a whole mess of ini files that Windows uses for who-knows-what. Now I needed to figure out how to delete them all. As I found found out, there is always more than one way to skin a cat in Linux. The easiest way was to simply put a delete behind your find command:
find . -name "*.ini" -delete

Now I must warn you. There is no going back with Linux. The command line gives you great power over your operating system, but with great power comes great responsibility (Spiderman). Let's say for some reason you are in the root of your file system and type this (find . -name "*.ini" -delete). You might have just deleted every ini file on your entire system. If you had your Windows partition mounted, you killed your ability to use Windows.

There are a few more things about the find command that I find useful and would like to pass on to you. My Pictures directory is massive. It weighs in somewhere around 20 gig. This is mainly because sometimes when I dumped my camera, I was in a hurry and didn't have time to resize/crop the pictures. When that happens I end up with a bunch of pictures that are about 2.3 megs a piece. So how can I go about finding all the files that I need to shrink? Simple, by using "size". Let's say we want to go through and look for all of my pictures that are over 2 meg.

This is how it's done, and just for kicks I'm going to throw in some other options just to show you:
find . -size +2000k -type f -name "*.jpg" -or -name "*.JPG"
-size +2000k - This only searches for files that are over 2000 kilobytes.
-type f - Only searches for files as opposed to directories, links, etc.
-or - What if the extension on my pictures are sometimes in caps and sometimes in lower case? I could write a script to make them all lower case or I could write a script to evaluate the name as lower case, but for the sake of this example let's just look for both. What I am doing with the -or switch is saying that the name (-name) can be either "*.jpg" or "*.JPG" this way all jpeg files are included.

That about wraps it up for what I know about the find command. There are many many other options and things that you can do with the find command. If you want to read about them type "man find" into any terminal. Good luck and happy Linuxing.

Learning by doing is the

Learning by doing is the best way to learn in my opinion. Nice post.

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.

More information about formatting options

Powered by Drupal - Design by artinet