Wednesday, February 25, 2009

Working with a text console

I thought I would post some notes on working with a text console or "terminal interface" using the Linux operating system. This has particular value when you are maintaining a web site on a local server.

Note!! In some places in this document there are words with capitals in the middle such as userName, fileName and directoryName. These words are being used to represent a file or directory that you will be working with. To execute the command being explained at this point in the document you must substitute the actual user, file or directory name for the one with the capital in the middle.

Web page data needs to be kept in a manner that stops people who should not have access to change the content. We can do this by setting the files and directories to be read only for these unauthorized people.

The easiest way to do this is by using a terminal interface and using the text based commands that allow us to see and adjust the read/write permissions.

When you are working with a Graphical User Interface [GUI] such as gnome or KDE, the terminal interface is often identified by an icon that looks just like a computer monitor. Activate this icon and you will be ready to begin. It is possible to have several of these terminals running at the same time and often it is beneficial to work this way. If you want to look at something else while in the middle of working with a command, just open another terminal. Switching between them just requires a left mouse button click on the top coloured line of the terminal area you wish to work with.

Very important note !!! You must ALWAYS use lower case when you are working with the following text commands. Linux is case sensitive. The only time you can use capitals is when you have a capital letter in the file or directory name.

You can use the text program called midnight commander to move and copy files. This program can also be used to change the settings on files and directories, however, you can't see the settings as easily as with the ls command. We will look more at midnight commander later in this document.

First we will look at the ls command. ls is the listing command and the name comes from the first and third letter of that word. ls has many options that can be applied. We will look at the -la options at this time. If you enter the command ls -la and press the enter key you will see the files and sub directories available in the current directory. if you just enter ls you will still see some information but you will not see any of the read/write settings.

Below is an example of using the ls -la command when looking at the desktop of my eeePC. You can see the file you are reading now is on my desktop at the time of this view.

eeepc-eastwind:/home/eastwind/Desktop> ls -la
total 24
drwx------ 2 eastwind eastwind 4096 2009-01-24 13:25 ./
drwxr-xr-x 39 eastwind eastwind 4096 2009-01-24 13:08 ../
-rw-r--r-- 1 eastwind eastwind 69 2009-01-24 13:08 .directory
-rw-r--r-- 1 eastwind eastwind 4398 2009-01-24 13:08 Home.desktop
-rw-r--r-- 1 eastwind eastwind 947 2009-01-24 13:34 Webfiles.txt

The first two lines (repeated below) show the directory structure of this part of the system. The single dot ./ means the current directory and the two dots ../ mean the parent directory. to the left of these dots is the time and date when the directory was created or last altered. Next to the left are the group and owner entries. Many Linux systems have a group called users for all user files.

The system I am using to develop this part of the help file calls the group the same as the user.

The x means that the directory is executable or in other words, accessible. The r means it is readable and the w means writable. The d tells us that it is a directory and not just a single file. There are three groups of the rwx settings. The set on the right represent the world, the centre set represent the group the user belongs to. The set on the left represent the user who owns the directory. In the example you can see here, only the owner can do anything with the current directory. The parent directory is accessible and readable by everyone but still only writable by the owner.

drwx------ 2 eastwind eastwind 4096 2009-01-24 13:25 ./
drwxr-xr-x 39 eastwind eastwind 4096 2009-01-24 13:08 ../

The next line we will look at is the listing for this document. You can see that there is no d on the beginning of this line. This is because this is not a directory but is a single file. This file is read/write by me but only readable by every one else. This is the setting we should have for every file that is used on a web site.

-rw-r--r-- 1 eastwind eastwind 947 2009-01-24 13:34 Webfiles.txt

If we want to change the settings of an entry we use the chmod command. We use numbers to represent the rwx set. the number 7 means rwx. The number 6 means rw. The number 5 means rx. The number 4 means r only. The number 3 would be wx. The number 2 would be w only. The number 1 would be x only. The number 0 means we do not have any access at all. However, unless you can read the file you really can't do anything so the settings that do not include the r are never used in a web site.

To set a directory we need to make sure we put the x in the settings. The command chmod 755 directoryName is the one we should use to set a directory for a web site.
To set a single file we use chmod 644 fileName or we can use the extension of a group with a wild card for the file names. An example of this would be chmod 644 *.jpg to change all the photos in a directory that have the .jpg extension.

When we are working with long filenames we can use the tab key to complete the file name once we have put the first letter on the screen. If there is more than one file with the first letter we enter the first part up to the point where there is a difference in the file name then press the tab key. Linux will offer the files that have the letters we have entered each time the tab key is pressed. When the file name is complete we can just press the enter key.

To move into a directory we use the cd command. The command is in the form of cd directoryName and would be given while in the parent directory where an ls command showed directoryName as one of its sub directories. To move back from this directory we enter the command cd .. where the two dots mean parent directory as in the ls command. We can use the tab key to complete the directory name in the same way as explained above.

The su command lets us work as the Super User or owner of the entire computer file system. It is not wise to do much work as the Super User because it is possible to do damage to the file system, however, in certain circumstances we have no other choice. We should close the terminal where we used the su command as soon as that specific task is completed. All the commands explained in this document can be carried out when you are logged in as the Super User. The next topic, ownership changes, can only be done by the Super User.

We need to change the ownership and group of a file sometimes when we have moved or copied it from a directory that is owned by another user. This is particularly applicable when we have used the su command to allow us to access an area owned by another user.

The two commands that are needed for this ownership change are the chown and chgrp commands. To change the ownership we enter the command chown userName file/directoryName while in the directory where we can see the file or directory with the ls command. To change the group we just substitute the chgrp command for chown and enter the file/directory name in the same way. If the group is not the same as the userName, such as in the case where the users are all part of the one group called "user" then we need to put that name in the chgrp command line in place of the userName. We can also use the wild card * just as in the chmod example above. This would then be chown userName *.jpg to change all the .jpg pictures to be owned by a the user who owns the directory.

The mc command is very powerful. Midnight Commander (mc) comes up in the terminal with two columns showing files available. There are also two menus, one on the top of the screen and one on the bottom. We can move from left to right of the screen by using the tab key. We move up and down the listing with the cursor arrows. You can also use the mouse to move around and select the menus. Moving to the /.. entry and pressing enter or clicking the left mouse button will move you to the parent directory.

The numbers on the bottom menu also represent the function keys of the same number. For example if we press the f9 function key the top menu (pull down) will be activated. Pressing the f10 function key closes mc.

To move or copy files using mc we first need to have the source and target directories showing on the screen. It does not matter which side we use for target or source. For this explanation I will say that the left hand list is the source and the right hand is the target.

Press the tab key so that you have your highlighted bar on the right of the screen. We will create a new directory here called temp. The f7 function key (7 on the bottom menu) is the directory create command. Press f7 and enter the word temp in the data entry box. Press the tab key once and move to the ok box then press enter. You now have a new directory called temp. Your highlighted bar is on this directory. Press the enter key once. Now you are in the temp directory.

Press the tab key so that you have your highlighted bar on the left of the screen. Use your cursor keys to move down to one of the files. The files do not have a / in front of them. The names that have the preceding / are directories. When you have chosen a file press the f5 function key (5 on the bottom menu).

You will now have a box with a number of options. You move around the box using the tab key. When you are on an option, pressing the space bar will toggle the selection on or off. By default the top line will show a * and represents the file you were on when you pressed f5. We leave the shell patterns X on. By default the target will be the other side of the screen to where you pressed f5. The only thing we sometimes do is move to the end of this box and alter the file name if we there is already a file with that exact name in the target directory. The target file name is the last box we move to using the tab key.

We normally leave follow links unselected. Dive into subdir is necessary if we are actually copying a whole directory. If we don't select this when copying directories we may find that some files do not get copied. If you are copying files from a CD or DVD we should de-select the preserve attributes option. If we just leave the X in place for this option we will have read only files in such a situation. Also if we are copying files from a memory stick, we will find that every one has read write access to the data and we will need to adjust the files to be 644 with chmod. We should always check everything with ls -la after we have copied files or directories so if you do not adjust this option it is not going to cause you too many problems. It just saves time if we copy with the correct attributes in the first place. Stable symlinks is normally left unselected.

The final step is to move to the ok button and press the enter key. Now you have a copy of your chosen file in your temp directory. Take care not to use the f6 key unless you want to move the file away from the source directory. F6 does not copy. It moves the file. Mostly we do not move files unless they are a problem in the source directory.

Having multiple copies of a file is usually a good thing in case we accidentally delete or corrupt a file that we have been working with. Todays computers do not always write files to the hard drive immediately. To save time there is a cache system that only writes the file to hard drive when the memory is full or you issue a complete shut down of the computer. If there is a power failure when you are changing a file, the data could be lost. This is why we should always use the shut down options to turn off a computer and not just turn off the main power switch.