Searching for the contents of many files in Linux

One thing that I find remarkable in Linux is the vast array of clever tools that allow you to do ‘clever stuff’.

The problem exists where these obscure tools are difficult to work out how to use effectively, and moreover, use right.

One task I aim to do occasionally is search a disk for files containing particular text. This is always difficult. Even in Windows XP – the search indexing service actually prevents Windows from searching for specific text within a file. I’ve searched for files that I know have certain strings in them, only for Windows to tell me that it cannot find them.

As my love of Linux and Ubuntu grows, I found myself needing to perform this task again. Recently, I’ve scraped by using the rather useful find tool:

<code>find . -type f -name foobar</code>

What this little snippet does is seach the current directory and all subdirectories for any files containing the word foobar in the name. So it could return names such as ./foobar.doc, ./test/foobar.doc, or ./this is a foobar file name.txt. Pretty useful.

Tonight I needed to search for a specific phrase in a Word document on a disk. This is where the Linux command line really becomes powerful:

<code>find . -type f -name *.doc -print0 | xargs -0 grep -i 'foo bar'</code>

This will join the power of two commands: grep and find to create a groovy search.

First of all, the find command is searching for all files (-type f) that end in .doc (-name *.doc) in all folders starting from the folder I am in.

Once find finds a match, we use the pipe (the |) to pass that file name over to grep, which will search the file for the string foo bar.

We have to use the -print0 and -0 options to make sure that find and grep share the file names correctly between them in case we find any unusual ones (files with spaces would be counted as unusual).

Finally, the -i tells grep that the search is case-insensitive. This means that any .doc file with foo bar, Foo Bar, fOO bAR or any other case variation will be caught. Without it, only the exact string will be matched.

Now go forth and search!

Selecting a sound device in Ubuntu

I’ve found that using the ALSA sound system in Ubuntu is pretty slick, but becomes a pain when the sound tool in Ubuntu doesn’t correctly switch devices using the graphical user interface.

In a nutshell, here are a few useful commands to list and select the sound devices that are on your computer:

<code>asoundconf list</code>

list available sound cards

<code>asoundconf set-default-card !!#</code>

Set the default sound card. Replace !! with the device name from the previous command

<code>alsamixer !!</code>

Adjust volume settings for your specific sound card. Once again, replace !! with the device.

Running through these 3 commands will probably save any user a great deal of grief in the long run.

Task Switching in Compiz-Fusion

Being a sucker for all kinds of eye-candy, I’ve been running the latest builds of Compiz-Fusion. I’ve got to say that it is fantastic and makes the desktop experience a much more pleasurable one.

One of the things that has caused an issue is that the Application Switcher plugin (ALT+TAB) keeps on switching to every second window. Very annoying if you have only an even number of windows on the desktop.

Today, after breaking Compiz with an update, I managed to get it behaving itself by essentially reinstalling it – but the fix for the application switcher is even easier!

To fix the switcher

  • Open up the CompizConfig Settings Manager
  • Under Window Management, click on Application Switcher
  • Click on the Actions tab
  • If you have multiple entries for the application switcher (like in this screenshot), you need to make sure that YOU ONLY HAVE SHORTCUTS SET FOR 1 GROUP.
    CompizConfig Application Switcher
  • Once you have cleared the settings so that only one ‘Next Window’ and ‘Prev Window’ shortcut is used, test it out.

And that is it! I think that the only reason that it became an issue was that some settings were inherited from Beryl.

Open docx files in OpenOffice on Ubuntu

I’ve been using Office 2007 quite happily for some time now. But the niggles are starting to hit me.

One of the main issues is the new .docx format not being supported across the board yet. And with me being a sucker for all things new, I have to make a point of saving an alternate version to open in OpenOffice.

Until now.

I found this little nugget on the Ubuntu forums to install the Novell converter in Ubuntu:

1) Download the file odf-converter-1.0.0-5.i586.rpm from http://download.novell.com/SummaryFree.jsp?buildid=ESrjfdE4U58~

2) Use alien to convert it to a Slackware tgz file

<code>alien -ct odf-converter-1.0.0-5.i586.rpm</code>

3) Unpack the slackware tgz file

<code>tar xzf odf-converter-1.0.0.tgz</code>

4) Copy three files into your OpenOffice.org directories — note that the usr that you’re copying from is a directory that was inside the tgz file (3 lines here)

<code>sudo cp usr/lib/ooo-2.0/program/OdfConverter /usr/lib/openoffice/program/</code>
<code>sudo cp usr/lib/ooo-2.0/share/registry/modules/org/openoffice/TypeDetection/Filter/MOOXFilter_cpp.xcu /usr/lib/openoffice/share/registry/modules/org/openoffice/TypeDetection/Filter/</code>
<code>sudo cp usr/lib/ooo-2.0/share/registry/modules/org/openoffice/TypeDetection/Types/MOOXTypeDetection.xcu /usr/lib/openoffice/share/registry/modules/org/openoffice/TypeDetection/Types/</code>

Once I restarted OpenOffice, I found that it could import basic docx files quite happily.

This worked nicely for me in Feisty Fawn – although it’s a good idea to make sure that OpenOffice is up-to-date.

Extract Audio from DVDs in Ubuntu

I wanted to extract Marillion’s cover of Toxic today, which lives on the DVD release of Thankyou Whoever You Are / Most Toys.

I found a useful page over at Ubuntu Geek which covered the topic nicely using a tool called transcode, which I’ve used a little of in the past.

I was having some difficulties with this one though, as transcode only worked on a few titles on the DVD. After a bit of testing, I discovered that this wasn’t woking on titles that had no video.

Eventually I settled on this command line to extract the song:

<code>transcode -i /dev/dvd -x null,dvd -T 4,1,1 -a 0 -y null,ogg -o MarillionToxic.ogg</code>

Here’s the breakdown:
transcode – run transcode
-i /dev/dvd – set the input to the DVD drive
-x null,dvd – set the input modules. As I don’t want video, I’ve used null and then dvd for the audio channel
-T 4,1,1 – set the Title 4, Chapter 1, Angle 1, which is the Toxic track on the DVD
-a 0 – set the audio track (the first is 0 on DVDs)
-y null,ogg – sets the output to ogg. Once again, I’ve forced null for the video
-o MarillionToxic.ogg – finally, I’ve set the filename for extracting the audio.

The whole track was extracted from DVD in a matter of seconds. Brilliant.

Ubuntu 7.04 fails to mount USB drive after kernal upgrade

After the recent Feisty kernel update, I found that I couldn’t access a USB drive without using root privileges in the command prompt.

When plugging a USB flash drive in, I’d get the following error:
Cannot Mount Volume

Cannot mount volume.

You are not privileged to mount the volume ‘drivename’.

The problem is the new way that Ubuntu identifies disk drives on your system, and also some changes in the mounting system. What I had to do was edit the /etc/fstab file to correct the error. So here goes:

  • Insert the USB pen drive into the USB port
  • Click Applications > Accessories > Terminal
  • type dmesg
  • Read the last entry that appears on screen (you should see some errors or warnings), and then type sudo gedit /etc/fstab
  • Here you need to find the connection between your error:
    <code>[ 1025.759465] NTFS-fs warning (device hde1): parse_options(): Option utf8 is no longer supported, using option nls=utf8. Please use option nls=utf8 in the future and make sure utf8 is compiled either as a module or into the kernel.
    [ 1026.098973] NTFS volume version 3.1.
    </code>

    and the fstab:

    <code>proc /proc proc defaults 0 0</code><code>
    # Entry for /dev/sdb1 :</code>
    <code>
    UUID=2966f4fe-7f28-4017-b986-1ca801944b4f / ext3 defaults,errors=remount-ro 0 1</code>
    <code>
    # Entry for /dev/sdb5 :</code>
    <code>
    UUID=68989c2d-87a1-4a4f-b5d7-210790b336c4 none swap sw 0 0</code>
    <code>
    /dev/hdd /media/cdrom0 udf,iso9660 user,noauto 0 0</code>
    <code>
    /dev/ /media/floppy0 auto rw,user,noauto 0 0</code>
    <code>
    none /proc/bus/usb usbfs devgid=100,devmode=666 0 0</code>
    <code>
    /dev/sda1 /media/VistaHDD ntfs umask=222,utf8 0 0</code>
  • For me, the connection is the ntfs-fs error and the list listed drive. The error says that I can’t use UTF8, so it’s stopping before it mounts the USB drive.
  • If you think that you’ve found the connection, comment out the line with a hash:
    <code>#/dev/sda1 /media/VistaHDD ntfs umask=222,utf8 0 0</code>
  • Save the file, then try and put the USB drive back in the computer again
  • If it works, great. If not, run dmesg again, and check the error is the same – then comment out the appropriate entry again.

This is mainly to do with recent kernal upgrades, which are changing a number of features. I’m sure it’s good in the long run!