Fixing Dates in Image EXIF Tag Data from Linux

Posted on June 21st, 2009 in Tech Tips by gmendoza

I recently needed to organize a large number of old digital photos that had the wrong date embedded in their EXIF tag data. The camera I used many years ago would often lose track of time and would sometimes be set to the wrong year. Applications I now use to organize photos read this data and made my albums difficult to navigate. I came across a Linux command line utility called jhead that allows you to modify this information to whatever you wish, and its easy to use in scripts as well.  Installing was easy, because it’s currently in most repositories, including Ubuntu’s.

To read existing EXIF tag data, simply run jhead against an image without any options. As you can see from the example below, my date is set to the year 2022.

jhead image.jpg
File name : image.jpg
File size : 159390 bytes
File date : 2004:01:12 07:35:23
Camera make : Samsung
Camera model : Digimax 200
Date/Time : 2022:02:12 04:04:17
Resolution : 800 x 600
Flash used : Yes
Exposure time: 0.045 s (1/22)
Aperture : f/2.8

To clear all EXIF data from the file, use the -de option. Then recreate the EXIF fields with the -mkexif option, and check the data again. Notice the new Date/Time is set to the timestamp on the file.

jhead -de image.jpg
Modified: image.jpg

jhead -mkexif image.jpg
Modified: image.jpg

jhead image.jpg
File name : image.jpg
File size : 147751 bytes
File date : 2004:01:12 07:35:23
Date/Time : 2004:01:12 07:35:23
Resolution : 800 x 600

To change the entire timestamp manually, use the -ts option. Notice, there is no space between the -ts and the option. I could not trust the month and day, so I simply chaged the date to midnight on January 1, 2003.

jhead -ts2003:01:01-00:00:00 image.jpg
Modified: image.jpg

jhead image.jpg
File name : image.jpg
File size : 147751 bytes
File date : 2004:01:12 07:35:23
Date/Time : 2003:01:01 00:00:00
Resolution : 800 x 600

For many more options, check out the man page or visit the jhead site for more info.

Convert MKV to Xvid with Mencoder

Posted on April 9th, 2009 in Tech Tips by gmendoza

I recently wanted to convert some of my 720p and 1080p Matroska Video (MKV) files to the Xvid format so that I can play them on my Xbox 360 (check out ushare). I really wanted to make sure that the video quality and Dolby Digital 5.1 audio would remain intact, and was pleased to finally get the job done with mencoder.

The syntax is easy and is described as follows.

mencoder movie.mkv -channels 6 -ovc xvid -xvidencopts pass=1 -oac copy -o movie.avi

The Dolby Digital 5.1 (AC3) output was a major pain to figure out because by default, mencoder (and mplayer) only will select 2 audio channels. So increasing the value to 6 ensures you receive them all. Otherwise, you end up getting standard stereo out all channels.

There’s a ton of options that you can use, so just be sure to read the man pages for mencoder.

File Synchronization with Rsync over SSH

Posted on April 6th, 2009 in Tech Tips by gmendoza

To quickly synchronize files between two systems, rsync is an excellent tool that not only decreases the amount of time it takes to transfer files through a data deduplication algorithm, but can also be used transparently over SSH. The beauty of running rsync over SSH is that it does not require the rsyncd server to be running before a synchronization request and the connection is both authenticated and encrypted. All that is required is for the remote host you are connecting to be running the OpenSSH server component and of course the rsync application.

I use rsync the most for synchronizing my “Music” and “Documents” folders between a number of my systems at home and at work. All of these systems have these folders in the root of my home directory.

ls -ld ~/Music ~/Documents
drwxr-xr-x 16 gmendoza gmendoza 4096 2009-04-06 23:23 /home/gmendoza/Documents
drwxr-xr-x  9 gmendoza gmendoza 4096 2009-04-06 23:23 /home/gmendoza/Music

To push my recent changes from my local system (host1) to my remote system called (host2), I use the following commands.

rsync -avPe ssh ~/Music host2:~/
rsync -avPe ssh ~/Documents host2:~/

Notice, “Music” and “Documents” are specified without a trailing “/”, e.g. “Music/” or “Documents/”. This is important, because otherwise, it would copy only the contents of the folder to the remote home directory, and not the folder itself, which is described in more detail in the rsync man page.

Instead of running the above commands twice, you can also specify multiple files all in a single line.

rsync -avPe ssh ~/Music ~/Documents host2:~/

To synchronize changes made on the remote system to my local system, just reverse the commands. Notice the periods at the end of the line, which specifies the destination as the local working directory. Also, instead of wasting space by entering the host twice, you can use standard syntax to specify ranges or sets of files. In this case, I use curly brackets to specify the two directories on the remote host that share the same parent directory should be copied to my local working directory.

rsync -avPe ssh host2:~/{Music,Documents} .

I’ll also use the “delete” option to remove any files and folders the have been removed from the source system.

rsync --delete -avPe ssh ~/Music host2:~/

By default, rsync compares files extremely fast using a “quick check” algorithm based on the file size or in the last modified time (per the rsync man page). While I was updating my Music collection, I noticed that rsync was not detecting my ID3 tag modifications. By using the “-c” option, rsync will compare files using a 128 bit MD4 checksum as a more definitive change detection method. While this will slow the process down significantly, there’s obvious accuracy benefits in using the checksum method.

rsync -acvPe ssh Music host2:~/

Also, as you may have noticed rsync is strictly a unidirectional utility. This means that it only sends or receives data in a single direction, and it will clobber or delete any file or folder with the same name in the direction your are sending the data. For a great bidirectional utility, check out unison, which I will cover in an upcoming article.

Line wrapping text made easy with fold

Posted on April 6th, 2009 in Tech Tips by gmendoza

Line wrapping text from the command line is easy with the fold utility, which of course is provided by the Free Software Foundation.  By default, the fold command will wrap text at 80 characters, but you can of course specify the width manually.  I prefer using the -s option, which will break only on spaces, making sure not to break in the middle of a word.

For example, the following command will concatenate a text file to standard output, adding line breaks  only at spaces or at 72 characters, whichever comes first.

fold -s -w 72 textfile.txt

This can also be useful if you want to clearsign a message with Gnupg, but wish to line wrap it beforehand.

fold -s -w 72 textfile.txt | gpg --clearsign -u user@email.com

Add redirection if you wish to output the results to a file.

fold -s -w 72 textfile.txt > newfile.txt

As mentioned here, the fmt command also provides the same primary features of fold, but is much better. Not only does it wrap long lines, but it also fills out short lines as well. There are additional options that are worth looking into. Be sure to check out the man page!

man fmt

Rename Files in Bulk from the Command Line

Posted on July 6th, 2008 in Tech Tips by gmendoza

Renaming a large number of files can seem like a daunting task, but no worries, your trusty Linux CLI is at your service. For this example, we will rename a number of MP3’s located in multiple subdirectories with a couple very easy commands; “find” and “rename”.

By listing the following directory, you’ll see that the MP3’s have been named with “(LP Version)”, and of course I don’t like this naming convention.

$ cd ~/Music/Metallica/Metallica/
$ ls -1
01 - Enter Sandman (LP Version).mp3
02 - Sad But True (LP Version).mp3
03 - Holier Than Thou (LP Version).mp3
04 - The Unforgiven (LP Version).mp3
05 - Wherever I May Roam (LP Version).mp3
06 - Don't Tread On Me (LP Version).mp3
07 - Through The Never (LP Version).mp3
08 - Nothing Else Matters (LP Version).mp3
09 - Of Wolf And Man (LP Version).mp3
10 - The God That Failed (LP Version).mp3
11 - My Friend Of Misery (LP Version).mp3
12 - The Struggle Within (LP Version).mp3

We’ll use the “rename” command to search for and delete the string ” (LP Version)” in any of the mp3 file names.

Syntax:

$ rename (search command) (files)
$ rename 's/search_for_string/replace_string_with_this/' files

To delete the matching string, simply leave the replace area empty like so:

$ rename 's/search_for_string//' files

Our Example:

$ rename 's/ \(LP Version\)//' *.mp3

Notice, the left and right parentheses need to be preceded with a backslash “\” character, although the spaces do not. The backslash is a metacharacter used to give you control over what your are matching against. For more info, here’s a link to a decent tutorial on the matter.

You can see the results of the command below.

$ ls -1
01 - Enter Sandman.mp3
02 - Sad But True.mp3
03 - Holier Than Thou.mp3
04 - The Unforgiven.mp3
05 - Wherever I May Roam.mp3
06 - Don't Tread On Me.mp3
07 - Through The Never.mp3
08 - Nothing Else Matters.mp3
09 - Of Wolf And Man.mp3
10 - The God That Failed.mp3
11 - My Friend Of Misery.mp3
12 - The Struggle Within.mp3

Now, to rename a large number of files spanning multiple directories, simply combine “rename” with the power of the “find” command.

Syntax:

find . -type f -name *.mp3 -exec rename 's/ \(LP Version\)//' '{}' \;

In this example, we searched starting from the current directory for only files with .mp3 in their file names. We use the find command’s -exec option to execute the rename command against the result set. See the find(1) manpage for more info.

Other useful examples:

Replace all spaces with underscores
rename 's/ /\_/g' *.mp3

Replace all uppercase with lowercase characters
rename 'y/[A-Z]/[a-z]/' *.mp3

Easy stuff, and you don't even need any fancy applications to do the job!

Vim in Color

Posted on September 30th, 2007 in Tech Tips by gmendoza

Turing on colored syntax highlighting in Vim can make it easier when looking through complicated text files, scripts, and source code. To quickly turn on or off the feature, use the “syntax” command within Vim:

:syntax on
:syntax off

The results are great. Here’s a screenshot of Vim in color.

Vim in Color

To make the feature permanent, edit the file /etc/vim/vimrc, or as your Vim package maintainer may suggest, /etc/vim/vimrc.local. One can also make the change simply for their user only by editing ~/.vimrc. In all cases, simply add the following line at the end of the file:

:syntax on

Users of Ubuntu by default have vim-tiny, which as it’s name suggests, is a smaller version of the Vim editor. This version does not support syntax highlighting and a number of other features. Of course, remedying this is very easy by installing the full featured vim:

# sudo apt-get install vim

Happy editing.

Grep in Color

Posted on September 2nd, 2007 in Tech Tips by gmendoza

If you would like to make it easier to visually spot what you are looking for when using “grep”, try out the “–color” flag. This option highlights any matches in the output of your search, giving you an upper hand when trying to visually scan through complicated or cumbersome sequences.

For example, if you were looking for any IP address in your /etc/hosts.deny file that has the number “209″ in it, issue the following command:

$ grep --color 209 /etc/hosts.deny

Here is the comparison of the same output, with and without the color option.

Grep with no color Grep with color

As you can see, one might find it very easy to miss the fact that there are some IP addresses that have multiple octets with the value of 209. The color flag really comes in handy.

Split and Reassemble Files

Posted on June 3rd, 2007 in Tech Tips by gmendoza

If you ever need to work with a large file and wish you could split it into smaller pieces, you’ll be pleased to know that it’s extremely easy to do in Linux. You can use the “split” utility that comes standard with most *nix variations. Lets take a look at a couple easy examples.

To create a test file to work with, the following will create one that’s exactly 100 megabytes. Note, I am using ‘dd’ with /dev/urandom to demonstrate that the results of the split and reassembly are completely accurate. This will be accomplished via md5 hash comparisons at the end of this process.

Useful APT Aliases

Posted on June 1st, 2007 in Tech Tips by gmendoza

If you’re an avid user of Ubuntu or other Debian based Linux distributions, then you’re probably very familiar with using APT and it’s related command line utilities. You might however find it useful to create some command line aliases that shorten the time it takes to type out these repetitive tasks.

For example,

“sudo apt-get update” can be shortened to “agu”.
“sudo apt-get install” can be shortened to “agi”.
“sudo apt-get dist-upgrade” can be shorted to “agd”.