Symmetric Key Encryption with GnuPG

Posted by admin on November 4, 2009 under Tech Tips | Be the First to Comment

If you ever want to quickly protect a file by encrypting it with a simple password, you can use GnuPG and symmetric key encryption for the job. Using this method, you can use industry strength encryption like AES256 and not have to worry about public and private keys. Just remember your password and use PGP compatible software to decrypt the files when needed.

For example, this is how you can encrypt a zip file called backup.zip and output the result to a new file called backup.zip.gpg.

gpg --symmetric --cipher-algo aes256 -o backup.zip.gpg backup.zip
Enter passphrase: *******
Repeat passphrase: *******

To decrypt the file, the following will work.

gpg -d -o backup.zip backup.zip.gpg
gpg: AES256 encrypted data
Enter passphrase: *******
gpg: encrypted with 1 passphrase

For fun, here’s how to create a Gzip Tar archive (tar.gz) and encrypt it on the fly.

tar czvpf - SomeFiles/ | gpg --symmetric --cipher-algo aes256 -o backup.tar.gz.gpg
Enter passphrase: *******
Repeat passphrase: *******

To decrypt and extract in a single command, the following also works.

gpg -d backup.tar.gz.gpg | tar xzvf -
gpg: AES256 encrypted data
Enter passphrase: *******
gpg: encrypted with 1 passphrase

If you’re curious to know what other ciphers are available to you, simple use the gpg --versioncommand.

gpg --version | grep Cipher
Cipher: 3DES, CAST5, BLOWFISH, AES, AES192, AES256, TWOFISH

Quickly Identify Video File Attributes

Posted by admin on November 3, 2009 under Tech Tips | Be the First to Comment

If you want to gain quick insight into the basic properties of a video file from the Linux command line, there’s a few really easy methods. The information you might be interested in are audio and video codecs, resolution, frame rates, bitrates, etc.

Using mplayer, you can see some basic information about a video file. For example, the following video is using the WMV3 video codec at the resolution of 1280×720 (720p), the bitrate of 3000 kbps, etc.

mplayer video.wmv -vo null -ao null -frames 0 2>&1 /dev/null | egrep "(VIDEO|AUDIO)"
VIDEO: [WMV3] 1280x720 24bpp 1000.000 fps 3000.0 kbps (366.2 kbyte/s)
AUDIO: 44100 Hz, 2 ch, s16le, 96.0 kbit/6.80% (ratio: 12003->176400)

The audio rate is shown to be 44100 Hz using 2-Channel stereo, but the codec is not listed. You can get additional information with the -identify option.

mplayer video.wmv -identify -vo null -ao null -frames 0 2>&1 /dev/null | egrep "(^ID|VIDEO|AUDIO)"
ID_AUDIO_ID=1
ID_VIDEO_ID=2
VIDEO: [WMV3] 1280x720 24bpp 1000.000 fps 3000.0 kbps (366.2 kbyte/s)
ID_CLIP_INFO_NAME0=name
ID_CLIP_INFO_VALUE0=
ID_CLIP_INFO_NAME1=author
ID_CLIP_INFO_VALUE1=
ID_CLIP_INFO_NAME2=copyright
ID_CLIP_INFO_VALUE2=
ID_CLIP_INFO_NAME3=comments
ID_CLIP_INFO_VALUE3=
ID_CLIP_INFO_N=4
ID_FILENAME=video.wmv
ID_DEMUXER=asf
ID_VIDEO_FORMAT=WMV3
ID_VIDEO_BITRATE=3000000
ID_VIDEO_WIDTH=1280
ID_VIDEO_HEIGHT=720
ID_VIDEO_FPS=1000.000
ID_VIDEO_ASPECT=1.7778
ID_AUDIO_FORMAT=353
ID_AUDIO_BITRATE=0
ID_AUDIO_RATE=0
ID_AUDIO_NCH=0
ID_LENGTH=2116.00
ID_VIDEO_ASPECT=1.7778
ID_VIDEO_CODEC=wmv9dmo
AUDIO: 44100 Hz, 2 ch, s16le, 96.0 kbit/6.80% (ratio: 12003->176400)
ID_AUDIO_BITRATE=96024
ID_AUDIO_RATE=44100
ID_AUDIO_NCH=2
ID_AUDIO_CODEC=ffwmav2

Another great tool for identifying video attributes is idvididvid comes with the tovid package, so be sure to install it from your repository. It is a bit slow, but the output is very clean and easy to interpret.

idvid video.wmv
--------------------------------
idvid
Video identification script
Part of the tovid suite, version 0.31
http://www.tovid.org
--------------------------------
Analyzing file: 'video.wmv'. This may take several minutes...
=========================================================
File: video.wmv
Width: 1280 pixels
Height: 720 pixels
Aspect ratio: 1.77:1
Frames: 63352
Duration: 00:35:13 hours/mins/secs
Framerate: 1000.000 frames per second
Video format: WMV3
Video bitrate: 3000000 bits per second
---------------------------
Audio track 1 (Stream 0.0, AID 0):
---------------------------
Codec: wmav2
Bitrate: 0000 bits per second
Sampling rate: 44100 Hz
=========================================================
Audio is compliant with the following formats:
Not compliant with (S)VCD or DVD
Video is compliant with the following formats:
Not compliant with (S)VCD or DVD
This video does not seem to be compliant with (S)VCD or DVD
standards. If you burn it to a video disc, it may not work.
=========================================================

Finally, another tool that works very well is exiftool. It’s available in the libimage-exiftool-perl package. The output is also very straight forward, and easy to interpret. It’s also a lot faster than idvid because the tool only displays metadata, which in some cases can be misleading.Mplayer and tovid actually probe the video, providing very accurate information at the cost of speed.

exiftool video.wmv
ExifTool Version Number : 7.30
File Name : video.wmv
Directory : .
File Modification Date/Time : 2009:04:20 09:21:58
File Type : WMV
MIME Type : video/x-ms-wmv
File ID : 12341234-1234-1234-1234-123412341234
File Size : 823200182
Creation Date : 2009:03:31 02:52:55Z
Data Packets : 102833
Play Duration : 35:16
Send Duration : 35:13
Preroll : 3000
Flags : 2
Min Packet Size : 8000
Max Packet Size : 8000
Max Bitrate : 3122812
Is VBR : False
Audio Codec Name : Windows Media Audio 9.2
Audio Codec Description : 96 kbps, 44 kHz, stereo 1-pass CBR
Video Codec Name : Windows Media Video 9
Video Codec Description : Professional
Audio Codec ID : Windows Media Audio V2 V7 V8 V9 / DivX audio (WMA) / Alex AC3 Audio
Audio Channels : 2
Audio Sample Rate : 44100
Stream Type : Video
Error Correction Type : No Error Correction
Time Offset : 0 s
Stream Number : 2
Image Width : 1280
Image Height : 720
Title :
Author :
Copyright :
Description :
Rating :
Image Size : 1280x720

As always, comments and additional tips are welcome!

Extract Audio from Video Files to WAV using Mplayer

Posted by admin on October 11, 2009 under Tech Tips | Be the First to Comment

You can extract the audio from a video file using mplayer and save the result to a WAV file, which you can then manipulate to your hearts content. For example, you may want to compress the audio to a stereo MP3 or OGG.

The following command instruct that the audio output (-ao) should be redirected out to a PCM WAV file as fast as possible, while suppressing all video output.

mplayer -ao pcm:fast:file=audio.wav -vo null -vc null video.avi

Convert the resulting WAV to MP3. The following is a great way to convert your WAV files to a high quality Variable Bitrate MP3. See the man page for a decent tutorial on the available options.

lame -V0 -q0 --vbr-new audio.wav audio.mp3

Add Stereo Audio Tracks to MKV Files

Posted by admin on September 26, 2009 under Tech Tips | Be the First to Comment

If you have Matroska Video (MKV) files encoded with AC3 Dolby Digital 5.1 or DTS audio tracks, you may want to simply extract the audio, convert it to a 2-channel stereo format like WAV, MP3 OGG, etc, and then add it back into the MKV as a separate audio track. This is useful when your media player (e.g. Western Digital Media Player WDAVN00) will not downscale the audio from a digital format like AC3 or DTS to stereo when you don’t have a receiver or TV with a built in Dolby Digital decoder.  Now you’ll have the choice of either audio format depending on your technical requirements.

The great thing about the Matroska multimedia container is that you can easily manipulate these files without having to re-encode, saving lots of time. I’ll be using mkvextract to extract the AC3 audio, ffmpeg to convert ac3 to mp3, and finally mkvmerge to add and remux the new audio track to the MKV container. All of these are available to a number of platforms, but in my examples, I’m using Linux.  Check out the MKVToolnix and FFMpeg websites for more info on the software.

If using Ubuntu Linux, install the relevant mkvtoolnix,  mkvtoolnix-gui and ffmpeg packages.

sudo apt-get install mkvtoolnix mkvtoolnix-gui ffmpeg libavcodec-unstripped-52

To view the existing tracks of the MKV, use the mkvmerge -i option. In the following example, you see my “Cool.Video.mkv” file has an MPEG4 video in track 1, an AC3 Dolby Digital audio file in track 2, and subtitles in track 3.

mkvmerge -i Cool.Movie.mkv 
File 'Cool.Movie.mkv': container: Matroska
Track ID 1: video (V_MPEG4/ISO/AVC)
Track ID 2: audio (A_AC3)
Track ID 3: subtitles (S_TEXT/UTF8)

Using mkvextract, extract the AC3 Dolby Digital audio from track 2, saving it to a file called audio.ac3.

mkvextract tracks Cool.Movie.mkv 2:audio.ac3
Extracting track 2 with the CodecID 'A_AC3' to the file 'audio.ac3'. Container format: Dolby Digital (AC3)
Progress: 100%

ls -lh audio.ac3
-rw-r--r-- 1 gmendoza gmendoza 432M 2009-09-26 11:58 audio.ac3

Convert the 6-channel ac3 file to a 2-channel stereo MP3 using ffmpeg. If you prefer a higher audio bitrate, adjust the -ab value as desired. e.g. 256, 384, etc, and adjust the audio rate to your liking as well.

ffmpeg -i audio.ac3 -acodec libmp3lame -ab 160k -ac 2 audio.mp3
[output omitted for brevity]

ls -lh audio.*
-rw-r--r-- 1 gmendoza gmendoza 432M 2009-09-26 11:58 audio.ac3
-rw-r--r-- 1 gmendoza gmendoza 87M 2009-09-26 12:08 audio.mp3

To simplify things, you could actually skip the digital format extraction process by running ffmpeg against the MKV file directly.

ffmpeg -i Cool.Movie.mkv -acodec libmp3lame -ab 160k -ac 2 audio.mp3

If you prefer encoding with more advanced options, you could extract the audio as a 2-channel WAV file instead, and then process it with LAME, Oggenc, or some other encoder of your choosing. The following shows the extraction to WAV, and then conversion to various formats for fun, e.g. MP3, OGG, and FLAC.

ffmpeg -i Cool.Movie.mkv -acodec pcm_s16le -ac 2 audio.wav
lame -V0 -q0 --vbr-new audio.wav audio.mp3
oggenc -q6 audio.wav
flac audio.wav

Use mkvmerge to combine the original MKV with the MP3 audio track to create a new file called Cool.Movie.New.mkv. Make sure you have enough disk space for both the original and new MKV file.

mkvmerge -o Cool.Movie.New.mkv Cool.Movie.mkv audio.mp3
mkvmerge v2.4.1 ('Use Me') built on Dec 13 2008 21:03:46
'Cool.Movie.mkv': Using the Matroska demultiplexer.
'audio.mp3': Using the MP2/MP3 demultiplexer.
Warning: 'audio.mp3': Skipping 32 bytes at the beginning (no valid MP3 header found).
'Cool.Movie.mkv' track 1: Using the MPEG-4 part 10 (AVC) video output module.
'Cool.Movie.mkv' track 2: Using the AC3 output module.
'Cool.Movie.mkv' track 3: Using the text subtitle output module.
'audio.mp3' track 0: Using the MPEG audio output module.
The file 'Cool.Movie.New.mkv' has been opened for writing.
Progress: 100%
The cue entries (the index) are being written...
Muxing took 270 seconds.

Verify that the audio track has been added. You can see Track ID 4 has been successfully added.

mkvmerge -i New.Cool.Movie.mkv 
File 'New.Cool.Movie.mkv': container: Matroska
Track ID 1: video (V_MPEG4/ISO/AVC)
Track ID 2: audio (A_AC3)
Track ID 3: subtitles (S_TEXT/UTF8)
Track ID 4: audio (A_MPEG/L3)

That’s really all there is to it. There are quite a few options available when editing MKV container files. For example, I wanted nice descriptions for my tracks since various media players will read and display them for you during menu navigation. I recommend using the mkvmerge gui application as shown in this screenshot.

mkvmerge-gui

It’s really just a front-end application to mkvmerge, and the following text shows the commands that were used to specify the language for each tag, re-order the audio tracks, disable subtitles by default, and give useful descriptions to each Track ID.

mkvmerge -o "Cool.Movie.New.mkv" \
--language 1:eng \
--track-name "1:Cool Movie (MPEG4)" \
--default-track 1:yes \
--display-dimensions 1:40x17 \
--language 2:eng \
--track-name "2:Dolby Digital 5.1 (AC3)" \
--default-track 2:yes \
--language 3:eng \
--track-name "3:English Subtitles" \
--default-track 3:no \
-a 2 -d 1 -s 3 Cool.Movie.mkv \
--language 0:eng \
--track-name "0:2-Channel Stereo (MP3)" \
--default-track 0:no \
-a 0 -D -S audio.mp3 \
--track-order 0:1,0:2,1:0,0:3

mkvmerge -i Cool.Movie.New.mkv
File 'Cool.Movie.New.mkv': container: Matroska
Track ID 1: video (V_MPEG4/ISO/AVC)
Track ID 2: audio (A_AC3)
Track ID 3: audio (A_MPEG/L3)
Track ID 4: subtitles (S_TEXT/UTF8)

RAR and UNRAR from Linux CLI

Posted by admin on September 23, 2009 under Tech Tips | Be the First to Comment

Two great packages available to the Linux community are RAR and UNRAR. If you are already familiar with the RAR compression format, these allow you to create, modify and extract RAR archives. For those of you that appreciate the graphical compression application for Gnome called File Roller, these packages enable it to read RAR files. For more information about the RAR format, check out rarsoft.com.

To install from Ubuntu or Debian, its as simple as:

sudo apt-get install rar unrar

To list the files in a RAR archive, use the l or v option:

rar l video.rar

To decompress a RAR archive called video.rar, simply issue the command:

unrar e video.rar

The unrar package is only intended for decompression tasks. Otherwise, just use the rar command with the e option to extract the files to the current directory.

rar e video.rar

To compress a single file called video.avi, adding it to a RAR file called video.rar:

rar a video.rar video.avi

To compress a single file called video.avi, splitting it into approximately 50 Mb files:

rar a -v50000 video.rar video.avi

This by default creates archive files starting with video.part01.rar, video.part02.rar and following the sequence to completion. To use the older and better known extension sequence format of .rar, .r00, .r01, etc, use the -vn option like so:

rar a -v50000 -vn video.rar video.avi

To compress an entire directory recursively, use the -r option:

rar a -r Documents.rar Documents/

To extract the files from an archive, such that the original directory structure is also recreated, use the x command:

rar x Documents.rar

There are so many other features available to the RAR format, so be sure to check out the man pages or the packaged documentation.

man rar
less /usr/share/doc/rar/rar.txt.gz

(if your version of less supports reading of compressed files)

Fixing Dates in Image EXIF Tag Data from Linux

Posted by admin on June 21, 2009 under Tech Tips | Be the First to Comment

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 by admin on April 9, 2009 under Tech Tips | Be the First to Comment

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 get the job done with mencoder.

In the following example, I decided to use a single pass, fixed quantizer value of 4. The audio will simply be copied.

mencoder movie.mkv -channels 6 -ovc xvid -xvidencopts fixed_quant=4 \
-vf harddup -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.

NOTE 1: This is not an exhaustive or definitive post on quality retention. This is just an easy way to re-encode a source video file to Xvid.

NOTE 2: MKV is only a container file format, meaning that you store audio and video tracks within an MKV file, as well as a number of other data types. e.g. Subtitles, Pictures, Fonts, etc. Many times, these video and audio tracks may already have been encoded with a codec supported by your media player. You could potentially extract the appropriate audio and video tracks with mkvextract (a component of the mkvtoolnix package), and recombine them into a container format supported by your platform. This is a great option because you would not have to re-encode, saving time and quality loss. I’ll update with more examples later.

File Synchronization with Unison over SSH

Posted by admin on April 7, 2009 under Tech Tips | Be the First to Comment

Previously, I posted on using rsync over SSH for file synchronization. While this works very well when pushing data in one direction, it’s not well suited for synchronizing modifications that are made on both sides. An excellent bidirectional utility for that type of job is Unison, which sports many of the same benefits as rsync, but has some distinct advantages for more complex synchronization scenarios.

A basic example would be to synchronize a local directory called “MyDocs” with a remote SSH server. From the following output, you can see that this directory contains four text files.

ls -ld ~/MyDocs
drwxr-xr-x 2 gmendoza gmendoza 4096 2009-04-09 16:05 /home/gmendoza/MyDocs

ls -l ~/MyDocs
total 12
-rw-r--r-- 1 gmendoza gmendoza 31 2009-04-09 16:09 file1.txt
-rw-r--r-- 1 gmendoza gmendoza 31 2009-04-09 16:09 file2.txt
-rw-r--r-- 1 gmendoza gmendoza 31 2009-04-09 16:09 file3.txt
-rw-r--r-- 1 gmendoza gmendoza 31 2009-04-09 16:09 file4.txt

The first time you run Unison for this particular directory structure, both sides will create a local index and hash table. You’ll get a warning and will be prompted with a message, asking you to hit the space bar if you accept. If the root directory on the remote side does not exist yet, you’ll also be prompted to accept the changes.

unison MyDocs ssh://host2/MyDocs
Contacting server...
Connected [//host1//home/gmendoza/MyDocs -> //host2//home/gmendoza/MyDocs]
Looking for changes
Warning: No archive files were found for these roots, whose canonical names are:
/home/gmendoza/MyDocs
//host2//home/gmendoza/MyDocs
(snipped for brevity...)
Press return to continue.[] Waiting for changes from server
Reconciling changes

local host2
dir ----> / [f]

Proceed with propagating updates? [] y
Propagating updates

UNISON 2.27.57 started propagating changes at 16:14:30 on 09 Apr 2009
[BGN] Copying from /home/gmendoza/MyDocs to //host2//home/gmendoza/MyDocs
[END] Copying
UNISON 2.27.57 finished propagating changes at 16:14:30 on 09 Apr 2009

Saving synchronizer state
Synchronization complete (1 item transferred, 0 skipped, 0 failures)

Subsequent synchronizations are shown as the following.

unison MyDocs ssh://host2/MyDocs
Contacting server...
Connected [//host1//home/gmendoza/MyDocs -> //host2//home/gmendoza/MyDocs]
Looking for changes
Waiting for changes from server
Reconciling changes
Nothing to do: replicas have not changed since last sync.

For the following example, I have modified file1.txt on host1, and file2.txt on host2. Both file3.txt and file4.txt have been modified on each side. The great thing about unison is that when there is a conflict, you have the opportunity to view the differences and select which direction you wish to synchronize. Pressing the “x” key displays some basic information about the files that differ. In this case, I have chosen the files with the most recent timestamp. You choose the file direction by pressing the greater and less-than symbols, “>” and “<“.

unison MyDocs ssh://host2/MyDocs
(snipped)
local host2
changed <-?-> changed file3.txt [] x
local : changed file modified on 2009-04-09 at 16:16:29 size 50
host2 : changed file modified on 2009-04-09 at 16:16:43 size 55
changed <==== changed file3.txt [] <
changed <-?-> changed file4.txt [] x
local : changed file modified on 2009-04-09 at 16:17:20 size 56
host2 : changed file modified on 2009-04-09 at 16:16:59 size 41
changed ====> changed file4.txt [] >
changed ----> file1.txt [f]
<---- changed file2.txt [f]

Proceed with propagating updates? [] y
Propagating updates

UNISON 2.27.57 started propagating changes at 16:18:27 on 09 Apr 2009
[BGN] Updating file file3.txt from //host2//home/gmendoza/MyDocs to /home/gmendoza/MyDocs
[BGN] Updating file file4.txt from /home/gmendoza/MyDocs to //host2//home/gmendoza/MyDocs
[BGN] Updating file file1.txt from /home/gmendoza/MyDocs to //host2//home/gmendoza/MyDocs
[BGN] Updating file file2.txt from //host2//home/gmendoza/MyDocs to /home/gmendoza/MyDocs
[END] Updating file file3.txt
[END] Updating file file2.txt
[END] Updating file file4.txt
[END] Updating file file1.txt
UNISON 2.27.57 finished propagating changes at 16:18:27 on 09 Apr 2009

Saving synchronizer state
Synchronization complete (4 items transferred, 0 skipped, 0 failures)

Unison also has a GTK front end for the graphically inclined. Be sure to check out all the documentation for a full understanding of syntax.

File Synchronization with Rsync over SSH

Posted by admin on April 6, 2009 under Tech Tips | Be the First to Comment

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 by admin on under Tech Tips | Be the First to Comment

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 [email protected]

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