Terry Harvey

7 Useful Command Line Tips

When I first started using the command line, everything was completely alien. CLI? Vim? Grep? None of it meant anything to me! And while there were plenty of resources online to get me started, there didn’t seem to be any crash-course or run-down on basic commands and techniques. So in this article (which is aimed at beginners with a little experience in the command line), I’ll demonstrate some of the tips and tricks I use on a day-to-day basis.

1 – Show Current Directory Path (pwd)

Often when darting around projects (especially when multitasking), it’s handy to quickly double-check which directory you’re currently in. You can do this with the pwd command. This will output the full path to the current directory.

2 – Line Numbering (nl)

This is a command I use fairly frequently, but doesn’t seem to be very well known. Want to quickly output the contents of a file with line numbers, but don’t want to have to open up your text editor? Well luckily, nl has got you covered. Simply runnl <filename> and you will be promptly presented with something like this.

1  var elixir = require('laravel-elixir');
2  /*
3  |--------------------------------------------------------------------------
4  | Elixir Asset Management
5  |--------------------------------------------------------------------------
6  |
7  | Elixir provides a clean, fluent API for defining some basic Gulp tasks
8  | for your Laravel application. By default, we are compiling the Sass
9  | file for our application, as well as publishing vendor resources.
10 |
11 */
12 elixir(function(mix) {
13     mix.sass('app.scss');
14 });

3 – Command Aliasing (alias)

If you find yourself typing the same long-winded commands over and over again, it may be a hint that you should set up an alias. Take the following example:

$ vendor/bin/phpunit --colors

In order to run my unit tests, I’m going to be running this command fairly regularly and I don’t want to have to type it out each time. Yes, you can simply use the arrow keys to cycle through previous commands, but if you’ve run a lot of commands recently, this will take just as long. Why not just set up an alias?

$ alias t="vendor/bin/phpunit --colors"

Then all I have to do is simply run t, and the full command will be executed.

4 – Searching Content of Files (grep)

Need to find a particular piece of code, knowing it could be anywhere in the massive abyss of files that is your codebase? Allow me to introduce you to my friend, grep. Say I want to find the file in which an str_slug() function is declared. I can do just that with the following command:

$ grep --ignore-case --recursive 'function str_slug' *

It’s fairly self-explanatory, but let’s quickly run through the anatomy of the above.

  • grep – this is the command name – obviously.
  • --ignore-case – tells the command to make the search case-insensitive.
  • --recursive – tells the command to search recursively, so it will automatically enter any directories and search the files within them.
  • 'function str_slug' – this is the search pattern. This is what the command will look for in the files it searches.
  • * – search in all files and directories (within the current directory). This can also be a file name or directory path.

And just FYI, we can condense the option names and shorten the command to this:

$ grep -ir 'function str_slug' *

There is much more to the grep command, so I’d recommend giving this linfo.org page a read if you’re interested!

5 – Repeat Previous Command (!!)

The amount of times I’ve tried to execute a command, only to be presented with an “access denied” error, is greater than I care to admit. Then I have to use the up arrow to enter the previous command into the window and hold my left arrow key until I’m at the beginning to prefix the whole thing with sudo. Well, next time you run into this issue, try simply running sudo, followed by two exclamation marks like so: sudo !!. And this works with any command!

6 – Output to File (>)

We all know how to output a string to the terminal (echo ‘foo’), but what if we wanted to put that into a file? Well, we could create the file by running vi example.txt, then entering the content and saving, but what if I could tell you that this could be done with one command? Try this:

$ echo 'foo' > example.txt

If you quickly run ls, you should see example.txt listed. If you open up the file in your text editor (or alternatively, use cat example.txt to quickly output the contents of the file), you should see the text “foo”.

7 – Get Contents of Web Page (curl)

And for my last trick, we’ll take a quick look at Curl. Try the following command in your teriminal:

$ curl http://terryharvey.co.uk

You should see the contents of my homepage. Now, while that is probably not helpful on its own, why not try combining it with the trick we learned in #6?

$ curl http://terryharvey.co.uk > example.html

Et voila! I will continue to keep this article updated as I think of more useful tips and commands for beginners. If you have any requests, please don’t hesitate to email me via my contact page!