Simon The Pi Man bookstore
Mmmm Raspberry Pi
Simon the Pi Man
To Infinity (probably) and beyond
(A beginners Resource for the Raspberry Pi computer using the Debian distro)
since June 2012
Keep up to date with our RSS feed

If you are looking for info on the Raspberry Pi computer stay on this site
however
If you are looking for a good cafe in Rye in East Sussex try "Simon the Pie man" near the church.

Beginners Guide to Unix - part 3 -  Looking at Files, their contents and the terminal window

On this page I will give you guidelines as to how you can look at the contents of files and commands used for files.

1. From the previous beginners guide have seen that the /etc folder contains system configuration files, so lets start by looking in there to see what is there - so let us run the following command
pi@raspberrypi:~$  cd /etc[Return or Enter]
This will c(hange)d(irectory) into to the /etc folder and you will see that the prompt has changed from

------------------------------------------------------
pi@raspberrypi:~$
------------------------------------------------------

this is the prompt you will see when you log into your Pi, and the [~] means that you are in your home directory
to

------------------------------------------------------
pi@raspberrypi:/etc$
------------------------------------------------------

You will see that the [~] has turned into [/etc], this shows that the you have changed your current directory into /etc.

2. Now we are in the /etc folder lets take a look at whats here.
pi@raspberrypi:/etc$ ls -l|more[Return or Enter]
As we page through the list by pressing the space bar we get to a few interesting folders starting in cron these are folders used for the system timer known as cron taken from Chronos the greek god of time, we will come to cron in another guide.

Lets stop at at a file called hosts so to stop paging through [---More---] press quit or press ctrl-c to take us back to the prompt.

3. So let us look at the contents of the file without damaging it, so we will use a command called cat that concatenates files and displays them on the screen. NOTE:- for big files you can add a |more to page through the file.
pi@raspberrypi:/etc$ cat hosts[Return or Enter]
This has displayed the hosts file as below.

------------------------------------------------------
::1             raspberrypi localhost6.localdomain6 localhost6
127.0.1.1       raspberrypi

127.0.0.1       localhost
::1             localhost ip6-localhost ip6-loopback
fe00::0         ip6-localnet
ff00::0         ip6-mcastprefix
ff02::1         ip6-allnodes
ff02::2         ip6-allrouters
------------------------------------------------------

Now just to confuse the issue try
pi@raspberrypi:/etc$ more hosts[Return or Enter]
This gives the same display as the cat command, and for even more confusion try
pi@raspberrypi:~$  less hosts[Return or Enter]
This also gives a similar display as the cat command, but you are now shown a hosts (END) prompt that you need to press q to allow you to quit the display.

A few quick comments on the more and less commands - a [space] will page you through the data being shown a [return] will move one line at a time and a quits the paging.

So as you can see there are at least 3 different ways of showing file contents, this is pretty common in the unix world where multiple different commands do similar things - just remember this whenever reading unix scripts.

4. Now lets look at the file again, so in the terminal press the [up] and [down] arrow keys, you will now see that all the commands you have used are stored in the terminals history of what you have done - the [up] key takes you back though your history, the [down] key takes forward.
NOTE:- this history is remembered even after you have logged out.

5. Sticking with the history theme just press the [down] arrow until the prompt is back to pi@raspberrypi:/etc$  then type the following
pi@raspberrypi:/etc$ history[Return or Enter]
You will now see a list of all the commands you have typed in, and at the beginning a number.
Please note the example below is just showin the last few entries in my history

------------------------------------------------------
107 cd /etc
108 ls -l|more
109 cat hosts
110 more hosts
111 less hosts
112 history
------------------------------------------------------

If the history list is longer than your screen just press the [up] arrow which will take you back to the history entry, and add |more and press [Return] at the end so you can page through the history.

6. Another thing regarding history - if you wish to run one of the history items without scrolling up and down the entries you can run the one you want by typing
pi@raspberrypi:/etc$ !{number}[Return or Enter]
Where the {number} is one of the numbers against the history records, so in my example if I wanted to run the cat hosts command again I would type in !109

7. One last thing about the terminal window type
pi@raspberrypi:/etc$ ls -l di[tab]
Where [tab] is the tab key the result is

------------------------------------------------------
pi@raspberrypi:/etc$ ls -l dictionaries-common/
total 0
lrwxrwxrwx 1 root root 20 Aug 25  2011 words -> /usr/share/dict/web2
pi@raspberrypi:/etc$
------------------------------------------------------

You will see that the prompt auto completed the name dictionaries-common without you - so you dont need to completely type the file or directory name. One more example of this is
pi@raspberrypi:/etc$ cd /u[tab]/g[tab][Return or Enter]
Which will take you into the /usr/games folder

------------------------------------------------------
pi@raspberrypi:/etc$ cd /usr/games/
pi@raspberrypi:/usr/games$
------------------------------------------------------


8. Finally lets return to look at the /etc/hosts file
pi@raspberrypi:/etc$ cat /etc/hosts[Return or Enter]
In this example we have put the full path for the file, we didnt change directory first. but we get the same result.

------------------------------------------------------
::1             raspberrypi localhost6.localdomain6 localhost6
127.0.1.1       raspberrypi

127.0.0.1       localhost
::1             localhost ip6-localhost ip6-loopback
fe00::0         ip6-localnet
ff00::0         ip6-mcastprefix
ff02::1         ip6-allnodes
ff02::2         ip6-allrouters
------------------------------------------------------

When a computer tries to contact a website it firstly looks up the hosts file to see if the domain is in it, then it will contact its DNS(Domain name server) to resolve the name into an IP address.
If you look in the file you will only find raspberrypi in 2 entries - a 127.0.0.1 which is an IP Version 4(soon to be superceeded as the maximum number of servers in the world 255^4 will soon be exceeded) and a ::1 which is an IP Version 6 type(new style IP address).
In this file you can add new shortcut entries to servers if you wish.

I will explain how to edit files using vi in the next Beginners Guide.

Useful commands that you may have missed above
  cd change directory
  ls for listing files
  ls -l for detailed listing of files
  cat to display files
  more to page through lists/files
  less to page through lists/files
  history to see what you have done
  [up arrow] to go back through your history
  [down arrow] to go forward through your history
  [tab] to automatically expand a file or directory name
  !{number} to run a historic command number {number}

simonthepiman.com 2012->2017
email:
simon@simonthepiman.com

hosted by VillageNetThe Raspberry Pi Foundation is a UK registered charity which exists to promote the study of computer science and related topics, especially at school level, and to put the fun back into learning computing.

Please Note:- any mention of the Raspberry Pi computer on these pages refers to the Raspberry Pi Foundation's product, who also have trademark rights to the term 'Raspberry Pi'.