Basic Shell Commands for Developers

Guillaume Montard
mythoughts.io
Published in
4 min readJan 21, 2014

What kind of shell commands a developer should know? Here is list of the one I personally use very often and think every developers should know.

What is a shell?

The shell is the user interface to access your OS and run commands (code, scripts, daemons etc.). We talk about Unix Shell in this document. Unix shell is the command line tools available on your Linux or OS X.
(more on the subject)

Is there different kind of shells?

Yes. Most used shells are Bash and ZSH. Bash is the one shipped by default for most *Nix system. If you see the shell as a software (yes it is!), all of them have the same goals (running commands etc.) but each of them provide unique/different features as auto-completion etc.
(link to go further)

Why a developer needs a shell?

Writing code involve accessing low level information on your OS. It also involve using lots of geeky technology only available as shell commands. If you write code but don’t know what is an OS, how to call commands then you are missing something big!

The very basic commands

ls, cd, mv, ln, mkdir, chmod, sudo

The most useful command

man

Yes this is it ! man for Man pages or Manual pages. When you want to use a command dig into it to understand what it does and how to use it. This is maybe the MOST important command that everyone should run at least once a day when using a shell.
(So yes man is a command line tool, also!)

Sadly there is no command — IWantToDo “how to do this”. Google is your friend.

My top 6 commands

touch, cat, tail, grep, sed, find

Touch

One way of using touch is for creating empty file without entering into any editor and having to quit, save etc.

Cat

Cat is simply used to output the content of a file(s) on the command standard output.

A simple use case, adding a public SSH key to the authorized_keys SSH file. You can print out the content of your key and send it to the authorized_keys file in one line:

cat my_key.pub >> /root/.ssh/authorized_keys

Tail

Tail help you visualize the last part of a file, humm? Lets say you run your web server, each seconds your log files gets new lines, how would you display it live in your console ? tail it !

tail logs/production.log

Grep

Sometime looking of a string inside a file or even a regexp and search through hundreds of files? grep is your man.

If you want to find a specific occurrence inside a file you could do:

grep “my query”

If you have many files, do it recursively:

grep -r “my query”

Do you want to only print the file where the match is found?

grep -rl “my query”

I personally find the “-A” and “-B” options very useful (read the man page)

Sed

Sed is a complex tool to read and edit (=stream) your file, the best use case are with the grep command. If you are looking for a string in multiple files chances are you want to change also it, then using sed you can do :

grep -rl “my query” * | xargs sed -i ‘’ ‘s/my query/my replacement/’

Find

Sometimes searching for a specific file in your directory?

find * -name “my query”

“Hacking” through the command line

Unix command can be combined and it’s what makes them so powerful. You already seen it previously with sed *or cat*. Below is the list of those utilities and a little explanation and example.

The pipe

Pipe is the way for you to pass as an input the result of one command to another one, for example:

ps -ax | grep “ruby”

The ps command will give you a result of all processes and then “piping” it to a grep will result of seeing only the ruby process

The redirect

Not not to be mistaken with the pipe, the > command let you send or redirect the result of a command to a file, for example:

ls -lh > myfile.txt

The redirect and append

As the previous command the » will send the result of a command to a file, but this time it’s not going to overwrite it but send the content at the end of it:

ls -ls >> myfile.txt

Basic usage of Vi(m) — Command line editor:

First time you use Vi you are lost. Below a very basic list of commands I usually need 6dc for very basic usage:

  • Entering the interactive mode (the edit mode) : Press Key i
  • Quitting the interactive mode : ESC
  • Search for a item in the file : ESC + / + “your query”
  • Going ot the last line : ESC + Shift + g
  • Deleting a line : ESC + d + d
  • Quitting and Saving : ESC + : + w + w + ENTER OR ESC + Z + Z

SSH Public key authentication

If you use SSH and use password to authenticate to your servers, STOP and read this: http://stackoverflow.com/questions/7260/how-do-i-setup-public-key-authentication

SCP Command

Scp is the easiest way to send a retrieve file, securely, from a distant server.

Uploading a file:

scp my_file user@host:/path/where/to/put/the/file*

Downloading a file:

scp user@host:/path/to/the/file local/path/of/the/file/to/copy

Of course you can send folders or multiple file, run the “-r” recursive option (as usual!)

“Your” Hosts file

Your computer holds a file called the Hosts file. As a developer it’s sometimes useful to trick your DNS for example to say the domain www.toto.com will resolve as your localhost instead of something else. If you want to do so, dig into this file:

sudo vi /etc/hosts

Managing Unix processes!

As a developer your code runs with other piece of softwares, for example a MySQL DB, a Redis K/V store, an NGinx Web server etc. All those softwares (or services) could crash (or your code makes them do!). Seeing what processes are up to and being able to kill them is more than useful.

Listing to all the processes running:

ps -aux

Listing most consuming processes, live:

top

If you want to kill a specific process:

kill -9 PID

Let me know if you think about other commands that a developer should absolutely know !

--

--