Command line for a newbie

Basics of command line explained. Functionality you should become familiar with.

What development can you do with the command line

The command line interface has become useful for scaffolding development projects in less time and with less effort.

With tools like git offering a simple but powerful version control interface it is becoming very important to add the command line to your toolbelt.

There are multiple online package libraries from which to download and install using the command line.

For php there is packagist.

For python there is pip.

For front end packages like angular or jquery and much more, there is npm.

The basics

So here’s a quick overview of the beginner stuff.

Change your location

When you open your command line tool, you will be in a particular folder on your computer. For windows you may be in C:\ and for ubuntu you may be in ~/

To change your location you can use command cd

cd [path]

Windows

Eg: cd C:/Projects/

Linux

Eg: cd ~/Projects

View files in current directory

As you move around the different folders on your computer you will want to see what other folders or files are within the folder you change to.

For Linux you will use the command ls:

ls

For Windows you will use the command dir:

dir

These will return a list of files and folders in the current folder.

You can combine this with a folder path in order to see a list of files and folders in a folder you are not currently in.

Linux:

ls [folder location]
Eg: ls /var/log

Windows:

dir /a [folder location]
Eg: dir /a C:/

Creating directories

Create a directory in the current location.

mkdir

If you want to create a directory in a different location you will need to use cd to go there.

Create a file

Ubuntu:

touch [filename]
eg touch index.html

Windows:

There are multiple ways to create a file in windows. You will just need to select the approach that works for you. Below are two of the various ways to create files on windows.

echo 2> [filename]

fc >> [filename]

Alternatively you can use notepad:

notepad [filename]

This will open up notpad which will then ask you if you would like to create a file named [filename]

Rename a file

ren

ren [current filename] [new filename]

Delete files

For Linux use rm

rem [filename]

For Windows use

DEL [filename]

Keep learning

This is only the tip of the iceberg. The command line is a very powerful interface and can save you a lot of time.

Get to know the hosts file

Find your hosts file

In windows your hosts file is located:

C:\WINDOWS\system32\drivers\etc\hosts

For windows you will need to open an editor as administrator to edit the file. Also you may need to disable your anti virus as it may prevent you from editing your hosts file. Just remember to enable the anti virus again as soon as you are done.

In linux your hosts file is located:

/etc/hosts

For linux you will need to use sudo to edit the hosts file.

Experiment with the hosts file

Add at the bottom of your hosts file:

127.0.0.1 moo
127.0.0.1 baa
127.0.0.1 moobaa

In your command line you can now ping these:

ping moo
ping baa
ping moobaa

Each one will show something similar to:

Reply from 127.0.0.1: bytes=32 time<1ms TTL=128

Then remove the lines we just added to the hosts file, and ping them again.

You will receive an error message similar to:

Ping request could not find host baa. Please check the name and try again.

Practice some more

Lets practice using an existing IP like google. First lets find google’s IP:

ping google.com

The result will return 216.58.223.36.

In your hosts file add this line at the bottom:

216.58.223.36 googlefun

Now ping googlefun:

ping googlefun

You will see that you have now pinged the google IP address.

What can you do with the hosts file?

You can point a domain name to an ip. You may want to do to block your computer from communicating with a certain domain name or just have custom domain names set up for fun.

Connect via ssh

Open ssh client then insert command:

ssh username@hostname

Trouble shooting ssh connections

If you’re struggling to connect to ssh, ping the domain name or ip address:

ping [domain/ip]

IP Addresses preset in the hosts file

In some cases you may set the hosts file for a domain name and IP address. If your ssh connects to an incorrect domain/ip, look at your .hosts file and check if you are pointing your ip and domain names correctly.

Server out of memory

I recently dealt with a server that stopped adding to its logs. It was as if the logs were frozen. After a lot of investigating it was found that the server had run out of memory.

Troubleshooting

Check your server space with the following command:

df -h

Investigate where the main memory is being used. This is very often in the /var/log/ folder.

If you would like to see the sizes of the files in a folder use this command:

ls -l  [folder]

eg: ls -l /var/log

If you would like to the see the sizes of folders:

du [folder]

eg: du /var/log

Clear up space by deleting files and folders

To delete a file use the following command:

sudo rm [filename]

To recreate a blank file use the following command:

sudo touch [filename]

Unable to find what is using server space

If you try to delete a file that is still in use by a service, the file will not delete off the server, but also not be displayed in the folder structure.

To investigate if there are any deleted files still in memory use this command:

lsof | grep ‘(deleted)’

You will see a list of files still in use with an id.

Use the following command to “kill” the file using the indicated id value:

kill -9 [id]

Plan your code

I have a mantra at this current point in my life:

If you need documentation to explain your code, your code is too complicated, but if you can’t create the documentation with ease, your logic is too complicated.

I have been reading “Introduction to systems analysis and design, an agile iterative approach”. And it has reinforced that mantra in the way I approach my development.

Planning is a very important part of development. I encourage all developers to pause and take some time to plan your code before you actually put any code to screen.

Also take the time to learn how other developers plan their work. Many use UML as a solution for planning their projects.

When you plan you allow for a big picture approach. Think about the code as a piece to a whole.

The logic should feel simple by the time you start adding code.

Documentation is good to have, especially if you need another developer to take over a project. It lessens the bus factor. I find documentation has become less as a means to explain code, and more of a means to understand the logic of the whole project.

The zen of python, by Tim Peters

Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren’t special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one– and preferably only one –obvious way to do it.
Although that way may not be obvious at first unless you’re Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it’s a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea — let’s do more of those!

View your command history

Forgot a useful command you used recently?

View your history of commands you have typed in linux by just typing in “history” in the command line tool:

history