Linux shell scripting – 2

11 Jan

My previous post was about using rsync to mimic Apple’s Time Machine; I actually set that up some time ago but just didn’t get round to writing about it.

It’s now been running successfully for over a month and I wanted to find out how much space does it save when you just create hard links to files rather than backing them up. You can’t just use the du command because it counts the files as if they were actually there but find is a command which can help.

If you type:

find . -links 1 -type f

Then you will get a list of files in the current directory which have just one link to them (ie they’re not linked from any other directory so they are “today’s” backup)

that list is very nice but tells me nothing about size. find can take a -exec option which then runs a command for each file

find . -links 1 -type f -exec ls -l {} \;

lists the files in long format so you can see their size but I don’t really want to try and add up all those sizes!

awk will allow me to do some processing on these results:

find . -links 1 -type f -exec ls -l {} \; | awk '{print $5}' 

Here, we’re piping the output of the ls command into awk and getting it to print the fifth column (the size)

Still not ideal; I really want it to add up those numbers for me!

find . -links 1 -type f -exec ls -l {} \; | awk '{ print $5 }' | awk '{total = total +$1}END{print total}'

This takes the long list of numbers and pipes it to another awk command which just adds each number to a variable called “total”. Once it’s finished, it prints that variable.

So, if you run that final command at the top of a directory tree containing new files and hard links to previous files you’ll ge a number – that’s the space taken up by files actually in that directory. Run a du-s for the same directory and you’ll get the total space Linux thinks is in use (ie including all the files that are really just hard links). The difference between the 2 is the space saved by doing this backup.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: