Make a command to count my files

· June 1, 2015

This is a very hands-on post, since I thought the last couple of ones (including the one I just threw away) was a bit high flying.

Here we go - I have grown into a bit of a statistics maniac, especially when it comes to my blog. I’m not getting better and I like it. :)

I now found myself in a situation where I simply wanted to count the number of files in my posts directory. In the terminal of my Mac.


First I found a nice little combination of commands that did exactly that:

ls -1 | wc -l

Yeah, exactly. That doesn’t look to hard. And yes - I would never remember that either. My memory is excellent but very short.

Remember it

Luckily there’s a very simple little tool that can make commands like that easier to remember. You can create an alias with a name that’s easier to remember. Like this for example:

alias count='ls -1 | wc -l'

Sweet! Now I can go count in the current directory and get the number of files, i.e. posts. 977 by the way. Yes I’m proud.

Permanent it

However - should you close the Terminal window and then, tomorrow, open it again you will be sorry since the count command now is gone. This is a bit surprising and also disappointing.

Well the answer is, with OS X / Linux measures, relatively easy to fix. Open your ~/.bashrc file in a text editor (for example with sublime ~/.bashrc) and add your alias in the end of the file.

Like this:

alias count='ls -1 | wc -l'

Now you can close the Terminal, reboot your computer or what ever you fancy and the count command will still be present.

Parameterize it

Now I realized that I actually have to cd into the directory I wanted to check. And apparently alias doesn’t support parameters to be passed.

But that’s easy to fix… Convert it to a function like this:

function count() {  ls -1 "$1" | wc -l; }

The $1 is the first parameter passed to the function. The last ;-char is important, or you’ll get a syntax error.

This is good because now I can do, for example this:

count ./Projects/blog/

And see my glorious 977 (sorry) posts again. But I have to supply a parameter. It would be cool if it used the current directory is nothing was supplied. Well… that can be done like this:

function count() {  ls -1 "${1:-.}" | wc -l; }

As everything Linux the commands are terse and super powerful, but basically the form is {parameter:option}. In the option part we give a - which translates to “if not supplied”, or default value. In our case it’s just the current directory, ..

Put that into your ~/.bashrc, save it and restart your Terminal. And now you can go:

marcus$ count 
marcus$ count Projects/blog/


Summarize it

I love diving into stuff that I know little about. I will not make commands like this everyday but you never know when I (or you ^^) will look here again.

Twitter, Facebook