How to identify and locate large files and folders

When it happens that your VPS is eating data by the second and there is disk read/write issues one port of call you are bound to visit is searching and identifying large files on your system.

Now, you would have been forgiven for thinking this is a complicated procedure considering some Linux Bash solutions for fairly simple things, but no. Linux Bash wins again!

du -sh /path/to/folder/* | sort -rh

Here, du is getting the sizes and sort is organising them, -h is telling du to display human-readable format.

The output should be something like this:

2.3T    /path/to/directory
1.8T    /path/to/other

It does take a while to organise as it is being done recursively however given 3-5mins and most scenarios will be fine.

A memorable way to Find and Replace recursively

So yeah, getting used to Bash is about finding the right way to do things. However, learning one-liners and picking up information here and there is all very useful, finding something you don't need to Google in order to recall is extremely important.

Take the case of recursive find and replace. We've all been there, it needs to be done frequently but you're either a) scared or b) forgetful. Then you use the same snippet from a web resource again and again and eventually make a serious error and boom, simplicity is now lost on you!

So here it is, something you can remember so that you don't use different methods depending on what Google throws up today.

grep -Rl newertext . | xargs sed -i 's/newertext/newesttext/g'

Notice, we use grep to search -R recursively with results fed to xargs which does a simple sed replace with g global/infinite properties.

Say you want to keep it simple though and find and review the files before doing the "simple" replace. Well, do this.

grep -R -l "foo" ./*

The uppercase -R denotes to follow symlinks and the ./* indicates the current directory. The -l requests a list of filenames matched. Neat.