Curriculum/Digital Media/Command Line introduction

From Graphic Design Arnhem
Jump to navigation Jump to search

Command Line histories

The CLI is not so Goo-ey

The command line is a text-only interface to your computer, which gives you a more explicit access to what is happening on your machine. Rather than clicking around you issue a variety of small text based commands to interact with your computer.

These text based commands are very simple and won't really think for you like graphical programs do. For example, when those graphical programs tell you it is not possible to open that sound file in your photo editor they are thinking for you. Command line programs instead will allow you to just do that and assume you are doing the thinking.

While a bit awkward at first you will soon notice that working with and understanding the command line provides you with a lot of creative potential. Once you get used to these text-based utilities, it becomes an extremely fast way of working and navigating your computer. Additionally you will find that there is a large amount of useful utilities that exist for the command line, which you can not find in your GUI.

Perhaps the most important thing is that it will give you a window into understanding the workings of computers. Because many operating systems have a shared history as derivatives of the UNIX operating system, you will find that once you get an understanding of the command line on your own computer, you will easily manage to get around on other operating systems and computers in general[^windows].

Although the command line is an old technology, it is wrong to think of the command line as old-fashioned. It is rather a completely different methodology for working on your computer. We will find that the command line is an interface and programming language at the same time.

Command Line Interpreter (CLI) / Shell / Terminal


‘The command line user interface provides functions, not applications; methods, not solutions, or: nothing but a bunch of plug-ins to be promiscuously plugged into each other.’

$(echo echo) echo $(echo): Command Line Poetics, by Florian Cramer
http://reader.lgru.net/texts/echo-echo-echo-echo-command-line-poetics

‘It is not a shrink-wrapped, or - borrowing from Roland Barthes - a “readerly”, but a “writerly” interface. (…) Reflecting (…) the “plurality of entrances, the opening of networks, the infinity of languages”, the writerly text aims to “make the reader no longer a consumer, but a producer of the text”.’

$(echo echo) echo $(echo): Command Line Poetics, by Florian Cramer
http://reader.lgru.net/texts/echo-echo-echo-echo-command-line-poetics

Ghost in the Shell

Go ahead and start using the command line by opening a terminal application.

You’ll see a text interface with a blinking cursor.

What happened when you opened the terminal is that it actually opened a so-called shell for you. The shell (sh) is a software which takes your keyboard input and gives it to the computer’s operating system.

We will use the shell software bash.

Screenshot1.png

So what exactly are we looking at?

anatomy of the command line

After having opened the terminal you will be greeted by the following prompt:

mb@ts:~$

MacBook-Pro-van-Anna:~ anna$

This is the main interface and it tells you a few things:

  • your username @ your computer’s name (hostname). In the example these are user mb @ computer ts
  • The location where you are on your computer at the moment. This is denoted in this case by ~. This is a UNIX shorthand for the user’s home folder, in our example’s case /home/mb/
  • The last thing this prompt shows is the dollar sign ($), denoting that after that symbol your input follows.

$

In many examples and tutorials you will encounter the dollar sign in lines that are meant to be copied.

However don’t copy it, with the dollar we simply mean to say: after $ type the following. It is used to indicate we are talking about a CLI command. So when you encounter:

$ cd ~/

The actual command to type is

cd ~/

on shells

The interesting things about shells is that they are simultaneously interfaces and programming languages.

Text typed in the shell can represent commands, data or just text.

Try typing hello for example:

$ hello

bash: hello: command not found

All words or characters typed into the shell are interpreted as commands.

So if you want to print text, use echo.

$ echo 'hello!'

hello!

Echo is in fact a little programme that allows us to display text.

See what the difference is between:

$ date

and

$ echo date

Commands, flags and arguments

A typical CLI command might look like this:

$ ls -al ~/Downloads/

Let’s analyze the above. A command issued follows this pattern:

$ command -flags arguments

The command is the name of the program to use,
the flags are the ‘options’ for the command
and the argument is what you use the command on.

So in the case of the above, the command is:

$ ls

The flags are:

$ -al

The argument is:

$ ~/Downloads/

One of the confusing things initially about the command line is that it doesn’t give you a lot of feedback.

So if you input a command, seemingly nothing might happen.

This is usually a good thing (!).

If your command gives you some output, it is usually something to take note of as something might be going wrong.

Moving around

The command pwd to tell us where we are at the moment.

Used to find our present working directory.

$ pwd

Moving to another directory is done with the change directory command:

$ cd foldername

For example moving to your desktop:

$ cd ~/Desktop/

The UNIX command line uses a lot of shorthands.
We’ve already encountered \~ for homefolder.
But there are also \..\ which means 'one back'
and . which means 'here'.

Move to your homefolder:

$ cd ~

Move to the parent folder, one folder up:

$ cd ../

Move two folders up:

$ cd ../../

Copy a file from your downloads folder to the folder you are currently in:

$ cp ~/Downloads/omg.zip .

As you can see you can make copies of files with cp.

This means that after copying you have two files.

You can also move a file like this. You then only have one file:

$ mv ~/Downloads/omg.zip ~/

To know where you are you can also look around in a folder with the list command.
It will print out all the files in the folder for you:

$ ls

Another shorthand is *.
It stands for ‘any’ and can be used to filter specific things.
Lets move all files into a folder:

$ mv ~/Downloads/* ~/Files/

The * can also be used to match patterns. Lets for example move all .jpgs in to another folder:

$ mv ~/Downloads/*.jpg ~/Pictures/

*.jpg will match any filename with the extension .jpg

Let’s delete (remove) all the jpgs that start with IMG_ from the Downloads folder:

$ rm ~/Downloads/IMG_*.jpg

(!!!) Careful with that, rm will not tell warn you and it will not move things to trash, but really delete the files.

Pipelines

One of the really powerful things about the command line is that different little programs can be chained together.

This is done through 'output redirection' or 'piping'.

This allows the output of one program to become the input of the other.

These input and outputs are also called stdin and stdout (or standard in, standard out).

The reason all these programs work together is because they adhere to the UNIX design philosophy where programs …

do one thing and do them well

and

expect the output of every program to become the input to another

input | [STDIN — PROGRAM — STDOUT] | [STDIN — PROGRAM — STDOUT] | output

Piping and redirecting output

Look at the following command:

$ ls -al *.html | grep 'Sep' > files_from_september.txt

The following symbols are for output redirection:

Pipe from one program to the other: $ ls|grep

(Over)write output to file: $ grep 'Sep'>files_from_september.txt

Append ('add') output to file: $ grep 'Sep'>>files_from_september.txt

So when we know all of the above we can start to understand the meaning of:

$ ls -al *.html | grep 'Sep' > files_from_september.txt
  1. list all the html files
  2. filter out the ones made in 'September'
  3. and write that list to files_from_september.txt

Read the manual!

The command line has a built in documentation system which allows you to look up how to use a program.
For example we never explained what the command 'grep' does exactly.

However you can read its manual like so: $ man grep

By doing so we will get the following output:

NAME
grep, egrep, fgrep, rgrep - print lines matching a pattern

SYNOPSIS
grep [OPTIONS] PATTERN [FILE...]
grep [OPTIONS] [-e PATTERN]...  [-f FILE]...  [FILE...]

DESCRIPTION
grep  searches  the  named input FILEs for lines containing a match to
the given PATTERN.  If no files are specified, or if the file  “-”  is
given,  grep  searches  standard  input.   By default, grep prints the
matching lines.

In addition, the variant programs egrep, fgrep and rgrep are the  same
as  grep -E,  grep -F,  and grep -r, respectively.  These variants are
deprecated, but are provided for backward compatibility.

Here we learn that grep is a pattern matching program. In the Synopsis we can see the basic use.
Use your up and down keys to navigate the manual and use q to exit.

Reading and writing

Files can be read (also called printed) on the terminal with cat.
This stands for concatenate, which is a fancy way of saying,
read the file line byline and add the outputs to each other.

$ cat myfile.txt

Sometimes your terminal gets full of output that it becomes difficult to read.

Clear it like so:

$ clear

Or use CTRL + L.

Some files or outputs are really long and you might want to be able to scroll back and forth through them to read:

$ cat longassfile.txt | less

Like the manual, use the arrow keys to navigate and q to quit.

The terminal is not only for reading you can also write files:

$ nano myfile.txt

This will give you an interactive interface where you can start typing in myfile.txt.
When you are done press ctr+x to exit the interactive editor
and choose if you want to safe the file with: y or n.

We can also edit the output of one command with sed, the stream editor,
and write the output to a file using the >.

$ echo 'Hey you!' | sed 's/you/everyone/g' > hey.txt


*B*O*N*U*S*

If you want to dig deeper, you can try the following two things:

cheat sheet

!!!!! TIP !!!!!!

To go over many different tools, you can use the Command Line Cheat Sheet.

further reading

$(echo echo) echo $(echo) on 'writerly' interfaces by Florian Cramer

Learning The Shell