Difference between revisions of "Linux1"

From SourceWiki
Jump to navigation Jump to search
Line 238: Line 238:
  
 
Note that on each line of the <tt>ls -l</tt> output, the first character marks the type of the entry, it is a dash "-" for a regular file, a "d" for a directory and it could be a "l" for a link. We will introduce links later.
 
Note that on each line of the <tt>ls -l</tt> output, the first character marks the type of the entry, it is a dash "-" for a regular file, a "d" for a directory and it could be a "l" for a link. We will introduce links later.
 +
 +
The permissions on a file or directory can be changes with the command <tt>chmod</tt>.
 +
 +
<pre>
 +
$ chmod -r file1
 +
$ ls -ltotal 48
 +
--w------- 1 jp jp  73 2008-02-04 12:46 file1
 +
-rw-r--r-- 1 jp jp  284 2008-02-04 12:50 file2
 +
drwxr-xr-x 2 jp jp 4096 2008-02-04 23:40 folder1
 +
drwxr-xr-x 2 jp jp 4096 2008-02-04 23:46 folder2
 +
drwxr-xr-x 3 jp jp 4096 2008-02-05 01:36 folder_a
 +
drwxr-xr-x 2 jp jp 4096 2008-02-05 01:34 new-folder
 +
</pre>
 +
 +
To add read and execute access to the group but not the owner or the others, use
 +
<pre>
 +
$ chmod g+rx file1
 +
$ ls -l
 +
--w-r-x--- 1 jp jp  73 2008-02-04 12:46 file1
 +
-rw-r--r-- 1 jp jp  284 2008-02-04 12:50 file2
 +
drwxr-xr-x 2 jp jp 4096 2008-02-04 23:40 folder1
 +
drwxr-xr-x 2 jp jp 4096 2008-02-04 23:46 folder2
 +
drwxr-xr-x 3 jp jp 4096 2008-02-05 01:36 folder_a
 +
drwxr-xr-x 2 jp jp 4096 2008-02-05 01:34 new-folder
 +
</pre>
 +
 +
Similarly, use the letter u for the user/owner, g for the group and o for the others. be careful, o is for the others, '''not''' the owner. <tt>chmod</tt. can also be used with a 3 digit permission summary too.
 +
 +
To change the owner or the group of a file, use the command <tt>chown</tt>. The syntax is:
 +
<pre>
 +
chown user.group file
 +
</pre>
 +
where "user" and "group" are the new owner and/or the new group for the file.
  
 
=Creating/deleting/modifying=
 
=Creating/deleting/modifying=

Revision as of 01:55, 5 February 2008


Introduction

Linux (sometimes called GNU/Linux) is an operating system, i.e. the middle man between you and the computer hardware. Therefore Linux is a full replacement for Microsoft Windows or Mac OS X. Linux comes with a range of graphical user interfaces where you can use the mouse to interact with the computer as you are used to. However, in most setup within the University (and particularly for the HPC facilities), you will not have access to this graphical user interface. Instead, you will be using Linux at the command line. Therefore this practical introduces some of the basic commands that you must know in order to carry out your research in a Linux based environment.

The user interface on a computer is technically called a shell. When using Linux at the command line, most often the bash shell is used. There are a range of shells (csh, zsh ...) for Linux with slightly different syntaxes. The bash shell is often the default and we will use bash syntax in this practical.

Logging in

Login is performed via a bit of protocol called secure shell or ssh. There is a client for Microsoft Windows which should already be installed on your computer. If not, contact a computer officer. To login select the machine that you want to log into, for instance dylan.ggy.bris.ac.uk or bluecrystal.bris.ac.uk and then enter your username and password. the system will log you in and you will directly be taken to your home directory.

Although you used your password to login, it is important to note that there are other login options available. For instance, you can configure ssh to log you in automatically using authentication keys instead of using a password. This is very interesting when you want scripts to run automatically and perform operations on the network. This is really beyond the scope of this practical.

Getting the content for this practical

Now that you are logged in a Linux system, it is time to get the practical content. The necessary files for this practical are hosted in a version control system. To obtain them, just type the following command:

$ svn export http://source.ggy.bris.ac.uk/subversion-open/intro-to-linux/trunk intro-to-linux

The dollar sign "$" is called the hell prompt. You do not need to type it in. It is merely there to show that the shell is waiting for a command. When a line starts without a dollar, it means that it is an output from the shell. This is a very common convention that will be used throughout this practical.

This will fetch all necessary files and put them in a folder called intro-to-linux/. Ignore the cryptic syntax so far, an introduction to version control using subversion (svn) will be given later on.

Navigation

To change directories (sometimes called folders), the command cd is used. It stands for ... "change directory" (!) To navigate to the directory containing the files for this practical, simply type:

$ cd intro-to-linux

By doing this operation, you technically used a relative path. Indeed intro-to-linux is not the full directory location but merely its relative path from where you were before. To find out the absolute path of your current location, use the command pwd (print working directory):

$ pwd
/gsa13/ggjpr/intro-to-linux

Obviously, the full path will depend on where your home directory is located. The absolute path starts at / called the root and then lists the branches of the directory tree leading to the current directory. Note that the directories are separated by slashes "/" and not backslashes as used by DOS in Microsoft Windows.

The cd command also accepts absolute paths. For instance:

$ cd /gsa3/ggjpr
$ pwd
/gsa3/ggjpr
$ cd /gsa3/ggjpr/intro-to-linux
$ pwd
/gsa13/ggjpr/intro-to-linux

To help navigation, every directory on a Linux system contains two shortcuts:

  • . (yes, one single dot!) links to itself
  • .. (two consecutive dots) links to its parent

The .. shortcut especially is important as it allows you to go up one directory without using a full path:

$ pwd
/gsa13/ggjpr/intro-to-linux
$ cd ..
$ pwd
/gsa3/ggjpr
$ cd ..
$ pwd
/gsa3

It is important to know that if you don't give an argument to cd, it will take you back to your home directory.

$ pwd
/gsa3
$ cd
$ pwd
/gsa3/ggjpr

Furthermore, you can use cd with the argument "-" to simply go back where you were before, a it like the back button on a web browser. It only worksone level though so using it twice sends you back to where you started. For instance:

$ pwd
/gsa13/ggjpr/intro-to-linux
$ cd
$ pwd
/gsa13/ggjpr
$ cd -
$ pwd
/gsa13/ggjpr/intro-to-linux
$ cd -
$ pwd
/gsa13/ggjpr

Listing a directory content

The content of the directory can be listed with the command ls (for list). For example, let's go back in the intro-to-linux directory and list its content:

$ cd intro-to-linux
$ ls
file1  file2  folder1  folder2

So our intro-to-linux directory contains two files and two directories. Depending on the secure shell software and the Linux machine, you could also have a colourised output to differentiate easily between files and directories. By default, an alphabetical order is used when listing directory content but sometimes, the directories are listed together before the files.

The ls command also accepts some options to modify its behaviour. The options are listed after a dash "-". For instance, the "-r" option reverse the listing order:

$ ls -r 
folder2 folder1 file2  file1

Files and folders can be hidden on Linux. To show hidden files, use ls with the "-a" option (for all):

$ ls -a
.  ..  file1  file2  folder1  folder2  .hiddenfile  .hiddenfolder

You can see that:

  • the dot and dot dot shortcuts are actualy hidden folders
  • hidden files and folders also start with a dot (".")

If you go in your home directory, there should be a few hidden files and folders already. They are usually used to store configuration information. Here is a typical home directory on a (heavily used) Linux system:

$ cd
$ ls -a
.              .gconfd            .mcoprc                      Templates
..             .gimp-2.4          .metacity                    .themes
.adobe         .gnome             .mozilla                     .thumbnails
.bash_history  .gnome2            Music                        .thunderbird
.bash_logout   .gnome2_private    .nautilus                    .tomboy
.bash_profile  .gstreamer-0.10    .openoffice.org2.0           .tomboy.log
.bashrc        .gtk-bookmarks     Public                       .Trash
bin            .gtkrc-1.2-gnome2  .pulse                       .unison
.config        hydrostab          .pulse-cookie                unison.log
Desktop        .ICEauthority      .qt                          Videos
.dmrc          .icons             .recently-used.xbel          .viminfo
Documents      intro-to-linux     .Skype                       .vimrc
Download       .kde               skype-2.0.0.13-fc5.i586.rpm  .wapi
.esd_auth      .libgda            .ssh                         .xine
.evolution     linux.tgz          .subversion                  .xsession-errors
.fontconfig    .local             systel90
fortran1       .macromedia        tecplot360-2008
.gconf         .mcop               

Obviously you can ignore these files most of the time. This is why they are hidden.

When you want more detail about the files and directories, use the "-l" option to use the long listing format. Let's go ack n the intro-to-linux directory and try it:

$ cd intro-to-linux
$ ls -l
total 32
-rw-r--r-- 1 jp jp   73 2008-02-01 10:43 file1
-rw-r--r-- 1 jp jp  284 2008-02-01 10:43 file2
drwxr-xr-x 2 jp jp 4096 2008-02-01 10:43 folder1
drwxr-xr-x 2 jp jp 4096 2008-02-01 21:30 folder2

The detailed output for each item starts with a big block which contains the file or directory permissions, the number of directories inside the directory, the file/directory owner, the file/directory group, the file size, the last modified date and time and eventually the file/directory name. The output might slightly vary between systems. Permissions, ownership and groups are explained later on. Note that the number of directories is one for files and at least 2 for directories (because dot and dot dot are always present).

The file size is given in bytes. To make it easier to read, it is possible to use the -h option (for human readable):

$ ls -l -h
total 32
-rw-r--r-- 1 jp jp   73 2008-02-04 12:46 file1
-rw-r--r-- 1 jp jp  284 2008-02-04 12:50 file2
drwxr-xr-x 2 jp jp 4.0K 2008-02-04 23:40 folder1
drwxr-xr-x 2 jp jp 4.0K 2008-02-04 23:41 folder2

Only the directory size has been change and you can see the K has been used for KiloBytes (KB). There is a more useful example in the folder2 directory.

$ cd folder2
$ ls -l
total 120
-rw-r--r-- 1 jp jp 104448 2008-02-04 23:41 file4.doc
-rwxr-xr-x 1 jp jp     48 2008-02-04 23:46 runme
$ ls -l -h
total 120K
-rw-r--r-- 1 jp jp 102K 2008-02-04 23:41 file4.doc
-rwxr-xr-x 1 jp jp   48 2008-02-04 23:46 runme

Note that 104,448 bytes is "rounded" to 102KB. This is not an error, it is simply that 1KB is 1,024 Bytes and not 1,000...

When several arguments are given to ls, they can be lumped together, therefore "ls -l -h" is identical to ls -lh".

There is a very useful combination of options for ls. To find the last file modified in a directory, use "ls -rtl". The "t" option use chronological order instead of alphabetical, "l" for long output and "r" for reverse, you end up with the youngest file or directory at the bottom of the list.

$ ls -rtl
total 32
-rw-r--r-- 1 jp jp   73 2008-02-04 12:46 file1
-rw-r--r-- 1 jp jp  284 2008-02-04 12:50 file2
drwxr-xr-x 2 jp jp 4096 2008-02-04 23:40 folder1
drwxr-xr-x 2 jp jp 4096 2008-02-04 23:46 folder2

In the example above, the directory folder2 contains the latest files.

Getting help

As you have seen with the ls example, some commands have many functions and it is easy to lose track. Luckily it is usually quite easy to get help for most commands. Two helps systems are available with the commands man and info. Just use either, followed by the name of the command you want help for. For instance to find help about the ls command using the "man" system, type::

man ls

Use the arrows to navigate up and down line by line, the space bar to go down a whole screen height, and enter q to quit. You can also enter "/word" to look for the string "word".

Similarly you can use the "info" help system:

info ls

Use the arrows to navigate up and down line by line, the space bar to go down a whole screen height, and enter q to quit. You can use enter on a any link identified by an asterisk to be taken directly to that content. You can also enter "/word" to look for the string "word".

The info system is newer than man and probably its successor but man is still very popular. Now, to get more information about them, you could try "info man" for instance...

Unix permissions

We saw earlier on that the long output of ls contains a block about file permissions. To understand this block, you need to know that users on a Linux system are organised in groups. Each file or directory has an owner who is a user and also belongs to a group. For each file and directory on a Linux system, it is possible to give permissions or access rights to the owner of a file, to the group to which the file belongs to ... and also to the other users of the system (i.e. those who don't own the file or belong to the group).

The permissions that can be given are called rad access (r), write access (w) and execute access (x). The permissions are usually listed together so a file with "rwx" for a user means that the user has all right on that file. "r-x" means the user can read and execute but not write/modify. "r--" means read only access. Note that in order to be able to enter a directory, a user need both read and execute access ("r-x").

The output of ls -l is now easier to understand, for instance:

$ ls -l
total 32
-rw-r--r-- 1 jp jp   73 2008-02-04 12:46 file1
-rw-r--r-- 1 jp jp  284 2008-02-04 12:50 file2
drwxr-xr-x 2 jp jp 4096 2008-02-04 23:40 folder1
drwxr-xr-x 2 jp jp 4096 2008-02-04 23:46 folder2

All files in this directory belong to the user jp and also the group jp. The user jp has read and write permissions on file1 and file2, users belonging to the group jp and other users only have read access. For the two directories folder1 and folder2, only the user jp can modify them. However, all users can enter them. Note that being able to modify a directory includes the right to add or remove files to the directory.

Often, the permissions are simplified (!) using a binary counting system where "--x" is 1, "-w-" is 2 and "r--" is 4. Therefore, "rwx" is 7 and "r-x" is 5. Using this system for the user, group and other users, the permissions of a file or directory can be sumarised as a 3 digit number. For instance, 755 can be used instead of "rwxr-xr-x" or 644 instead of "rw-r--r--". These two are the typical permissions for a directory and file respectively.

Note that on each line of the ls -l output, the first character marks the type of the entry, it is a dash "-" for a regular file, a "d" for a directory and it could be a "l" for a link. We will introduce links later.

The permissions on a file or directory can be changes with the command chmod.

$ chmod -r file1
$ ls -ltotal 48
--w------- 1 jp jp   73 2008-02-04 12:46 file1
-rw-r--r-- 1 jp jp  284 2008-02-04 12:50 file2
drwxr-xr-x 2 jp jp 4096 2008-02-04 23:40 folder1
drwxr-xr-x 2 jp jp 4096 2008-02-04 23:46 folder2
drwxr-xr-x 3 jp jp 4096 2008-02-05 01:36 folder_a
drwxr-xr-x 2 jp jp 4096 2008-02-05 01:34 new-folder

To add read and execute access to the group but not the owner or the others, use

$ chmod g+rx file1
$ ls -l
--w-r-x--- 1 jp jp   73 2008-02-04 12:46 file1
-rw-r--r-- 1 jp jp  284 2008-02-04 12:50 file2
drwxr-xr-x 2 jp jp 4096 2008-02-04 23:40 folder1
drwxr-xr-x 2 jp jp 4096 2008-02-04 23:46 folder2
drwxr-xr-x 3 jp jp 4096 2008-02-05 01:36 folder_a
drwxr-xr-x 2 jp jp 4096 2008-02-05 01:34 new-folder

Similarly, use the letter u for the user/owner, g for the group and o for the others. be careful, o is for the others, not the owner. chmod</tt. can also be used with a 3 digit permission summary too.

To change the owner or the group of a file, use the command chown. The syntax is:

chown user.group file

where "user" and "group" are the new owner and/or the new group for the file.

Creating/deleting/modifying

Now that we know about permissions, it is time to actually create a some content.

To create a directory, use the command mkdir:

$ mkdir new-folder
$ ls
file1  file2  folder1  folder2  new-folder

You can also create nested directory using the option "-p" (for create <>parents):

$ mkdir -p folder_a/folder_b/folder_c
$ ls
file1  file2  folder1  folder2  folder_a  new-folder
$ cd folder_a
$ ls
folder_b
$ cd folder_b
$ ls
folder_c

To create a new empty file, use the command touch

$ touch new_file
$ ls -rtl
total 4
-rw-r--r-- 1 jp jp 0 2008-02-05 01:38 new-file

New directories are usually created with the permissions 755 (rwxr-xr-x) and new files with 644 (rw-r--r--).

The touch command can actually be used to modify the time stamp of the file, i.e. it changes its latest modification date without changing its content.

2$ ls -rtl
total 4
-rw-r--r-- 1 jp jp 0 2008-02-05 01:38 new-file
bash-3.2$ touch new-file
bash-3.2$ ls -rtl
total 4
-rw-r--r-- 1 jp jp 0 2008-02-05 01:42 new-file