linux :: add a new hdd

e.g. /dev/sdb

partition disk:

fdisk /dev/sdb
new partition: key n
primary partition: key p
partition 1
default values
write: key w

format disk:
mkfs -t ext3 /dev/sdb1

edit /etc/fstab accordingly

convert between character sets on linux

to convert a file from utf-16 to utf-8 you can use recode (make a copy of the file first!):

recode UTF16..UTF8 myFile.txt

diff two folders on OSX

diff -qr "path/to/first/dir" "/path/to/second/dir" | grep -v -e 'Thumbs' -e 'DS_Store' | sort > diff.txt

openvpn resolv.conf

sudo rm /etc/resolv.conf
sudo ln -s /etc/resolvconf/run/resolv.conf /etc/resolv.conf

selecting a random file in the current folder

#!/bin/bash
files=(*)
echo "${files[RANDOM % ${#files[@]}]}"

using grep and xargs

open all files that contain “/sdk/” for edit:
grep -Rl "/sdk/" . | xargs p4 edit

replace all occurences of /sdk/ with /new_sdk/
grep -Rl "/sdk/" . | xargs sed -i "s#/sdk/#/new_sdk/#g"

reindex avi

if your avi index is corrupt, you can use a tool included in MPlayer (on OSX: sudo port install MPlayer)

mencoder -idx corruptAvi.avi -ovc copy -oac copy -o ReIndexedAvi.avi

replace non ascii chars in file names

for filename in *.xml; do newname=`echo $filename | tr -cd '\11\12\15\41-\176'`; mv "$filename" "$newname"; done

trace file access on OSX

OSX contains some tools for system level analysis originally developed for Solaris – one of them is opensnoop.
The tools’ descriptions can be found here: http://www.brendangregg.com/dtrace.html

using python to set mp4 tags from a csv file

using python to create the lengthy AtomicParsley calls described in the last post:

#!/usr/bin/python
import csv
import pipes
import StringIO

#metadata.csv has to contain the following rows:
#filename, show, season, episode, title, artwork, description
csvfile='metadata.csv'
csvreader = csv.reader(open(csvfile, 'rbU'), delimiter=',', quotechar='"')

class Command:
	def __init__(self, file):
		self.command = StringIO.StringIO()
		self.command.write('AtomicParsley ')
		self.command.write(pipes.quote(file))
	def addParam(self, key, value):
		self.command.write(' --')
		self.command.write(key)
		self.command.write(' ')
		self.command.write(pipes.quote(value))	
	def toString(self):
		return self.command.getvalue()
	def close(self):
		self.command.close()

def handleFile(row):
	m4vfile = row[0]
	show = row[1]
	season = row[2]
	episode = row[3]
	title = row[4]
	artwork = row[5]
	description = row[6]
	episodeCode = season + '' + str(episode).zfill(2) ##leading zeroes

	command = Command(m4vfile)
	
	command.addParam('artist', show)
	command.addParam('title', title)
	command.addParam('album', show + ', Season ' + season)
	command.addParam('artwork', artwork)
	command.addParam('stik', 'TV Show')
	command.addParam('description', description)
	command.addParam('TVShowName', show)
	command.addParam('TVEpisode', episodeCode)
	command.addParam('TVEpisodeNum', episode)
	command.addParam('TVSeasonNum', season)

	print command.toString()
	command.close()

for row in csvreader:
	handleFile(row)