Introduction
In this article we're going to look at some terminal commands/tricks that I find useful during my day to day job as a software developer.
Before we do, you should at least know the basics, I'm assuming you know what the following commands are for and are comfortable using simple commands like these:
cd
, ls
, mkdir
, touch
, grep
With that out of the way, let's move on to some commands you may not know about and how they can be used.
The history
command
The history
command shows you a full history of every command that you've entered into your terminal. There's a limit to how much is kept in your history buffer, but this is configurable.
The history
command is commonly used with grep
, allowing you to view a subset of history matching a specified pattern.
For example, say you wanted to see all npm
commands recorded in your history, you can use the history
and grep
commands to achieve this:
history | grep 'npm'
This will output a list of npm
commands:
$ history | grep 'npm'
...
2649 npm i glob --save-dev
2704 npm i handlebars --save-dev
2706 npm i mkdirp --save-dev
...
You can then re-run any of the listed commands using the pattern !#
where #
represents the id of the command you want to run. For example, to run the npm i handlebars --save-dev
command again you would enter !2704
.
The reverse-i-search
command
The reverse-i-search
command isn't a conventional command (is it even a command?) because you don't type the command into a terminal to execute it, but it's extremely powerful and definitely worth knowing about.
The reverse-i-search
command is a history related command, but instead of listing history it allows you to search backwords through your history, looking for commands in history that match the keyword(s) you've entered. Essentially it's an autocomplete for commands in your history buffer.
It's best understood by using it, inside your terminal hit CTRL+R
to start the search, now start typing some letters and matching commands will be presented to you. When you see the command you want to execute hit enter.
You can also view similar commands by repeatedly hitting CTRL-R
, for example, if you searched for npm
it would show you the last npm
command you entered. Hitting CTRL-R
again would then step back through related npm
commands.
The find
command
The find
command is great for searching for files or folders matching a specified pattern. For example, to recursively search for all .json
files in the current directory you'd enter the following command into the terminal:
find . -type f -name "*.json"
The .
represents the path that we want to search within, in this case the current path.
The -type
option is used to specify the type that you're searching for - in this case we're searching for files so we passed f
.
Finally the -name
option allows you to provide a pattern to search for, in this case we're searching for any .json
files.
The ag
command
You'll likely have to install ag
, but it's definitely worth the effort. It's a super fast file search tool, optimised for searching source code files. For example, to search for all instances of PersonController
in the current directory you'd enter the following command:
ag PersonController .
Here the first argument is the pattern that we want to search for and the second argument is the path that we want to search within, in this case we've specified the current path by passing .
.
The alias
command
The alias
command is great for creating new abbreviated commands for the commands you use often.
You use the alias command like this:
alias name "command"
For example, I often use these these aliases when browsing the file system:
alias p "cd ~/projects/"
alias c "clear"
alias l "ls -la"
alias q "exit"
There are lots of other useful aliases too, for example there are some common aliases for git commands available on the web.
Summary
It's worth getting comfortable with the command line, it can be very rewarding and the productivity gains can be massive. It takes a little getting used to, but you just have to be persistent - it's worth it in the end.
These are just some of the commands that I use, there are many more that I've not included and even more that I don't know about. It's a constant learning process, but it's one worth pursuing.