A standout among the most helpful, yet under-utilized, tools a web developer has is the command-line. The terminal frequently frightens people away; so here’s the place we demonstrate some of the most helpful day-to-day commands.

Show Current Directory Path (pwd)

Often when dashing around projects (particularly when multitasking), it’s helpful to rapidly double-check which directory you’re at present in. You can do this with the pwd command. This will provide the full path to the present directory.

Line Numbering (nl)

This is a frequently used command, yet doesn’t look to be very well known. Want to rapidly output the contents of a file with line numbers, but would prefer need not to open up your text editor? Well, fortunately, nl has got you covered. Basically, run nl <filename> and you will be quickly given 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 });

Command Aliasing (alias)

On the off chance that you end up writing the same long-winded commands, again and again, it might be a clue that you ought to set up an alias. Take the following illustration:

$ vendor/bin/phpunit –colors
In order to run the unit tests, make the use of this command fairly regularly and you don’t have to type it again and again. Yes, you can simply make the use of arrow keys to cycle via commands but if have run a number of commands recently, this will take pretty much as long. Why not simply set up an alias?
$ alias t=”vendor/bin/phpunit –colors”
At that point all you have to do is simply run t and the full command will be executed.

Searching Content of Files (grep)

Need to search a specific bit of code, knowing it could be anywhere in the huge abyss of documents that is your codebase? No need to worry as grep is here for you. Let’s assume you need to find the file in which a str_slug() capacity is declared. You can simply do that with the below-given command:
$ grep –ignore-case –recursive ‘function str_slug’ *
It’s fairly self-explanatory, but let’s have a quick 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 present directory). This can also be a directory path or file name.
And just FYI, we can consolidate the option names and shorten the command to this:

$ grep -ir ‘function str_slug’ *

Repeat Previous Command (!!)

There may be times when you try to execute a command but it only comes up with an “ access denied” error. Then make the use of up arrow to enter the previous command into the window and hold the left arrow key until you are at the starting to prefix the entire thing with sudo. And next time if you go through this issue, simply try running sudo, trailed by two exclamation marks like so: sudo!!. This works with any command.

Output to File (>)

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

$ echo ‘foo’ > example.txt
You simply run Is, you should see example.txt recorded. Now, if you open up the file in your text editor (or then again, utilize cat example.txt to speedily get the contents of the file), you should see the content “foo”.

ServerPronto offers the best affordable and secure hosting service in all dedicated server packages.

Comments are closed.