Finding out the size of directories in Linux

Do you want to know the size of a directory in Linux? Use the du command.

du -sh

Explanation

The du command estimates the space used by a directory.

The options -sh are (from man du):

  -s, --summarize
         display only a total for each argument

  -h, --human-readable
         print sizes in human readable format (e.g., 1K 234M 2G)

To check more than one directory and get the total size of those directories, use

du -sch *

What does the extra option mean?

  -c, --total
         produce a grand total

Include hidden files?

du -sch * .*

Want to know how much disk space has been used?

du -h .

Bash shell shortcuts

I have been irritated not having standard keyboard shortcuts, like Ctrl-arrow left (move word left) and Ctrl-arrow right (move word right), available on the bash command line. It turns out they are. If you know what the meta key is. Meta key? Yes, I didn’t know which key this was either.

It turns out that it is the Alt key. This is because historically, many Unix workstations had a key labeled Meta where PCs have a key labeled Alt.

So here is a little bash command line cheat sheet.

  • Ctrl-a: Move to the start of the current line.
  • Ctrl-e: Move to the end of the line.
  • Ctrl-f: Move forward a character.
  • Ctrl-b: Move back a character.
  • Meta-f: Move forward to the end of the next word. Words are composed of letters and digits.
  • Meta-b: Move backward to the start of the next word. Words are composed of letters and digits.

Have fun.

Finding out which files are open and by which programs

Grrrrrrr ran into my inotify problem again. So I was wondering how I can see which program has files opened and how many. Well as with everything under Linux it is possible with a few “simple” commands. Try the following command for fun and entertainment.

$ lsof | awk ‘{print $1}’ | sort | uniq -c | sort -g -k 1 | tail

  • lsof: lists the open files
  • awk: takes the first column of the output
  • sort: yep it sorts the output
  • uniq: counts all the same value
  • sort: do you want me to repeat it? a little clarification is necessary -k tells which column to sort on and -g makes sure that the contents is treated like a number
  • tail: gives the last ten entries

Inotify limit reached under PT Magic

Today I ran into a problem with PT Magic (PTM) that the inotify limit was reached on Ubuntu. Inotify (inode notify) is a Linux kernel subsystem that acts to extend filesystems to notice changes to the filesystem, and report those changes to applications. Just as a future reference for myself and as a reference to others a very quick write up how to solve this.

You can get your current inotify file watch limit by executing:

$ cat /proc/sys/fs/inotify/max_user_watches

You can set a temporary new limit with:

$ sudo sysctl -w fs.inotify.max_user_watches=16384

If you like to make your limit permanent use:

$ echo fs.inotify.max_user_watches=16384 | sudo tee -a /etc/sysctl.conf
$ sudo sysctl -p

That’s it. The problem should be solved. At least this is the workaround. I have submitted an issue to TP Magic so that they can fix it properly. We will see.

Validating a json file from the command line in Linux

Today I needed to validate a json file on one of my servers. It turns out there is a simple nodejs program or actually a linter to do this with.

Just execute the following line and you are in business.

sudo npm install jsonlint -g

This assumes nodejs is already installed. If not, execute the following line.
sudo apt-get install -y nodejs npm

To validate a json file run the following command.

jsonlint -qc settings.analyzer.json

When running the program the first time I ran into the problem that the json file contains comments. Yes, I know this is not in line with the spec, but it is damn handy for understanding the file. So I needed to strip the comments to valiate the file. After googeling around a bit I found an awk command to do just this. Try the following command.

awk '{sub(/\/.*$/,"")}1' settings.analyzer.json > settings.analyzer.json

Now run jsonlint again and it should work.

P.s. I know the title of this post says Linux, but jsonlint will also work under any other system that nodejs runs on.

Hope this saves someone some time.