Archive for February, 2012

Unix shell Hackery

Saturday, February 4th, 2012

I was helping a friend try to set up a git repository of a rather large existing software project.  He loves guis, and the github gui tool was choking on the initial commit/upload.  Here is the series of shell commands I sent him via email to execute.  It’s pretty cool what you can do with unix and how it lets you communicate.

$EXT is the root directory where you have stored the existing code base

# copy everything over to a completely new directory
1. cp -r $EXT ~/th2

# cd into that directory
2. cd ~/th2

#how much space is that directory taking
3. du -h | tail

#remove the git part of that directory
4. rm -rf ~/th2/.git

#how much space is it taking up now
5. du -h | tail

# how many files are in this whole directory tree
6. find . | wc -l

# what are the most common file extensions,  change -n30 until you think it's giving a reasonable report
7.  find . -type f | grep -v git |   sed 's/\(.*\)\([\/.][^\/]*$\)/\2/'  | grep -v "^\/" | sort | uniq -c  | sort -nr | head -n 30

#create a file size by type script
8.
echo "#/bin/bash" >> size_by_extension.bash
echo "size=`find . -type f | grep "$1$" | xargs -L1 ls -lrt  | awk '{sum+=$5} END{print sum}'`" >> size_by_extension.bash
echo 'echo "$size $1"' >> size_by_extension.bash

./chmod +x size_by_extension.bash

# what file types use the most space
9. find . -type f | grep -v git |   sed 's/\(.*\)\([\/.][^\/]*$\)/\2/'  | grep -v "^\/" | sort | uniq | xargs -L1 ./size_by_extension.bash  2> /dev/null  | sort -nr 

There may be more elegant ways to get some of this done, but all in all it is pretty cool. imagine trying to communicate a similar workflow with gui oriented commands.