How to know the size of multiple folders on Linux/Unix

Folder IconOften while I’m administrating a Linux/Unix box I come across the tediousus task of freeing up space on the server, usually this task is easily accomplished by issuing a simple “ls -lh” command, you get a nice listing of the files in the current directly along with some nifty details like last modification date and size (the -h parameter for ls allows us to get the size in a human-readable way).

The downsize of doing it this way is that you don’t get to know for real the size of the folders within the current directy, you get a nice total on top of the “ls” listing. A nice command to get the size of a certain folder is issuing a ds

du -sh folder/

This way you’ll get the following result:

pepper:~ pepper$ du -sh perrohunter.com/
694M    perrohunter.com/

You can see that the folder perrohunter.com weights a total of 694 MB. (yeah, that much).

But what if you’d like to know the size of all the folders within the current folder? Piece of cake, just use the * wildcard and that’d do the trick:

du -sh *

And you should get a result similar to the following:

pepper:~ pepper$ du -sh *
628K    application
8.0K    cache
40K    codeigniter
3.6M    database
156K    fonts
180K    helpers
4.0K    index.html
68K    language
536K    libraries
8.0K    logs
36K    plugins
76K    scaffolding

You can see the folders now have the total size of them in MB. Hope this is helpful for you.

Repair a Mac OS X HFS+ Partition table

At the time of writing this post I must say that I feel like a hero! That feeling is wonderful you know, when someone has a problem with their computer and not a simple but a HUGE one and you are able to repair em you turn to be that people hero.

Today my friend Avaco12 made a little mistake while installing bootcamp on her iMac, she left her external (200GB) disk connected, and when Windows asked her where to put the new Partition she accidentally selected her preciousus external drive, when she realized it was already too late, windows had already destroyed the partition table on her disk. She had 1 HFS parition and 1 FAT. She was crying because she said she had her entire life on that disk, so she asked me for help and I started researching what could I possibly do to repair the disk or at least retrieve her data.

There are plenty of solutions out there, some of them are really expensive, and people on the forums are not very happy with them so I wanted to look at little more into the issue and I came across this solution called TestDisk which is an Open-Source Multi-platform solution for repairing disk partitions. I gave it a try and awesomely I managed to fix the disk using TestDisk and the pdisk utility.

When I called Avaco12 to tell her that I fixed her data she was so happy she couldn’t stop smiling she told me I was her Hero and so that’s how I felt, the process wasn’t easy specially because noone talks about how pdisk works on Mac OS X.

Here’s how I did it , hope it works for you.

1.- I downloaded TestDisk from their official site :http://www.cgsecurity.org/wiki/TestDisk

2.- I opened the terminal and surfed to where I downloaded and unziped TestDisk

3.- I issued the command ‘sudo ./testdisk’ it indetially promped me to make my terminal bigger =P, after doing so It displayed me a simple disclaimer and then a notice telling me that TestDisk could Log all the activity if I wanted to. I selected Yes and continued.

4.- On the screen it will display you all the Disk it detects connected to your Mac select the one that got damaged partition tables and press Enter. Take note of which drive you selected, in my case I chosed /dev/rdisk1 (rather than /dev/disk1)

5.- Next screen select the menu Analize, it will probbly display you and error telling you the block 0 couldn’t be read, just hit enter on the ‘Quick Analize’ option

6.- ATTENTION: TestDisk will quickly search for partitions on the disk and display you information about it on your disk. Take note of the information it will display you, since you’ll need it to repair the disk later on with pdisk.

Here’s a screenshot of what I got:

 

 

Note: if you try to repair your partition table with TestDisk it will fail since that function is not yet implemented.

7.- you can now exit TestDisk. The next steps are what distinguish a child from a Men

8.- Issue the command ‘sudo pdisk /dev/rdisk1’ (where /dev/rdisk1 should be the same name you chosed on step 4). If you type the command ‘c’ and hit enter it will tell you the following:

9.- That’s completely normal. Now type ‘i’ and it will display you some affirmations about block sizes and such, just hit enter:

 

10.- Now we are going to need the info from Step 6. type the command ‘c’ and press enter, it will ask you to type down where your first partition starts, how long it is and how you want to name it. repeat this step for every partition you have.

 

11. Now if you are completely sure you wrote down everything correctly (like I did) just type down ‘w’ and hit Enter, It will prompt you to confirm just say yes (y)

12. type ‘q’ to quit the application and go see for yourself if your disk appears now in Finder. If it does, go to Step 13. else try disconnecting your external hard disk and reconnecting it. After Mac OS X 10.6.7 it is suggested that you restart your machine and reconnect the disk. If this fails try again from step one. Don’t worry about rewriting the partition table it doesn’t harm your data.

13. You are a hero, no matter if it’s your own disk, you just saved your data!

UPDATE:

If you are having problems with an error message on pdisk saying “the map is not big enough” follow these steps:

Before the analyze step go to GEOMETRY and change the sector size according to the following table:

Volume Size Default Block Size
<=256 MB 512 bytes
256 MB <= 512 MB 1024 bytes
512 MB – 1 GB 4096 bytes

Now go back to the ANALYZE step continue from there. Thanks to Stan Alien for the heads up.

Congratulations, and I hope this information is usefull to you now remember to backup your data! Personally I recommend Backblaze.

You can buy me a drink if you’d like to share your joy 🙂




cheers !

How to kill a process by it’s PID on Windows

I don’t want to sound mean but, God, MSDN API sucks =P. Not because it doesn’t tell you the stuff you need to know but because it requires alot of imagination from you to understand it.

For example, I was looking for a way to kill a process using it’s process id ( PID) however it turn that when you are programming for WIN32 you need to use HANDLE’s for everything so if you’d like to have a process that creates process and then kill them you’d have to store and manage every handle.

Now I don’t lik handling Handle and pointers I like to keep it simple using integers, an array of integers to me is way easier to use than an arrat of handles since pasing pointers and casting and etc.. is not my passion on C++. Thus I found a way to kill processes by their PID using this nice simple function:

void kill_by_pid(int pid)
{
	HANDLE handy;
	handy =OpenProcess(SYNCHRONIZE|PROCESS_TERMINATE, TRUE,pid);
	TerminateProcess(handy,0);
}

Hope this works for you

Cheers

Parsing <contend:encoded> attribute from a wordpress feed with simplexml

wordpressThat was a long title for this post o_o. Well , I first must clarify that my blog is not a wordpress mod, however it doesn’t mean I don’t have to work with wordpress at some point in the day. Recently I was doing a newsletter for a Website and I decided I would parse the RSS from that website to get the 4 newest contents however I ran into a trouble while using PHP’s SimpleXML class and this was that I SimpleXML couldn’t parse the contentn since it came with what is called namespace. For those of you who only came in here for the solution here it is =)

Replace <content:encoded> for <content> 😉

this is a simple example on how I did it

 

	$data = file_get_contents(“http://myurls.com/feed.xml”);
	$data = str_replace("content:encoded>","content>",$data);
	$xml = simplexml_load_string($data);
	$item = $xml->channel;
	
	foreach($item->item AS $art)
	{
		$content = $art->content;    
		echo $content;
	}

and thats it.

For those of you who kept reading I’m gonna tell you the solution to this problem, it’s indeed quite easy. Indeed a namespace tell us that what’s inside that tag is encoded in some other weird standard but don’t worry, the same feed it’s telling you what it is. if you see at the begining of the wordpress feed you’ll see this:
 

			

Javascript Regultar Expression for matching Domains on a string

Today I had a hard time creating a regexp to match domains on a string. I wouldn’t like the internauts who visit this small blog to suffer the same pain, so here’s how to :

var StringWithData = ” asd http://subdomain.perrohunter.com/lol/something.php asd”;
re = /http://w+([.-]?[a-z]+)*.com/;
var results = re.exec(StringWithData);
alert(results[0]);

results will be an array of matches so if you only want the first match use the 0 position, else loop the array for what you need.

Ecoute: a lightweight iTunes replacement

Sometimes I feel like iTunes is a little bit too demanding on system resources, it happends to me from time to time that I start feeling my Mac a little bit slow and then I check my ActivityMonitor.app and iTunes is eating up to 1.5GB of my RAM (out of my 4GB :P) that’s alot of memory for a simple music player. After surfing the net for a nice solution I came across Ecoute.

Ecoute seems to me a really nice lightweit music player, the first thing that I like about Ecoute is that it plays your music right out of your iTunes library so you don’t have to manage 2 librarys when syncing with your iPod/iPhone, I liked that since you only have to download this 3.9MB app unzip it, open and you are ready to go.

This app also has some nice interface features that I found fascinating. The first one I’d like to talk about is that it looks and behaves just like and iPod touch interface, you have a listing of your songs and you can browser them by Song, Artist, Album and they are sorted alphabetically, you can even rate your songs quickly.

The second thing I liked about it’s interface is that while you are playing a song you get this nice cover-album art instead of the song listing just like in the iPod.

What I’m implying here is that if you are familiar with an iPod touch you are really going to feel comfortable with Ecoute since it’s usability is quite the same as the ipod touch.

The third feature about Ecoute that I like is that it’s integrated with Growl and when a new song is playing it displays a Growl notification so you know what song is currently playing, and not only that it also adds a nice cover-album art player to your desktop wallpaper so you can change your song straight from your desktop. That’s nice specially when working with multiple spaces.

It also integrates search capabilitys and an menu which stays on the top bar so you can easily change the song, however what I consider to be the Killer feature is that it allows you to set Keyboard shortcuts, as you might guess I’m more of a keyboard person and I really like having all the work done without the use of my mouse and Ecoute allows you to easily set keyboard shortcuts to to display the player window, go to next song, play/pause,Mute/unmute and search. I really like this feature since I believe is something that iTunes is missing.

Lastly.fm but not least 😛 it also has Last.fm integration so you can scrobble your plays 🙂 so If you like scrobbling this app would be a 2 in 1 ! yaaay! more free RAM for us RAM lovers.

And the best of all you can download Ecoute and give it a try for free, the licence is only 10 Dlls which for me is a cheap price to pay for such a good quality app. So point your browsers at http://www.ecouteapp.com/ and give it a try, you have nothing to loose 🙂

How to install Plesk 9 on Ubuntu 8.04 LTS

44.pngI love administrating servers but when it comes to fulfilling my clients need I wish I had some magic software that could help me manage the server resources and everything. Well my iPhone does this … nah I’m just kidding obviusly Plesk is the way to go since thats the title of this post.

When I found what was Plesk capable off I was amazed because it offers alot of features that can help you automate your bussiness however there is one thing that I don’t like about it: “Plesk documentation sucks” literally. And i’m sure you agree with me and that’s probably why you are here.

When I wanted to install plesk I started by choosing an OS for it, so I chosed Ubuntu Server 8.04 LTS because I’ve been seen all this Ubuntu hype and I wanted to give it a try so here’s my log of what happened.

First I downloaded the whole Plesk installer but really quick I found this useless since it was a bunch of .deb packages that had no instructions on which order I should follow to install it.

Second I downloaded the plesk auto installer but oh surprise it didn’t work either, the problem was I havn’t installed MySQL 5 first. Here I tryed with a simple apt-get but that didn’t work so I had to install first the sources I needed and then update apt-get repo and finally try to install MySQL 5.1 so here’s what you should try first

On your terminal using your favorite editor edit this file and add the following lines:
#nano /etc/apt/sources.list

and at the very bottom add
deb http://ppa.launchpad.net/monty/ubuntu gutsy main universe restricted multiverse
deb http://ppa.launchpad.net/smurf/ubuntu gutsy main universe restricted multiverse

Press Ctrl-X and Y enter to save and exit

Do both updating and upgrading to make sure apt-get works as it should
#apt-get update
#apt-get upgrade

Get mysql
#apt-get install mysql-server-5.1

If you happend to have troubles at this step try

#apt-get -f install

which should force the instalation of whatever else is needed and finish the installtion

After that browse to where you downloaded your Plesk auto installer , for example I saved my auto intalled to /home/perrohunter/parallels_installer_v3.4.1_build090120.13_os_Ubuntu_8.04_i386

So first add it execution permissions:

#chmod +x parallels_installer_v3.4.1_build090120.13_os_Ubuntu_8.04_i386

and now if you want everything to go smooth for you try installing Plesk using it’s web interface

#./parallels_installer_v3.4.1_build090120.13_os_Ubuntu_8.04_i386 –web-interface

this will prompt you to open your server hostname at the port 8447 on any browser, I had some problems using the hostname so I used the IP

htt://172.16.12.131:8447

from there everything was fine, hope this tips help you get straight to the solution 🙂

cheers