Mass Convert RAW Images into JPEGs

First, you’ll need to install ufraw-batch:

sudo apt-get install ufraw-batch

Now, you can easily and quickly dump your RAW images into JPEGs:

find . -maxdepth 1 -type f -iname "*.cr2" | while read file do
    ufraw-batch --wb=camera --exposure=auto \
        --out-type=jpeg --compression=90 \
        --output="${file%.CR2}.JPG" \
        "$file"
done

The above command will find all files ending in CR2 in the current directory and convert them to JPEGs in the same directory.

Converting WMA Music to MP3 with MPlayer and Lame

Another quick, hopefully painless tutorial.

You can ask MPlayer to dump WMA files into WAV files, which you can then transcode into MP3 files:

mplayer "myfile.wma" -vo null -vc dummy -af resample=441000 \
    -ao pcm:waveheader \
    -ao pcm:file="out.wav"

Alternatively, you can write it directly to lame‘s standard input to daisy-chain the operation:

mplayer "myfile.wma" -vo null -vc dummy -af resample=44100 \
    -ao pcm:waveheader \ 
    -ao pcm:file=>(lame -b 192 -q 0 - "out.mp3")

Bash Input and Output Redirection and Piping

Today, we’re going to talk about Bash and input/output redirection. This is pretty standard stuff for any Unix and Linux user, but there are a few ins and outs that nobody ever told me about. Let’s just use some examples:

Pretty standard stuff, write all standard output to a file:

# write the stdout of mycommand to myfile.log
mycommand > myfile.log 

Write all standard output and standard error to a file:

# write the stdout and stderr of mycommand to myfile.log
mycommand 2>&1 > myfile.log

Write standard input to one file and standard error to another file:

# write stdout of mycommand to myfile.log and stderr of mycommand to myfile.error.log
mycommand 2> myfile.error.log > myfile.log

Append standard output to a file (not completely overwriting it as above):

# append stdout of mycommand to myfile.log
mycommand >> myfile.log

The same can be done with stderr:

# append stderr of mycommand to myfile.error.log
mycommand 2>> myfile.error.log

You can also force-clobber the file if you really want it to overwrite things:

# force-overwrite stdout of mycommand to myfile.log
mycommand >! myfile.log

Now, let’s move on to piping.

Instead of writing all standard output to a file, we’ll print it and write it to a file with tee:

# pipe the stdout of mycommand to `tee`, which will print it and write it to myfile.log
mycommand | tee myfile.log

What about standard error, though? You can redirect standard error to standard out:

# pipe the stdout of mycommand (pushing stderror to stdout) to `tee`
mycommand 2>&1 | tee myfile.log

Or, redux: accomplish that whole standard error redirect in less characters:

mycommand |& tee myfile.log

And, just for kicks, redirect standard output to standard error:

mycommand >&2 

As far as Bash i/o redirection goes, that’s pretty much it. Have fun!

Drop or Empty/Truncate all Tables in a MySQL Database

Unfortunately, no command exists in MySQL to drop or truncate all tables in a database. You can drop or truncate each table in a database one-by-one, but even that is cumbersome, as you can’t even pass a comma-delimited list of tables to use.

There’s no command like drop all tables;, neither is there a command that will allow you to drop or truncate multiple tables at once like drop table users, addresses, locations;. You’re left with doing it line by line in a tedious, repetitive manner like so:

drop table users;
drop table addresses;
drop table locations;

If you have a lot of tables, this simply doesn’t work.

If you have a Bash-like shell, you can do it in a much more efficient way via the mysql command and the mysqldump command and some regular expression hackery:

mysqldump -u username --password=password --add-drop-table --no-data database_name | grep ^DROP | mysql -u username --password=password database_name

Winning! If you want to do the same thing for truncating all databases, look no further:

mysqldump -u username --password=password --add-drop-table --no-data database_name | grep ^DROP | sed "s:^DROP TABLE IF EXISTS:TRUNCATE TABLE:g" | mysql -u username --password=password database_name

Now that you’ve emptied your database, it’s time to weep that you forgot to make a backup ;)

via thingy-ma-jig.co.uk

X Clipboard Magic Hackery

Dramatic title, no? In any case, you may or may not know that you can do some really awesome stuff using the X clipboard in Linux.

For example, one of the things I often find myself doing something like this:

cat something.log | xclip -selection clipboard

This basically copies the file contents of something.log to the X clipboard.

For stackoverflow junkies, you can use a nice little sed command to tab the output so you’ll be able to directly paste code into a question and have it formatted in Markdown properly:

cat ProblemChild.java | sed 's:^:\t:g' | xclip -selection clipboard

The above will insert a tab before each line as it copies ProblemChild.java to the clipboard.

Let’s finish off with some awesome hackery. What if you’ve copied something to the clipboard but need to format it for StackOverflow? Well, it’s really not too hard:

xclip -o | sed 's:^:\t:g' | xclip -selection clipboard

This dumps the clipboard to standard out, inserts a tab on each line at the beginning, then fills the clipboard with the new content. Talk about winning!

Convert a MP3 to a WAV File

There are a ton of ways to convert a MP3 file to a WAV file, so take your pick:

mpg123 -w output.wav input.mp3

Or, if you’d prefer to use lame:

lame --decode input.mp3 output.wav

Perhaps you live on the wild side and would like to use FFMPEG:

ffmpeg -i input.mp3 output.wav

As you can see, it’s pretty darn simple to do, so have at it!

Resample and Reencode All MP3 Files in a Directory

This is going to be a fairly short and simple post, wherein we’re going to simply drop into Bash and reencode some files. Let’s do it:

find . -type f -iname "*.mp3" | while read file 
do
    mv "$file" "${file}.old" && \
    lame --mp3input -q 0 -b 320 --resample 44.1 "${file}.old" "$file" && \
    id3cp "${file}.old" "$file" && \
    mv "${file}.old" "/tmp/${file%.old} 
done

It’s pretty simple logic. We chain everything together with “&&” so that if any one piece in the puzzle fails, it’ll stop executing. First, we move the file to it’s name plus “.old” (so “01 – The Early Days.mp3″ becomes “01 – The Early Days.mp3.old”). Then, if that works we convert the file with lame. We force lame to assume that it’s an MP3 file, because it is, set it to highest quality and a constant bitrate of 320, resample it to 44.1kHz, and rename the output to the original file name. Then, if that worked, we copy all ID3 tags from the old file to the new one. When that’s done, we move the old file to the /tmp directory so we can delete it if everything went well or restore it if we need to.

[Ubuntu] Redmine+Gitosis – An Awesome Project Management Solution

When it comes to project management, there are a lot of awesome offerings out there. When you’re looking for something free, your list gets a little smaller. Redmine is an awesome project management solution written in Ruby. It features bug/issue/time tracking, multiple users and projects, repository watching, forums, wiki, documents, and more. Gitosis is a Git-hosting server to make it easy for you to put your Git repositories online. Put ‘em both together and you’ve got a really powerful way to manage your projects, either for yourself or for your company. Let’s get to work.

Read more…

[Bash] Silently start background processes with ‘fart’

Perhaps it’s not the best choice of names for a script like this, but I wrote this script at a late hour, so cut me some slack :) ‘fart’ will simply start a process, redirect all of its output (standard output and standard error) into oblivion (/dev/null), fork it to the background, and disown it. What this means is that you’re essentially truly creating a background process that will run independently of the current shell and will only be killed once it terminates or once you terminate it yourself. When you’re connecting to a computer over SSH, this script will save you a lot of time, as it’ll disown your processes so that when you disconnect, they’ll keep running. Here it is:

#!/bin/bash
$@ > /dev/null 2>&1 &
disown $!

I know it’s not much, but let’s look at an example. Suppose you wanted to start ‘gedit’ in the background. Here’s how you’d do it with ‘fart’:

fart gedit

Here’s how you’d do it without ‘fart’:

gedit > /dev/null 2>&1 & \
disown $!

Much easier. Enjoy. Make sure you make ‘fart’ executable and put it on your path via the following:

chmod +x fart 
mv fart /usr/local/bin 

[Networking] Detect Open Ports on a Remote Computer Using ‘netcat’

Ever needed to detect open ports on a remote machine for purely altruistic reasons? Once again, netcat to the rescue! You can easily scan a range of ports on a given IP address using our networking swiss army knife.

You want to find out if any ports are open between ports 1024 and 2048 on another computer on your network. With the legendary knowledge of netcat that you have, you enter the following in your terminal:

netcat -z 192.168.1.5 1024-2048

Note that this does actually take some time to complete. Alternatively, you can use the short form of the above:

nc -z 192.168.1.5 1024-2048

It’s as easy as that. netcat will dump out any info on ports it finds that are open.