Real programmers’ secrets – why to use the command line
Command line. Some are afraid of it. Some hate it. And some just love it.
I am neither of them. I consider tools I use in my everyday live to be just tools and I use them if they do solve the problem. If I need a graphical design – I will probably use the Photoshop or Gimp. If I need to edit a text I may use Word. If I need to work with Maven dependencies I will use the Command line.
So let’s treat it, the Command line, just as any another tool or program. Gimp. Word. Command line. When you need Word, you use Word. When you need Command line, you use Command line. Does it make sense? It is not something from the other world. It is a tool.
When to use a command line
Then comes the next big question – when to use the command line. Well, it is usually not used by simple users who just browse the internet or make a PowerPoint presentation. It is mostly used by developers and system administrators to automate or speed up certain tasks. It is like digging the ground yourself or let an excavator do it.
I personally have used it and find it a perfect tool for the following:
- running commands of Git (which is a code version control system saving the history of your code)
- automation scripts and helper scripts
- package managers (Maven, Node package manager)
- connecting to the server, executing the commands there, editing text files there
- taking advantage of the developer tools – such as Phonegap, Grunt, Less, Sass, Javascript minification, etc.
- Developing and configuring the web: server configuration, WordPress, Drupal development and management.
If you don’t know some of the tools above – don’t worry, I am planning on covering these in the upcoming articles. Most of them are just amazing helpers that will make your life easier.
Choose the right software
As with any tool or software program, you may find that you need or don’t need it. For example, if I want to format a nice article and print it out later, Microsoft’s Word is a good choice. If I want to make a simple Todo-list for the day, I may just use a simpler program – Notepad (Windows), TextEdit (Mac) or a special Todo-app. Similarly with the command line: if you use a Microsoft Visual Studio to develop a .NET application, you will probably have most of the needed tools there – e.g. code version control or code minification tools. And to build the application, you just need to press the “build” button. On the other side, if you develop a web application that is using Less (as promised, more on it in upcoming articles), your good bet is to run the command line commands for automating and converting it into a minified CSS.
Are you a Microsoft guy? Don’t head out yet. Do you know that if your underlying version control program is Git, then Visual Studio will actually run Git commands when you add or commit the files? And when you press the Build button the equivalent of the following Command line command is executed (as can be seen here):
So, hint hint: stay with us to get a better understanding of what’s going on under the hood of the command line!
Where is this command line?
Let’s say this. On the computers with Linux and on Macs (they are sometimes referred to as “*nix” or Unix-like operating systems) the command line is an integral part of the environment. Therefore it is quite natural to run it in these environments. You just need to run a program called Terminal (on some Linux versions it is called Konsole). A good summary of how to start it on different Linux versions is here. On your Mac just find Terminal app in your Applications or type “Terminal” in the spotlight search to find it.
On Windows it is a bit another story. To start the command line there, you need to start a program called Command Prompt. On latest versions of Windows (like Windows 8) just start typing “Command Prompt” or “cmd.exe” while being on the apps window. On older versions start the Run command from the start menu, type in “cmd.exe” and press OK. This will start the command prompt. The problem is that it has less functions and advantages as compared to the Command line on the *nix environments mentioned above. For example, it does not have the “view”, “grep” or “vim” commands built in. However in many cases you can just use other Windows tools for such tasks.
There are also software tools that try to help overcoming this issue on Windows and create a more *nix-alike command line (e.g. Cygwin). But for many tools that we are interested in as developers – Git, Grunt, Maven etc – the standard Command Prompt works fine or there are other solutions (e.g. Ruby command line powershell allowing you to run Sass).
What the command line really does?
The whole trick is to understand what the command line really does. As we agreed, it is a tool. And it does the following: executes the commands you type in. That’s it and nothing else. You write the command and press return/enter button. The command line executes it:
Let’s take an example. You want to start the Microsoft Word program. Let’s assume it’s executable is in the “C:\Program Files\Microsoft Word\Word.exe” location.
Option one: in your visual folder structure, change to the directory with the executable file and double click its icon (well, we also have shortcuts on the desktop – but it is a shortcut to the executable above).
Option two: in the command line type in the following: “C:\Program Files\Microsoft Word\Word.exe” and press Enter key. It will start the Word as well!
What is the big deal with that? Can’t I just open the Word with a double click? Yes, you can. But there are many other commands that are written specifically to be ran on the command line. Those are the ones you cannot run with a double click. You HAVE TO use a command line.
Let’s take version control software Git as an example. Assuming I have Git installed (it is not installed by default on some systems), I can run the following:
git commit -m “my message”
You cannot run the above command otherwise than in the command line. You cannot run Git software otherwise. It is a command line tool. There ARE graphical tools allowing you to accomplish the same result – TurtoiseGit, GitExtensions (Windows), SourceTree (Mac) and others. But make no mistake – all of these tools are NOT Git. They USE Git in the background to “save” you from the command line. Now I saved you from that.
That’s cool, but do I need it?
As with anything in your life, it depends. Do you need a car? Probably you can get out without it in the city. However if you live in the suburbs, it can be very convenient.
Similarly with the command line. Do I need it to develop the WordPress theme? If I only do it for a hobby project I can get out without it. However if I do it for an important project or develop the themes for living, it can be much more convenient to use the command line. Why? It will give you the speed, automate the tasks and (optionally) can even increase your self-esteem. No kidding.
Let’s face it. Will you minimise the Javascript if it is not done automatically? Will you combine many CSS files into one? I must confess: I have never done it before I came across the command line tools like Grunt helping with that. I wasn’t even aware how simple it can be. You will see how simple it is to use the command line yourself. Just hang around until other articles. Cheers!
Update: you can now learn how to use the Command Line in our recent post.
Sources of images:
Wikipedia
http://publicdomainvectors.org/en/free-clipart/Linux-shell-terminal-window-vector-clip-art/13013.html
https://openclipart.org/homepage
http://dir.coolclips.com/Industry/Farming/Crop_Farming/Harvesting/Planting/man_digging_hole_peop2119.html
http://www.freedigitalphotos.net/
https://imgflip.com/memegenerator/Jackie-Chan-WTF
123rf
http://www.bezambee.com/smiley_preview/sigh/359
Leave a Reply