SCHOOL ON RADIO USE
FOR DIGITAL AND MULTIMEDIA COMMUNICATIONS


12th February 2002


Exercises on
basic Unix (Linux) commands



Carlo Fonda, the Abdus Salam ICTP
(Trieste - Italy)



 

 
 
 
 
 
 

1.1 Introduction

 

Hints:

 

System configuration

 

shell bash;

different accounts ;

User needs to have personal groups (user user belongs to group students, ..);

su;

virtual console

 

 

1.2 Access to the system and logout

 

Let's start with something wrong.

 

login: iuser[Enter]

 

Even if the account doesn't exist you'll be asked for a password

 

Password: merida[Enter]

 

Login incorrect

 

 

 

Let's start again

 

login: user[Enter]

 

Password: merida[Enter]

 

Last login: Wed Oct 17 10:45:11 on tty1

 

You get information about your last access

 

The prompt shell means the system is ready to receive your commands.

 

$

 

 

 

1.2.1 Switching account

 

logging out and then logging in with a different account

using su.

 

$ su root[Enter]

 

Password: ceidis[Enter]

 

If it is the right password you get identity and the rights of the typed user.

 

1.2.2 Virtual Consoles

 

[Alt+Fn] (where n is in the range 1..6).

 

You can start another login session on a different console

 

1.2.3 Who am I?

 

When you have more then one account may be necessary to check which account you are using.

 

$ whoami[Enter]

 

root

 

whoami let you know under which identity you are working on.

 

$ logname[Enter]

 

user

 

logname allows you to know what account you used when you first entered in the system (login).

 

1.2.4 Ending a session

 

Just need to end the shell, that is the program that shows the prompt.

 

$ whoami[Enter]

 

root

 

$ exit[Enter]

 

In case we had used su

 

$ whoami[Enter]

 

user 

 

This exit closes last shell, and logs the user out of the system.

 

$ exit[Enter]

 

login:

 

 

 

1.2.5 Shutting down

 

login: root[Enter]

 

Password: ceidis[Enter]

 

# shutdown -h now[Enter]

 

System is going down NOW!!

 

...

 

 

 

 

 

System halted

 

 

 

When this happens it is possible to reboot the system.

 

---------

 

You can use su to become root and shutdown the system.

 

$ su[Enter]

 

Password: ceidis[Enter]

 

# shutdown -h now[Enter]

 

1.3 Password management

 

1.3.1 User root changes password to regular user

 

# passwd user[Enter]

 

New UNIX password: 123[Enter]

 

The password is too short, the system advises you but it doesn't stop you from using it.

 

BAD PASSWORD: it's a WAY too short

 

 

 

Retype new UNIX password: 123[Enter]

 

passwd: all authentication tokens updated successfully

 

 

 

1.3.2 Regular user changes its password

 

$ passwd[Enter]

 

You'll be asked for the old password.

 

Changing password for user

 

 

 

(current) UNIX password: 123[Enter]

 

New UNIX password: elpoep[Enter]

 

BAD PASSWORD: it is based on a (reversed) dictionary word

 

passwd: Authentication token manipulation error

 

 

 

$ passwd[Enter]

 

Changing password for user

 

 

 

(current) UNIX password: 123[Enter]

 

New UNIX password: I cannot understand PCs[Enter]

 

Retype new UNIX password: I cannot understand PCs[Enter]

 

passwd: all authentication tokens updated successfully

 

 

 

1.4 Moving around directories

 

The filesystem is organizated in directory and subdirectory.

 

1.4.1 Current Directory

 

$ cd /usr/bin[Enter]

 

$ pwd[Enter]

 

/usr/bin

 

 

 

1.4.2 Absolute and relative path

 

The absolute path starts from the root directory while the relative one starts from the current directory.

 

$ cd /usr/local[Enter]

 

This is an absolute path since it starts with /.

 

$ pwd[Enter]

 

/usr/local

 

 

 

$ cd bin[Enter]

 

This is a relative one: it starts from the local directory moving downward to bin.

 

$ pwd[Enter]

 

/usr/local/bin

 

 

 

1.4.3 Moving backward

 

Every directory holds two references to special subdirectory:

 

single (.) that is a reference to the current directory.

double (..) that is a reference to the previous directory.

 

These symbols are effective directory names.

 

$ cd ..[Enter]

 

$ pwd[Enter]

 

/usr/local

 

 

 

$ cd ../bin[Enter]

 

$ pwd[Enter]

 

/usr/bin

 

 

You may go backward for more then one level.

 

$ cd ../../var/tmp[Enter]

 

$ pwd[Enter]

 

/var/tmp

 

 

 

$ cd /usr/bin/../local/bin/..[Enter]

 

$ pwd[Enter]

 

/usr/local

 

 

 

1.4.4 The precise reference to the current directory

 

The current directory can be seen as a single dot. In practice all relative paths can start with the prefix ./.

 

$ cd ./bin[Enter]

 

$ pwd[Enter]

 

/usr/local/bin

 

 

 

1.4.5 Home Directory

 

Every user has his own personal directory, known as home, it keeps all the data owned by the user. Typing just cd the user can reach his home directory.

 

$ cd[Enter]

 

$ pwd[Enter]

 

/home/user

 

 

 

Some shells replace tilde (~) at the begging of a path with the path of the home directory of the working user.

 

$ cd ~[Enter]

 

$ pwd[Enter]

 

/home/user

 

In the same way if tilde is set before a user account it will be replaced with the path of user's home directory.

 

$ cd ~ftp[Enter]

 

$ pwd[Enter]

 

/home/ftp

 

  

Going back to home directory.

 

$ cd[Enter]

 

1.5 File content

 

1.5.1 Directory content

 

To list the directory content command ls is used

 

$ ls /bin[Enter]







arch           dd             gzip       netconf        sleep
ash            df             hostname   netstat        sort
ash.static     dmesg          igawk      nice           stty
aumix-minimal  dnsdomainname  ipcalc     nisdomainname  su
awk            doexec         kill       ping           sync
basename       domainname     linuxconf  ps             tar
bash           echo           ln         pwd            tcsh
bash2          ed             loadkeys   red            touch
bsh            egrep          login      remadmin       true
cat            ex             ls         rm             umount
chgrp          false          mail       rmdir          uname
chmod          fgrep          mkdir      rpm            userconf
chown          fsconf         mknod      rvi            usleep
consolechars   gawk           mktemp     rview          vi
cp             gawk-3.0.4     more       sed            view
cpio           grep           mount      setserial      vimtutor
csh            gtar           mt         sfxload        ypdomainname
date           gunzip         mv         sh             zcat

 

The command ls /bin lists the content of /bin/.

 

A more expressive listing can be obtened using option -l.

 

$ ls -l /bin[Enter]

-rwxr-xr-x    1 root     root         2612 Mar  7 11:29 arch
-rwxr-xr-x    1 root     root        60592 Feb  3 20:12 ash
-rwxr-xr-x    1 root     root       263064 Feb  3 20:12 ash.static
-rwxr-xr-x    1 root     root         9968 Feb  3 19:04 aumix-minimal
lrwxrwxrwx    1 root     root            4 Apr 13 23:28 awk -> gawk
-rwxr-xr-x    1 root     root         5756 Mar  7 12:15 basename
-rwxr-xr-x    1 root     root       316848 Feb 27 18:44 bash

... many lines ...

-rwxr-xr-x    1 root     root         4320 Mar  7 12:15 true
-rwsr-xr-x    1 root     root        26608 Feb  3 15:14 umount
-rwxr-xr-x    1 root     root         6196 Mar  7 12:15 uname
lrwxrwxrwx    1 root     root           14 Apr 13 23:49 userconf -> /bin/linuxco
nf
-rwxr-xr-x    1 root     root        16252 Mar  8 17:26 usleep
-rwxr-xr-x    1 root     root       346352 Mar  7 18:18 vi
lrwxrwxrwx    1 root     root            2 Apr 14 00:00 view -> vi
-rwxr-xr-x    1 root     root          362 Mar  7 18:18 vimtutor
lrwxrwxrwx    1 root     root            8 Apr 13 23:51 ypdomainname -> hostname
-rwxr-xr-x    3 root     root        46384 Feb 15 17:04 zcat

 

$ cd[Enter]

 

$ ls[Enter]

 

To list hidden files

 

$ ls -a[Enter]

.              .bash_history  .enlightenment       .mc
..             .bash_logout   .gnome               .tcshrc
.ICEauthority  .bash_profile  .gnome-desktop       .xsession-errors
.Xauthority    .bashrc        .gnome-help-browser
.Xdefaults     .cshrc         .gnome_private

 

1.5.2 File Content

 

In order to analyse the file content cat,less and more ca be used (it doesn't make sense if it is a binary file).

 

$ cat /etc/issue[Enter]

 

Red Hat Linux release 6.2 (Zoot)
Kernel 2.2.14-5.0 on an i586

 

 

1.5.3 Find out the file type

 

file is command the uses the so called magic number (Unix tradition) to figure out the file type..

 

$ file /etc/*[Enter]

 

/etc/CORBA:                directory
/etc/DIR_COLORS:           English text
/etc/HOSTNAME:             ASCII text
/etc/X11:                  directory
/etc/adjtime:              ASCII text
/etc/aliases:              English text

...many lines...

/etc/shells:               ASCII text
/etc/skel:                 directory
/etc/smb.conf:             English text
/etc/smrsh:                directory
/etc/snmp:                 directory
/etc/sound:                directory
/etc/sysconfig:            directory
/etc/sysctl.conf:          English text
/etc/syslog.conf:          English text
/etc/termcap:              English text
/etc/up2date.conf:         can't read `/etc/up2date.conf' (Permission denied).
/etc/vga:                  directory
/etc/yp.conf:              English text
/etc/ypserv.conf:          English text

This method is not realible but can be useful.

 

1.5.4 Used and free space

 

In order to check the free space on disk you can use df.

 

$ df[Enter]

 

The result maybe something similar to this one.


Filesystem           1k-blocks      Used Available Use% Mounted on
/dev/hda5              1511968    805300    629860  56% /
/dev/hda1                99521      2482     91900   3% /boot

To check the directory used space: du.

 

$ du /bin[Enter]

 

5240 /bin

  

In this case the /bin/ directory holds files for a total amount of 5240KB.

 

1.6 File creation, copy and deletion

 

1.6.1 File creation

 

There are different ways to create a file. The easiest way to create an empty file si to use touch. First move to the home directory, the best place where to play.

 

$ cd[Enter]

 

$ touch myfile[Enter]

 

$ ls -l myfile[Enter]

 

-rw-rw-r-- 1 user user 0 Dec 23 10:49 myfile

 

The file was created..

 

You can use cat too:

 

$ cat > myfile2[Enter]

 

there are better ways to write[Enter]

 

text.[Enter]

 

This is a oneway writing.[Enter]

 

[Ctrl+d]

 

$ cat myfile2[Enter]

 

1.6.2 File copy

 

$ cp myfile2 myfile3[Enter]

 

Group copy is possible only if the last file is an existing directory.

 

$ cp myfile myfile2 myfile3 /tmp[Enter]

 

$ cp myfile* /tmp[Enter]

 

1.6.3 File deletion

 

Becareful when you delete something as root!!

 

$ rm myfile myfile2[Enter]

 

There is noway to recover deleted files.

 

You can use the wild chars:* and ?.

 

$ ls myfile*[Enter]

 

myfile3

 

$ rm myfile*[Enter]

 

1.7 Working with directory

 

1.7.1 Directory creation

 

$ cd[Enter]

 

$ mkdir mydir[Enter]

 

Let's check with ls.

 

$ ls -l[Enter]

 

... 

drwxr-xr-x   8    user    user  1024 Dec 23  12:11 mydir

...

 

The d character at the beginning of the string tells has that the file is a directory.

 

1.7.2 Directory copy

 

cp with option -r or-R.

 

$ cp -r mydir mydir2[Enter]

 

1.7.3 Directory deletion

 

You may delete an empty directory using rmdir.

 

$ rmdir mydir2[Enter]

 

Let's try with something more complex.

 

$ mkdir carbon[Enter]

 

$ mkdir carbon/hydrogen[Enter]

 

$ mkdir carbon/oxygen[Enter]

 

$ mkdir carbon/hydrogen/helium[Enter]

 

$ rmdir carbon[Enter]

 

rmdir: carbon: Directory not empty

 

$ rm -r carbon[Enter]

 

1.8 Moving around directories and linking files

 

In Unix environment rename and moving a file are the same thing.

 

1.8.1 Moving and renaming

 

The command used is mv.

 

$ touch white[Enter]

 

$ touch green[Enter]

 

$ mkdir purple[Enter]

 

Let's check.

 

$ ls -l[Enter]

 

...

 

-rw-rw-r--   1   user   user     0 Dec 25 12:46 white

 

-rw-rw-r--   1   user   user     0 Dec 25 12:46 green

 

drwxrwxr-x  2   user   user  1024 Dec 25 12:46 purple

 

...

 

 

 

Let's rename white file to make it brown.

 

$ mv white brown[Enter]

 

$ ls -l[Enter]

 

...

 

-rw-rw-r--   1     user    user    0 Dec 25 12:46  brown

 

...

 

 

 

To move more file all in once the destination must be a directory.

 

$ mv brown green purple[Enter]

 

$ ls -l purple[Enter]

 

-rw-rw-r-- 1 user user 0 Dec 25 12:46 green

 

-rw-rw-r-- 1 user user 0 Dec 25 12:46 brown

 

 

 

$ mv purple /tmp[Enter]

 

1.8.2 Linking

 

Instead of copying a file we may want to create a reference to it. There are two kind of links that can be created hard links and soft links. Let's see the soft one

 

The command is ln with the option -s.

 

Let's set the environment.

 

$ touch one[Enter]

 

$ touch two[Enter]

 

$ mkdir three[Enter]

 

Checking ..

 

$ ls -l[Enter]

 

...

 

-rw-rw-r--   1 user user     0 Dec 25 12:46  two

 

drwxrwxr-x  2 user user  1024 Dec 25 12:46  three

 

-rw-rw-r--   1 user user      0 Dec 25 12:46  one

 

 

 

$ ln -s one one.bis[Enter]

 

$ ls -l[Enter]

 

...

 

lrwxrwxrwx  1    user   user     3 Dec 25 12:47  one.bis -> one

 

 

 

It's the same for directories.

 

$ ln -s /tmp miatemp[Enter]

 

$ ln -s /home/user/one* /home/user/two three[Enter]

 

$ ls -l three[Enter]

 

lrwxrwxrwx  1   user   user   15 Dec 25 15:21  two -> /home/user/two

 

lrwxrwxrwx  1   user   user   15 Dec 25 15:21  one -> /home/user/one

 

lrwxrwxrwx  1   user   user   19 Dec 25 15:21  one.bis -> /home/user/one.bis

 

 

 

1.9 The shell

 

The shell is the way to interact with the operative systme. The shell bash is the one used in our exercises.

 

1.9.1 Automatic fulfilling

 

The shell can fulfill a command using [Tab], this feature is particularly useful when you have file with long names.

 

$ touch microprocessor[Enter]

 

$ touch microscopic[Enter]

 

$ touch supersonic[Enter]

 

$ ls sup[Tab]

 

$ ls sup[Tab]ersonic[Enter]

 

$ ls mic[Tab]ro

 

$ ls mic[Tab]rop[Tab]rocessor[Enter]

 

1.9.2 Substitution: wild char

 

This is an alternative way to fulfill a command, it is the the shell that changes symbols with the right information.

 

1.9.2.1 Asterisk *

 

That symbol can be replaced with a sequence, from 0 to infinity, of symbols.

 

$ ls[Enter]

 

$ ls *[Enter]

 

This command is different, the shell changes the * with the list of files and directory held in the current directory. This means that if there is any subdirectory its content

would be displayed.

 

$ ls micro*[Enter]

 

microprocessor microscopic

 

* can be changed with null string:

 

$ touch millimicro[Enter]

 

$ ls *micro*[Enter]

 

microprocessor microscopic millimicro

 

 

1.9.2.2 Question mark ?

 

The question mark ? can be change with just one symbol

 

Let's create some files.

 

$ touch xy123j4[Enter]

 

$ touch xy456j5[Enter]

 

$ touch xy789j111[Enter]

 

$ touch xy78j67[Enter]

 

$ ls [Enter]

 

xy123j4

xy456j5

xy789j111

xy78j67

 

$ ls ?????j?[Enter]

 

xy123j4

xy456j5

 

 

 

 

We would have a different result using *

 

$ ls *j*[Enter]

 

xy123j4 xy456j5 xy789j111 xy78j67

 

 

 

1.9.2.3 Square brackets [ ]

 

The square brackets are used to have a range of symbols from which to choose the substitution symbols. Just one symbol from the one listed is used.

 

$ ls xy????[4567]*[Enter]

 

xy123j4 xy456j5

 

 

$ ls xy????[4-7]*[Enter]

 

1.9.2.4 Escape

 

For some special symbols you need to use escape

 

$ touch six\*height[Enter]

 

$ ls[Enter]

 

...

 

six*height

 

If you create a filename with a space inside

 

$ touch my\ letter[Enter]

 

$ ls[Enter]

 

...

 

my letter

 

six*height

 

 

 

1.9.3 Input/Output redirection and pipeline

 

The shell allows you to redirect command output from the standard output (usually the screen). The same for the input.

 

1.9.4 Redirection

 

$ ls -l > mylist[Enter]

 

$ cat mylist[Enter]

 

For the input .. instead of using the standard input (keyboard).

 

$ cat < mylist[Enter]

 

Appending to a file.

 

$ ls -l /tmp >> mylist[Enter]

 

$ cat mylist[Enter]

 

1.9.5 Pipeline

 

The pipeline is a way of redirecting input and output commands.

 

$ cat mylist | sort[Enter]

 

$ cat < mylist | sort[Enter]

 

Make it easier but without using pipeline.

 

$ sort < mylist[Enter]

 

1.9.6 Alias

 

Aliases allows you to create an alternative name to an existing command.

 

$ alias ll='ls -l'[Enter]

 

$ ll[Enter]

 

This alias take options as an ordinary command.

 

$ ll micro*[Enter]

 

It's the same as ls -l micro*.

 

-rw-rw-r-- 1 user user 0 Dec 26 10:19 microprocessor

 

-rw-rw-r-- 1 user user 0 Dec 26 10:19 microscopic

 

 

This are the aliases that are usually created to avoid mistake

 

$ alias rm='rm -i'[Enter]

 

$ alias cp='cp -i'[Enter]

 

$ alias mv='mv -i'[Enter]

 

Now try to remove a file.

 

$ rm microprocessor[Enter]

 

rm: remove `microprocessor'?:

 

n[Enter]

 

In this way the file was not removed.

 

1.10 Searching

 

Searching files or directories it's an important task in a filesystem as complex as the Linux (or Unix) one.

 

1.10.1 Find

 

Searching a file or directory using its name or other external feature find command is used.

 

$ find / -name bash -print[Enter]

 

This command search for files and directory named bash inside all the directory starting from the root directory (/).

 

/bin/bash

 

...

 

find: /var/run/sudo: Permission denied

 

find: /var/spool/at: Permission denied

 

find: /var/spool/cron: Permission denied

 

...

 

 

 

You can use wild chars, in this case it will be the find command that will have to manage them, without using shell.

 

$ find / -name \*sh -print[Enter]

 

The escape char \ tells the shell not to translate the * as wild char.

 

/bin/bash

 

/bin/ash

 

/bin/sh

 

...

 

 

 

1.10.2 Grep

 

To search inside the content of a file grep is used.

 

$ grep user /etc/*[Enter]

 

/etc/group:user::500:user

 

/etc/passwd:user:Ide2ncPYY1234:500:500:userlab:/home/user:/bin/bash

 

grep: /etc/skel: Is a directory

 

grep: /etc/sudoers: Permission denied

 

...

 

 

 

1.16 References

 

Matt Chapman, Frankie Blaskovic, The Guide -- A Beginners Guide to UNIX

 

http://www.belgarath.demon.co.uk/guide/

 

Christopher C. Taylor, Unix is a Four Letter Word... and Vi is a Two Letter Abbreviation

 

http://www.linuxbox.com/~taylor/4ltrwrd/

 


 Back to the School Home Page

updated 17 October 2001, © 2001 CEIDIS-ULA, HTML by cfonda