2006-08-20

Revision 2 as of 2008-08-06 16:21:30

Clear message

first off lets open a terminal in gnome click on application -> accessories -> terminal (kmenu -> system -> konsole for kde)

To switch between the terminal and your IRC client using only the keyboard you can use "alt and tab" to cycle through the active windows.

I want to introduce the concept of a shell
you have in front of you in the default black writing on a white background on the left there is something that looks like this

ompaul@desktop:~$

this is your username at your machine and it currently indicates you are in your home directory
if you type at that  command set | grep SHELL  you will see that the first line returned is  SHELL=/bin/bash  that indicates what "shell" your "environment" is set to use
this is the program that you use to converse with the kernel
this program provides a programming environment (usually in the form of scripts) to allow a user to interact with the machine and manipulate it
there are two ways it does this:
it invokes command within itself
or it can invoke commands external to itself

the ones within are referred to as "buildins"
so the first command I want you to in there now is pwd
this command tells you where you are
pwd is hard to remember but it actually stands for print working directory
now you may see from time to time things such as  ls -al  the part after the ls is called an argument
pwd accepts two arguments
the first is --help - which displays help for the program and
the other is --version - which displays the version number
after both of these functions have completed (and providing they complete correctly) the program exists
please run the following

pwd --help

[PWD has a bug --help doesn't work, we move on to another command]

to demonstrate an argument let us use the command which invokes manuals
this is the "man command"
please type  man --help in your terminals now

you will notice that a lot of data is on the screen and the scroll bar on the right should indicate that some data has passed by however we do not always have the luxury of a scroll bar so now we will have a quick look at how to see more data

so to see the data one screen at a time you can do this:

man --help | more

the function of that pipe (|) is to take the output from man --help and then present it to the command more
what more does is it allows output to be viewed one screen at at time
if you press the down arrow the screen will scroll down one line at a time and if you use the space bar it is one page at a time
however more faces a restriction a big restriction - you can only move in one direction - it only allows you to go down

we can do "more than more" by using a new command less
the benefit of less is that we can go up and down
we can use up and down arrows and page up and page down
this is useful of you have lots of data

so if you do  man --help | less  you can test this you can navigate up and down and then to quit you can press q

now lets have some fun

please type  man man 
on the foot of the screen you will see something like this  Manual page man(1) line 527/551 (END) 
this indicates what manual page you are looking at before the bracket(1) in this case man, and it also lets you know how big the manual page is the last number, in this case 551
although the manual pages are written in a very "tight" style, if you get used to them you will find them good friends to discover options when you try to do something more complex
now please press q and exit that man page

firstly let me tell you something of the layout of linux as it will help you understand some of what is there in front of you
when I say layout just now referring to the filesystem
you are currently in what is called your home directory
the prompt is as I showed eariler

ompaul@desktop:~$

the ~ after the : is short hand for saying /home/your_username/
the word directory in english actually means "list" however to be clear about what a directory is I like to think of the hard drive as a filing cabinet
there are many drawers in the filing cabinet each of the drawers can contain more drawers
to see the list of "primary drawers" we can use the command ls which is short for list
the whole cabinet is called / which we call "slash" not forward slash but slash
this \ is "back slash"
there is no forward slash Smile :)

that must be something else you are thinking about Smile :)

so let us get a list of the things in /

ls /
  • Q: is there a meaning for the colors of the dirs and files after ls ?
    A: there is, but lets not get ahead of ourselves

so you see bin
it is the first directory there
so now please do

ls /bin

the colour you see for bin indicates it is a directory
you are now looking at the contents of /bin
you can see some very common every day commands there
ls is there
date
ps (which shows what processes are running on the computer when given certain arguments)
tar touch sed and a lot of others
the colour you see for arch and bash and most of the others in there is for an executable file
depending on how your console is set up but in general, green

ls -al /bin/bash

-rwxr-xr-x 1 root root 664084 2006-04-21 23:51 /bin/bash

that is what it replies or something like that

you will notice that the there are several letters on the left hand side of the screen and some dashes
they are the file permissions
r = read from the file | w = write to the file | x = execute (run) the file
you may notice that I say file
and also you may remember I referred to "drawers containing drawers"
well directories have file permissions too
in fact directories are nothing more than fancy files that contain more files
and they too have permissions
if you run

ls -l / | grep bin

you will get back something like:

drwxr-xr-x   2 root   root    4096 2006-07-12 17:50 bin
drwxr-xr-x   2 root   root    8192 2006-08-18 23:37 sbin

now on the extreme left of the permissions you will see the letter d
this indicates that these files are directories

so now you have two concepts: files and directories
you have also a concept of permissions, however not in detail

you will notice if I take the /bin/bash permissions that it starts with a -
then it has the first group of three rwx
then a group with r-x
and another group with r-x

well reading left to right we call these groups
Owner Group World (or I have heard it called Everybody)

so what does this mean?
well this gets more meaning from the two columns after the number 2
root root
this is the owner of the file and the group that the file is owned by
root owns the file and also there is a group called root
they are not the same Smile :)
in some cases
like where your username is the group
I will break it out into little blocks

so in your own home directory there are files

  • Q: the file is owned by a user called "root" and the file is also owned by a group called "root" ?
    A: yes
    Q: by group root.. u mean the username?
    A: no, explanation is to come

please type ls -al /etc/group

first off note that it is not executable
second note that while any one in the group root anyone (everybody/world) can read it
so it is readable by all users on the computer
now let us look inside that file

less /etc/group

first line
root:x:0:
so this is a group
the group has a numerical id it is 0 (as seen in the output) then as you look down the file you will see your username it will be a member of some of the groups in the machine.
On ubuntu group 106 admin, for instance, is occupied by users who have "administration powers"
if you press the space bar you will get to the end of the file

you will see the first user created on the machine as a group name = the username and with a GID (group id number) of 1000

in a business you might have a group sales
and they would all be able to access the files belonging to the group "sales"
now the reason we went down this road (and whole chapters in books have been written about permissions)
was to explain that a file has an owner (as in a user) and a group owner and these things can be used to grant access to the users on the box or deny them access to a file
this is important because if you can't run a file you may be trying to access something that the system will not allow

so now in your home directories
all that information in a way that people don't usually "see" is
please type

stat Desktop

ompaul@desktop:~$ stat Desktop/
  File: `Desktop/'
  Size: 4096            Blocks: 8          IO Block: 4096   directory
Device: 303h/771d       Inode: 4653091     Links: 7
Access: (0755/drwxr-xr-x)  Uid: ( 1000/  ompaul)   Gid: ( 1000/  ompaul)
Access: 2006-08-20 12:20:29.000000000 +0100
Modify: 2006-08-20 08:05:46.000000000 +0100
Change: 2006-08-20 08:05:46.000000000 +0100

okay you will see the file name at the top
I said eariler that directories were files
you will see Desktop/ being called a file
now for the most mind blowing thing you will ever learn about linux and unix based systems (at least in my opinion)
EVERYTHING IS A FILE
and I mean that. your screen, your keyboard, your mouse, they can be read, written to. your printer, your usb key, the whole system is a collection of files. You take data and you move it from one place to another this all goes back to an early design choice and it was very fortunate for us they choose that what it means is that you can take the output of a file and redirect it to another you an then add to it subtract and a whole lot of other things
I will demonstrate that next

first off let us create a file in our home directories.
the way to create an empty file is to use the touch command
it is normal to refer to random files as "foo"

please type touch foo123 in your terminal
now stat it
the inode data there is its actual address on the file system
which contains the info about where the file lives on the system
as you note the file is owned by you
it is readable and writable by you
it is readable by the group and anyone on the machine
you remember eariler we used man --help
well let us capture that data in the file foo123
so do this

man --help > foo123

we can now look at the file with stat
or we can do it the way most people do it

ls -l foo123

right now it looks like -rw-r--r-- 1 ompaul ompaul 2390 2006-08-20 12:45 foo123
the 2390 is the size of the file
lets overwrite
but first have a quick look

less foo123

press the space bar until you get to the end
then press q to get out of less

so do this

wc foo123

now what do you think your looking at?
lets find out what your looking at

man wc

what does that tell you
wc - print the number of newlines, words, and bytes in files

so it tells you the lines that are in the file
please press q to leave the man page
so we have seen the original stat data of the file
lets look at that

stat foo123 >> foo123

that should be fun
after that type wc foo123

okay so when we type wc foo123 we note that the line has a size
then if we do the next part: stat foo123 >> foo123
we add the stat info onto the end of the file
so the point here is that the > single redirection pipe
takes some "random input" and puts it into the file "in this case foo123"
when we use the >> this APPENDS the additional data to the file
in this case we got it to add on the contents for foo123 to the tail end of the file
you can see this if you now type less foo123

now that is three pipes covered
"A | B" take the data from the left (A) and hand to the the program on the right (B) to be processed
we have also taken random data and put it into a file
usually that is output we want to capture to analyse for later
that is done with the >
and then we can open an already full file and append data to it
this is the >>
NOTE the > overwrites all previous data

ls
ls -al
|
>
>>
touch
man
and the concept of an argument

now to find your way around the file system
you are in your home directory
you can move from there to your desktop
so in the terminal please type

cd Desktop

you have now moved to the desktop
if you touch foo1234 here you will create an empty file on your desktop
please do so and check it exists by minimising your irc window

to explain some more about where you are please consider the file system to be like this

 /                  <---- also referred to as root
 /home/                  <---- this is where all the users home directories live
      /ompaul/                <-- as it is in my case - this will be your username :)
             /Desktop/              <-- note the capital D

the full path is
/home/ompaul/Desktop/
now we also know about /bin/
there are hundreds of directories on the machine
and thousands of files (if not millions)
now

cd /

takes you to the root of the file system
you do not want to be creating things there
in fact you do not have rights to create data in that directory
the place you should create data is in you own directory or some subdirectory below that
now if you are in the Desktop directory you can create a new directory in that
mkdir myfirstdirectory
you can then enter that

cd myfirstdirectory

so now you are within that location
lets do something
lets use the df command which tells us information about the file system layout
but lets make it human friendly output
do the following

df -h

now if you want to compare how much disk space is used over time
we can record the disk space used today
so do this

df -h > dfinfoAug20-2006

so this gives us as humans a file we can relate to
now if you go to your desktop and click on myfirstdirectory
you can find the file we just created in it

okay lets make a copy of that file into our home directory in the terminal
type this much only

cp df

now hit tab key
see it complete the file name
now you want to get it into your home directory

cp dfinfoAug20-2006 /home/ompaul/.

note the dot at the end

I could change its name with this

cp dfinfoAug20-2006 /home/ompaul/MyNewName

or even do this

cp dfinfoAug20-2006 ~/.

which is the same as the first one

the dot means "keep the same file name"
but .is is a hidden file called .is Smile :)

ls -al

the a means hidden and the l is long format (give lots of detail)

(at this point all the Students left, and the class gets cut short)

For more information see: https://help.ubuntu.com/community/BasicCommands