Archive for September, 2009

Easily Renaming Multiple Files

Posted on September 18th, 2009 in Web Development | 1 Comment »

Update (2009-10-23): This works well for adding a prefix to files in a folder. To add a suffix to a file read “Renaming files – Adding a suffix to the file name” instead.

Here is a quick way to rename multiple files using bash.

Objective: Rename all image files in a directory in uppercase, to lowercase and append a tn_ prefix.

Step 1: Prepare a working directory

mkdir test
cd test
touch FILE_1.jpg
touch FILE_2.jpg
touch FILE_3.jpg

Step 2: Test first – print out all files in lower case

for i in *.jpg; do echo $i | tr [:upper:] [:lower:]; done

Output

file_1.jpg
file_2.jpg
file_3.jpg

Step 3: Test first – print out all files prefixed with tn_ and lower cased

for i in *.jpg; do echo "tn_"$i | tr [:upper:] [:lower:]; done

Output

tn_file_1.jpg
tn_file_2.jpg
tn_file_3.jpg

Step 4: Rename the files in the directory

for i in *.jpg; do mv $i `echo "tn_"$i | tr [:upper:] [:lower:]`; done

Done!

Hosting a git repository on dreamhost

Posted on September 15th, 2009 in Web Development | No Comments »

This last week I wanted to set up a git repository on dreamhost and came across this article on the Autopragmatic blog. The author provides easy step by step instructions to set up the repository and instructions to work with git. Here I provide a quick summary of what I consider the most important sections, if you’d like to read the original article and get a more in-depth understanding I recommend reading the original article.

Step 1: Compile git on dreamhost (change USER to your username)

mkdir ~/src
cd ~/src
wget http://www.kernel.org/pub/software/scm/git/git-1.6.5.rc1.tar.gz
tar xzf git-1.6.5.rc1.tar.gz
cd git-1.6.5.rc1
./configure --prefix=/home/USER/ NO_CURL=1 NO_MMAP=1
make
make install
git --version

Step 2: Automate repository creation. Insert this function in your ~/.bashrc. Change gitdir to point to the path where your repositories will reside.

newgit()
{
if [ -z $1 ]; then
echo "usage: $FUNCNAME project-name.git"
else
gitdir="/home/USER/git/$1"
mkdir $gitdir
pushd $gitdir
git --bare init
git --bare update-server-info
chmod a+x hooks/post-update
touch git-daemon-export-ok
popd
fi
}

Thats it!

Now, what can be done:

1. Create new repository while ssh’d into your dreamhost account: 

newgit repo-name.git

2. Create new repository remotely 

ssh USER@MACHINE newgit repo-name.git

3. Create a new repository remotely. Clone it (check it out). Create file. Add to git. Commit. Push to repository. Make another change. Commit. Push. 

ssh asamtani@amitsamtani.com newgit test.git
git clone ssh://asamtani@amitsamtani.com/home/asamtani/git/test.git
cd test
touch sample.txt
git add .
git commit -a -m "first commit"
git push --all
vi sample.txt
git commit -a -m "second commit"
git push

- password will be asked when a new repository is created, a repository is cloned and when a push is made
- when the clone is made a warning will be issued that you are cloning an empty repository 

warning: You appear to have cloned an empty repository.

- you need to pass the –all flag when doing the first push or you will get an error like this: 

No refs in common and none specified; doing nothing.
Perhaps you should specify a branch such as 'master'.
fatal: The remote end hung up unexpectedly
error: failed to push some refs to 'ssh://asamtani@amitsamtani.com/home/asamtani/git/test.git'

Again, read the original article for a more in depth explanation of what is going on and what more you can do.