No I don’t hate the command line… maybe I used to a little bit… OK maybe more than that… but it doesn’t have to be that way.
For most of my 15 years developing software I’ve been spoiled by the tools available in Windows. I started out my career developing windows applications using Visual C++. I later adopted C# with the later version of Visual Studio. Now currently working with Java but still in a Windows environment even though our application is hosted on Solaris (Unix).
In those 15 years I haven’t had the need much for the command line, outside of Git and Node. Even with Git and Node there’s only a few commands that you must know to used those tools effectively. The tools available for a windows developer are really good and don’t require command line knowledge to be effective.
However, I have noticed a trend as of recent years where more and more developers are turning to developing on Macs and embracing the command line. I’ve also noticed there is a vast world of hosting solutions on Linux and Unix systems. The most common ways to access these systems is to SSH into them and use the command line. Even Microsoft is aware of the deficiency in the windows command line and they are trying to do something about. They tried for years, at least I belive powershell was an attempt at that, but they are coming face to face with the reality that the Unix command line is hard to match.
For someone coming from a pure Windows world where you can remote desktop on to the server and point and click to do what you need, it can feel almost crippling to move around the command line. The bash command line is extremely powerful and that can make it very intimidating. I think what makes base so intimidating is due to it’s it “can do everything under the sun” philosophy. There’s entire books around single bash commands (that should give you and idea of how much sophistication some of these commands posses).
Point and click may be where you start your career but if you do this software development thing long enough you will be expected, explicitly or implicitly, to know the command line. Considering just how much of the web is hosted on *Nix systems you will find yourself in the command line soon enough. It wouldn’t surprise me if in 5 years down the road Microsoft completely adopted the command line or if 99% of developers moved towards the Unix/Linux world for their development environments.
No… I don’t really hate the command line. Intimidated by it might be more accurate. I do hate being getting thrown into unpleasant waters and expected to make my way out. It’s not a pleasant experience.
I will admit that I’ve been aware of it for many years but chose to ignore it. In college I found PowerPoint slides of commands and switches boring and extremely cryptic. Now fifteen years later, I’ve decided to demystify it because it’s long overdue.
So… no I don’t hate it. In fact lately I’ve come to embrace it. Over the past few weeks I’ve made Linux Mint my main personal system and it has given me ample opportunity to play around with the command line.
Recently in two occasions at work, it paid off to know a little something about the command line. One was where I needed to get a closer look at the folders for a Word Press site we host. The other was related to a issue with a issue of command line parameters with special characters. Based on the little knowledge I had, I was able to finagle my way around these issues.
If you are timid about the command line and would like to know more, let me share a few guidelines that have made it click for me.
Commands You Must Know
Navigating your way around the file system requires some essential knowledge. To navigate the file system you must be able to move forward, backwards, up and down. You must be able to find what you are looking for. Sometimes you will need to inspect the contents of a file. Other times you may need to edit the file but before you edit it you must copy it or move it. Other times you may need to execute a file. Here is my list of essential commands to empower you with these essential tasks.
There are many more commands that you will find extremely useful and empowering as you progress, but I have found the ones below vital.
*Note: ignore the # preceding each command. It’s just a safety mechanism to prevent accidental execution of the file containing these commands.
#pwd -tells which directory you are in the command line
#ls -al -gives you a listing of the contents of the directory
#cd -lets you change directory. Without any arguments it places back at your home directory
#cd / -lets you navigate to the root of your system
#cd /etc -takes you the root etc folder. /etc is just an example for now
#cd myDir -takes you to the child directory within the directory that you are already in
#cd .. -takes you up one directory. You can combine .. with other directory segments
#cat myfile -allows you to dump the contents of the file onto the screen safely. I say safely because, unlike vi, there is no chance of you modifying the contents. Combine cat with more to allow scrolling the file like this cat myfile | more. When you use more and there is more scrolling involved than you care to scroll through remeber that you can use q to quite the more sessions. Notice that | is called a pipe. One of the beauties of nix systems is that you can redirect the output of one command as the input to the next command. When you are ready piping is worth investigating closer.
#cat myfile | more -more allows you to scroll down the contents of the file starting from the beginning
– you can use more with any other command that provides output such as ls
#vi myfile -you can use the vi editor to edit the file. Before you use this command make sure you take a tutorial on using vi. You should feel comfortable to exit vi with and without saving. Before you open a file with vi make sure that you know how to exit without saving. You must enter command mode and execute :q!. To save the file without exiting :w! and to save and exit :wq!. It can be confusing so here is a good tutorial on the Unix vi Editor
#nano myfile -nano is an editor available to use. It is much simpler than vi. You can use it to easily edit a file. It lists all the commands at the bottom of the editor screen so you don’t have to memorize all the shortcuts like you do with vi editor.
#cp thisFile overToThisFile -allows you to copy thisFile to overToThisFile
#mv thisFileName newFileName -allows you to move a file from one directory to another or simply rename it in place
#apropos keyword -If you are interested in doing something but don’t know the command you can use apropos and it give you a list of all commands that use the keyword you use in their description.
#man command -you can view the man pages for that command. Although for a beginner the man pages can actually be confusing. So, instead you may just choose to let google be your friend instead. You can usually scroll through man pages. Once you are ready to quit out of the man page you are in use q to quit.
#echo $PATH -This gives you a listing of directories where the system looks for command to execute.
#which command -Gives you the location of the command you are querying about (e.g. which grep will likey produce /bin/grep).
#exit -if you have SSHed in, use this command to exit your session
#chown -to change file ownership
#chmod 444 myfile -Use this command to modify the file permissions of a file. Each position is for a different entity (owner, group, other).
The values in each of these positions can be any of the following: 7, 6, 5, 4, 3, 2, 1. Since rwx would be 111 which is the binary represenation for 7.
rw- would be 110 which is the binary represnation for 6. r-x would be 101 which is the binary representation for 5. Hopefully you get the picture. Basically 4 is read, 2 is write, and 1 is execute. You can combine any of those three values to combine the read write execute permissions. For example read and write would be 4 + 2 which gives 6, which gives you rw-
#sudo -Hopefully you are not logged in as root. There are many reasons why you should not be logged in as root. If you are really curious read here. Sudo gives you the ability to execute a command that you would otherwise not be able to execute. So, for example, you could issue the command sudo chmod …. At that point you would be prompted for the password in order to execute the command.
#mkdir mydir -to create a directory.
#touch myfile -to create a file.
# > -you can use this to redirect the output of a command to file. This creates the file or overwrites it, if it already exists. For example ls -al > mylsresults.txt would create the file mylsresults.txt with whatever came our of ls -al
#>> -use it the same way as > but it will append to the file instead
Once you are ready to go beyond the essentials you can use a good command cheat sheet like the unix linux command line reference to further your knowledge.
Here are some very important things to remember as you navigate the command line.
Always save a copy of whatever file you are about to edit
It’s extremely easy to fudge a file using the vi editor. Even if you don’t fudge it, you may want to revert back to the original file. It is much better to have a backup of the file before you edit it. *Hint: Use the cp command to back it up.
What’s in a command anyway?
Commands such as grep, ls, etc are nothing more than small binary files akin to .exe files in Windows. Unlike Windows, Unix doesn’t care about file extensions. Actually the norm in Unix is for executable files not to have extensions. The command line has capability to run executable as well as different flavored scripts (e.g. BASH, PERL, Python). Most scripts usually have the extension indicating their type such as .sh (for shell scripts), .py for python and so on.
You can also run scripts by navigating to the script location and preceding it with ./. For example let’s say I am in my home directory and have a script called backup.sh. I can call it by typing ./backup.sh.
Keep in mind that in order to execute a script it must have execute permissions assigned for the entity that I belong to. For example if I am the owner it mus be -rwx——. Ignore all the values except the x. It must have the x if I am the owner otherwise it will not execute. This can trip up the windows user since Windows does not have an implementation of permissions like this.
Know the basic folder organization of a *nix system
Here is a good link that explains them Linux file system . Knowing about these can help go a long way in navigating the system. Knowing what is stored where may help you find what you are looking for. For example I was looking for the WordPress site and I was able to find my way by first visiting /var which led me to /var/www/html. My second option would have been /opt. Later I restarted the servers but the WP site didn’t come up so that led me to /etc/init.d to find the start up scripts for mysqld and apache2. I didn’t find apache2 but I found a htmld which was the script for starting apache.
Understand about file permissions and reading the output of the ls command
The ls -al gives you a listing that provides a lot of information but it doesn’t do much good if you don’t know how to read it. Here is a good link for understanding how to read the output of ls -al What do fields in ls output mean?.
Understanding the file permissions goes a long way. I’ve seen in many occasions where bugs are reported with our application and the real reason is a folder or file did not have the right permissions. Your folders must have the right permissions in order for your program to have access to them.
Be very cautious about the rm command
The rm command is essential for deleting files and directories but you can easily wipe out data and not be able to recover it. Make sure you fully understand it before you use it. I didn’t even list the command ls, as a precaution.
Let Google be your friend
If there is something that you want to do in the command line the easiest way to find out how to do it is by searching for it on the internet. Just about anything you can think of can be done with the command line in a Unix system. You can monitor the system, start up processes, kill processes, compress and archive, you can install and remove programs, update your system, you name it. I am not sure if there is anything that you can’t do through the command line in Unix. I guess that’s why it’s so common to access Linux/Unix systems through SSH.
That means that if you want to do something and don’t know how to do it, you are only a search away. The commands listed here will have you walking, the results from the internet will have you running.
Just be careful to not take any destructive commands right from the internet if you don’t understand them. By destructive I mean anything that modifies your file system in any way.
Leverage your powers!
Once you get beyond navigating the command line and learn the essentials you will discover that you have an awesome power that many nix administrators lack. You know how to program. I assume that you know how to program since you are in the beginningprogrammer.com blog. Anything you can do in the command line you can actually script. You can either learn bash scripting or learn a more programmer friendly language like python or PERL. Here is a good tutorial to get started on shell scripting.
Where and how can you practice the things I just talked about?
The best way to practice is in a virtual environment. If you are using windows there are tools such as GitBash and Bash for Windows but I don’t recommend them since they are small subsets of the true nix environment. Using those systems won’t allow you to use all the features in a nix system.
My recommendation would be to load Ubuntu on a virtual box. Before you load it you will need the iso image which you can download online for free. Once you download the iso you will need to burn the image to a DVD disc. Windows has built in capability for this. It’s important that you burn the image and not simply extract the contents. On Windows you can right click and select burn to disc or copy to disk. I can’t verify right now since I’m on a Linux box.
If you have a more than one machine you can play around from the live CD. Most Linux distributions are loaded from a live CD, which means you can run Linux right from the CD without installing it. The beauty of it is that you can still access your hard drive from these live CDs.
If you are beginning programmer, don’t make the mistake I did by letting all these years go by before you learn the Unix command line. If you are a seasoned programmer this is the perfect time to get on board.