You are on page 1of 120

YAT

Unauthorized reproduction or distribution prohibitedฺ Copyright© 2013, Oracle and/or its affiliatesฺ

s a
) h a
l ฺ c om
UNIX and Linux Essentials m ai eฺ
@ g uid
a n 8 t G
Activity Guide
zid tude n
e l
d is S
( a
i d an se th
a n Z to u
l r h m nse
b de e lice
e d A rabl
h am nsfe
M o -tra
l
de non
A
D76989GC10
Edition 1.0
June 2012
D77817
YAT

Copyright © 2012, Oracle and/or its affiliates. All rights reserved.

Disclaimer

This document contains proprietary information and is protected by copyright and other intellectual property laws. You may copy and
print this document solely for your own use in an Oracle training course. The document may not be modified or altered in any way.
Except where your use constitutes "fair use" under copyright law, you may not use, share, download, upload, copy, print, display,
perform, reproduce, publish, license, post, transmit, or distribute this document in whole or in part without the express authorization
of Oracle.
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2013, Oracle and/or its affiliatesฺ

The information contained in this document is subject to change without notice. If you find any problems in the document, please
report them in writing to: Oracle University, 500 Oracle Parkway, Redwood Shores, California 94065 USA. This document is not
warranted to be error-free.

Restricted Rights Notice

If this documentation is delivered to the United States Government or anyone using the documentation on behalf of the United
States Government, the following notice is applicable:

U.S. GOVERNMENT RIGHTS


The U.S. Government’s rights to use, modify, reproduce, release, perform, display, or disclose these training materials are restricted
by the terms of the applicable Oracle license agreement and/or the applicable U.S. Government contract.

Trademark Notice
s a
Oracle and Java are registered trademarks of Oracle and/or its affiliates. Other names may be trademarks of their respective ) h a
owners.
l ฺ c om
m ai eฺ
Authors
@ g uid
Uma Sannasi, Pardeep Kumar Sharma
a n 8 t G
zid tude n
Technical Contributor and Reviewer e l
d is S
( a
Harald van Breederode
i d an se th
a n Z Tutor
This book was published using: Oracle t o u
l r h m nse
b de e lice
e d A rabl
h a m nsfe
M o -tra
l
de non
A
YAT

Table of Contents
Practices for Lesson 1: Introduction ........................................................................................................ 1-1
Practices for Lesson 1............................................................................................................................. 1-2
Practice 1-1: Getting Familiar with Your Practice Environment .................................................................. 1-6
Practices for Lesson 2: Introduction to UNIX........................................................................................... 2-1
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2013, Oracle and/or its affiliatesฺ

Practices for Lesson 2............................................................................................................................. 2-2


Practice 2-1: Logging In and Changing Your User Password .................................................................... 2-3
Practice 2-2: Displaying System Information Using the Command Line ..................................................... 2-15
Practice 2-3: Using the Man Pages .......................................................................................................... 2-17
Practices for Lesson 3: Working with Files and Directories .................................................................... 3-1
Practices for Lesson 3............................................................................................................................. 3-2
Practice 3-1: Accessing Files and Directories ........................................................................................... 3-4
Practice 3-2: Using File and Directory Commands.................................................................................... 3-9
Practice 3-3: Locating Files and Text ....................................................................................................... 3-13
Practices for Lesson 4: Using the vi Editor .............................................................................................. 4-1
s a
Practices for Lesson 4............................................................................................................................. 4-2
) h a
l ฺ c om
Practice 4-1: Using the vi Editor ............................................................................................................... 4-3
Practices for Lesson 5: Using Commands Within the Default Shell ........................................................
m ai e5-2 ฺ
5-1
g i d
Practices for Lesson 5.............................................................................................................................

n 8@ t Gu 5-3
Practice 5-1: Using the Shell Metacharacters ...........................................................................................
a
l z id uden
Practice 5-2: Using Variables in the Bash Shell ........................................................................................ 5-5

d e t
Practice 5-3: Using the Command History................................................................................................
a S 5-7

n ( this
Practice 5-4: Using Redirecting Commands ............................................................................................. 5-10
Practice 5-5: Customizing the User's Work Environment
id usea.......................................................................... 5-12
Z
Practices for Lesson 6: Using Basic File Permissions
m an e............................................................................
to 6-1

l e n s
rh....................................................................................................
Practices for Lesson 6............................................................................................................................. 6-2
e
Practice 6-1: Changing File Ownership
l ic
d e .................................................................................................. 6-3
Practice 6-2: Changing AFilebPermissions l 6-6
d a b
fora
Practices h
me sfer Basic Process Control .................................................................... 7-1
Practice 6-3: Modifying Default
Lesson 7:nPerforming
Permissions............................................................................................. 6-10

M o for Lesson
- tra7............................................................................................................................. 7-2
e l
Practices
n
o Controlling System Processes ............................................................................................. 7-3
Ad Practicen7-1:
Practices for Lesson 8: Using Advanced Shell Features in Shell Scripts ............................................... 8-1
Practices for Lesson 8............................................................................................................................. 8-2
Practice 8-1: Using Advanced Bash Shell Functionality ............................................................................ 8-3
Practice 8-2: Using Shell Scripts.............................................................................................................. 8-9
Practices for Lesson 9: Archiving Files and Remote Transfer ................................................................ 9-1
Practices for Lesson 9............................................................................................................................. 9-2
Practice 9-1: Archiving and Retrieving Files ............................................................................................. 9-3
Practice 9-2: Compressing and Restoring Files ........................................................................................ 9-8
Practice 9-3: Performing Remote Connections and File Transfers ............................................................ 9-15

Copyright © 2012, Oracle and/or its affiliates. All rights reserved.

UNIX and Linux Essentials Table of Contents


YAT
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2013, Oracle and/or its affiliatesฺ

s a
) h a
l ฺ c om
m ai eฺ
@ g uid
a n 8 t G
zid tude n
e l
d is S
( a
i d an se th
a n Z to u
l r h m nse
b de e lice
e d A rabl
h am nsfe
M o -tra
l
de non
A
YAT
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2013, Oracle and/or its affiliatesฺ

s a
) h a
l ฺ c om
Practices for Lesson m ai eฺ
1:
g id
Introduction n8@ t Gu
i d a en
Chapter 1 el z u d
( a d is St
i d an se th
a n Z to u
l r h m nse
b de e lice
e d A rabl
h am nsfe
M o -tra
l
de non
A

Copyright © 2012, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 1: Introduction


Chapter 1 - Page 1
YAT

Practices for Lesson 1


Practices Overview
This practice provides an introduction to your course assignment and the infrastructure, which
you will use for performing the practices. The practices in this assignment are mapped to the
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2013, Oracle and/or its affiliatesฺ

respective lessons.
The following checklist will run throughout the practices showing your progress:
UNIX and Linux Essentials - Activities Checklist

Introduction

Introduction to UNIX

Working with Files and Directories


s a
) h a
Using the vi Editor
l ฺ c om
m ai eฺ
Using Commands within the Default Shell
@ g uid
a n 8 t G
Using Basic File Permissions
zid tude n
e l
d is S
( a
Performing Basic Process Control
i d an se th
a n Z in tShell
o u
Using Advanced Shell Functionalities
r h m n s e Scripts
l ice
deRemote
Archiving Files b
A and
l e lTransfer
m ed ferab
o ha rans
e l o n -t
M Infrastructure
Ad Practices
n
This section presents the architectural overview of the infrastructure required for the practices.
Your practice environment is based on the Oracle VM VirtualBox virtualization software. The
VirtualBox is a cross-platform virtualization application. It provides multiple virtual machines
(VMs) that are configured on a private internal network (192.168.1). Each VM can
communicate with other VMs on the same private network (see Figure 1). Internet access is not
configured for these VMs.

Copyright © 2012, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 1: Introduction


Chapter 1 - Page 2
YAT
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2013, Oracle and/or its affiliatesฺ

s a
) h a
l ฺ c om
m ai eฺ
@ g uid
a n 8 t G
zid tude n
e l
d is S
( a
i d an se th
a n Z to u
l r h m nse
deFigure l i e
cVirtual
b
A able 1: Pod Network Schema
d
e fer
am
h rans
o
M n-t
e l
Ad no

Copyright © 2012, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 1: Introduction


Chapter 1 - Page 3
YAT
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2013, Oracle and/or its affiliatesฺ

s a
) h a
l ฺ c om
m ai eฺ
@ g uid
a n 8 t G
zid tude n
e l
d is S
( a
i d an se th
a n Z to u
l r
Figure h 2:
e n seOracle VirtualBox VMs
mConfigured
b de virtual
Figure 2 shows the configured
e licmachines. The VirtualBox environment consists of the
A l
following VMs:
m ed ferab
haof theraVM
Name
o n s Description
e l M n -t
S11-Server
o This is the Oracle Solaris 11 guest OS image where the
Ad n student performs the practice tasks related to Oracle Solaris
11 OS. This is the primary VM.
OL6-Server This is Oracle Linux 6.2 guest OS image, which can be used
as an alternative by students to perform the practice tasks.

The VMs are further configured to communicate with the host machine through the shared
directory. The shared directories are listed in the following table:
Resource Name Location Description
Host share directory /opt/ora Contains various course files
Student Files /opt/ora/labs Contains lab bundle contents
The details of the shared directories can be verified in the respective VM settings.

Copyright © 2012, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 1: Introduction


Chapter 1 - Page 4
YAT

User Credentials

VMs Credentials
S11-Server • Username: student
• Password: student1
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2013, Oracle and/or its affiliatesฺ

Note: As a student user, use su to switch to the primary


administrator (root) role. The password is oracle1. The
root is configured as a role by default in Oracle Solaris 11.
The first user name created on the system during the
installation is the initial privileged user who can assume the
primary administrator role. This can be verified in the
/etc/user_attr file.
OL6-Server • Username: student
• Password: student1
s a
For administrative access, switch to root user using the su
) h a
om
command as and when instructed by the instructor.
• l ฺ c

Username: root
m ai eฺ
Password: oracle1
@ g uid
a n 8 t G
zid tude n
e l
d is S
( a
i d an se th
a n Z to u
l r h m nse
b de e lice
e d A rabl
h am nsfe
M o -tra
l
de non
A

Copyright © 2012, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 1: Introduction


Chapter 1 - Page 5
YAT

Practice 1-1: Getting Familiar with Your Practice Environment

Task1: Starting the Oracle VM Virtual Box Manager


Perform the following steps when you first access your practice host environment:
On your host system, start the Oracle VM Virtual Box Manager by double-clicking its icon on
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2013, Oracle and/or its affiliatesฺ

your desktop.

Task2: Starting and Logging in to the Virtual Machine


1. In the Oracle VM VirtualBox Manager window, double-click the S11-Server VM to start it.
Alternatively, you can select the S11-Server VM and click the Start button.

s a
) h a
l ฺ c om
m ai eฺ
@ g uid
a n 8 t G
zid tude n
e l
d is S
( a
i d an se th
a n Z to u
l r h m nse
b de e lice
e d A rabl
h am nsfe
M o -tra
l
de non
A

2. After the S11-Server VM is powered ON, log in with the username student and password
student1.

Copyright © 2012, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 1: Introduction


Chapter 1 - Page 6
YAT
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2013, Oracle and/or its affiliatesฺ

3. Similarly, to start the OL6-Server VM, log in to the virtual machine with the username
student and password student1.
Note: All VMs use this login combination.
s a
) h a
Task3: Opening a Terminal Window
l ฺ c om
1.
ai eฺ
After successfully logging in into the VMs, right-click on the desktop and select the Open
m
g uid
Terminal option in the Oracle Solaris VM or the Open in Terminal option in the Oracle
@
Linux VM.
a n 8 t G
zid tude n
Note: In Oracle Linux VM, the default directory will be /home/student/Desktop. You
e l
would require switching to /home/student directory in order to perform practice tasks.
d is S
( a
Your instructor will guide you through this process as you proceed to later practices.
2. i d an se th
In the terminal window, type the su command to assume primary administrator privileges.
3. a n Z to u
Enter oracle1 when prompted for password.
l r h m sunse
de e lice
student@S11-Server:~$
b
Password: oracle1
e d A rabl
root@S11-Server:~#
h a m nsfe
M
Task4:oClosing
- traand Powering off a Virtual Machine
l
de1. Close n
A noa VM by clicking the close (X) button that is located on the top-right corner of the
VM’s window.
2. In the Close Virtual Machine dialog box, select Send the shutdown signal option and click
OK.

Copyright © 2012, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 1: Introduction


Chapter 1 - Page 7
YAT

3. Repeat steps 7 and 8 to shut down the other VM, OL6-Server.


Unauthorized reproduction or distribution prohibitedฺ Copyright© 2013, Oracle and/or its affiliatesฺ

4. Verify that no VMs are running at this time, by viewing the status of the VMs in the Oracle
VM VirtualBox Manager window. The status of the VMs should read Powered Off.

Special Instruction
s a
• The practice tasks in this course are written for Oracle Solaris 11 environment (S11-
) h a
l ฺ c om
Server), but can also be performed on Oracle Linux environment (OL6-Server).

ai eฺ
Therefore, it is recommended that one system be used at a time, preferably Oracle
Solaris (S11-Server).
m
g uid

n 8 @
Students will perform the exercises in the /home/student directory. For Oracle
G
a n t
Linux, change the directory from Desktop to the /home/student directory. Your
l zid tude
instructor will help with how to change the directory.
e
• ( a d is S
The command output may vary from system to system.
• i d an se th
Shut down the VMs when not required. This releases system resources for the primary
VM.
a n Z to u
• h m nse
Follow the instructions in the practices with diligence for a smooth learning experience.
l r
b de e lice
e d A rabl
h am nsfe
M o -tra
l
de non
A

Copyright © 2012, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 1: Introduction


Chapter 1 - Page 8
YAT
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2013, Oracle and/or its affiliatesฺ

s a
) h a
l ฺ c om
Practices for Lesson m ai eฺ
2:
@ g uid
Introduction to
a n 8
UNIX t G
id ude n
Chapter 2 elz
( a d is St
i d an se th
a n Z to u
l r h m nse
b de e lice
e d A rabl
h am nsfe
M o -tra
l
de non
A

Copyright © 2012, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 2: Introduction to UNIX


Chapter 2 - Page 1
YAT

Practices for Lesson 2


Practices Overview
In this practice, you will perform a set of tasks described in the corresponding lesson. Here is
the list of those activities:
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2013, Oracle and/or its affiliatesฺ

• Log in to the system


• Change your user login password
• Display system information using the command line
• Use the man pages
• Log out of the system
UNIX and Linux Essentials - Activities Checklist

Introduction
s a
Introduction to UNIX ) h a
l ฺ c om
Working with Files and Directories
m ai eฺ
@ g uid
a n 8 t G
Using the vi Editor
zid tude n
e l
Using Commands within the Default Shell (ad i s S
d an se t h
i
Using Basic File Permissions n Z o u
a
m nse t
l r h
Performing Basic d
b e Control
Process l i ce
d A able
m e feShell r Functionalities in Shell Scripts
a
Using
s
Advanced
oh -tran
M
Adel nArchiving
on Files and Remote Transfer

Copyright © 2012, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 2: Introduction to UNIX


Chapter 2 - Page 2
YAT

Practice 2-1: Logging In and Changing Your User Password

Overview
In this practice, you learn to login, logout, and use some common date, time commands in a
UNIX system.
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2013, Oracle and/or its affiliatesฺ

Assumptions
It is highly recommended that one system is used at a time. Please make sure that the
instructions are followed as it is. In the task 3, you need to reboot the system as asked in the
step before performing the key combination.

Task 1: Logging in to the Oracle Solaris and Oracle Linux systems


Note: You will first perform the following steps in the S11-Server VM (Oracle Solaris 11) and
then in the OL6-Server VM (Oracle Linux 6.2). These VMs are exact replicas of the actual OS
s a
environment.
) h a
l ฺ c om
1. In the Oracle VM VirtualBox Manager window, double-click the S11-Server VM to start it.
ai eฺ
Alternatively, you can select the S11-Server VM and click the Start button.
m
@ g uid
a n 8 t G
zid tude n
e l
d is S
( a
i d an se th
a n Z to u
l r h m nse
b de e lice
e d A rabl
h am nsfe
M o -tra
l
de non
A

Copyright © 2012, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 2: Introduction to UNIX


Chapter 2 - Page 3
YAT

2. In the desktop Login screen, enter the user credentials.


Unauthorized reproduction or distribution prohibitedฺ Copyright© 2013, Oracle and/or its affiliatesฺ

a. Type the username, and press Enter.


b. Username: student
c. Type your password, and press Enter or the click the Log In button.
s a
d. Password: student1
) h a
3.
l ฺ c om
Similarly, in the Oracle VM VirtualBox Manager window, double-click the OL6-Server VM
ai eฺ
to start it. Alternatively, you can select the OL6-Server VM and click the Start button.
m
@ g uid
a n 8 t G
zid tude n
e l
d is S
( a
i d an se th
a n Z to u
l r h m nse
b de e lice
e d A rabl
h am nsfe
M o -tra
l
de non
A

a. In the desktop Login screen, enter the user credentials.


Copyright © 2012, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 2: Introduction to UNIX


Chapter 2 - Page 4
YAT
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2013, Oracle and/or its affiliatesฺ

b. The default username student is already listed there.


c. Type your password, and press Enter or click the Log In button.
d. Password: student1

Task 2: Changing your User Password s a


) h a
In Oracle Solaris 11:
l ฺ c om
1. Right-click in the desktop background. The workspace menu opens.
m ai eฺ
2. Select the Open Terminal option. A terminal window appears.
@ g uid
Note: In Oracle Linux 6.2, the menu item is Open in Terminal.
a n 8 t G
zid tude n
e l
3. Use the passwd command to change your password to mypass1.
d is S
student@S11-Server:~$ passwd ( a
passwd: Changing password foran student th
New Password: n Z o use
i d
m a e t
Re-enter new Password:
h s
lrsuccessfully
en changed for student
passwd: password d e l i c
d Ab able
student@S11-Server:~$
4. Close a m
the
e fer
terminalswindow.
h ranwindow, click System.
Onothe desktop
5. M
e l o n -tLog Out <username> to log out of the desktop environment. A logout
Ad 6. n
Next, click
confirmation window appears.
Note: The username is student (without quotes).

Copyright © 2012, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 2: Introduction to UNIX


Chapter 2 - Page 5
YAT

7. Click Logout Anyway.


Unauthorized reproduction or distribution prohibitedฺ Copyright© 2013, Oracle and/or its affiliatesฺ

8. Now, enter the following incorrect username and password on the Login screen:
s a
Username: student2
) h a
Password: wrong
l ฺ c om
The following dialog box appears indicating authentication failure.
m ai eฺ
@ g uid
a n 8 t G
zid tude n
e l
d is S
( a
i d an se th
a n Z to u
l r h m nse
bdeTheleLogin
9. Click OK or pressAEnter. licescreen reappears.
ed user r a b
10. Login with correct
m sf e credentials.
o ha rstudent
Username:
a n
e l o n -tmypass1
MPassword:
Ad n
11. Now, repeat steps 1,2, and 3 to reset the default password of the user before moving to the
next task. The default password is student1.

In Oracle Linux 6.2:


1. Right-click the desktop environment background. The workspace menu appears.
2. Select Open in Terminal from this menu. A terminal window appears.
3. Use the passwd command to change your password to mypass1.
Note: Oracle Linux follows a stricter password authentication mechanism, by default.
Therefore, you need to use a strong password.
[student@OL6-Server Desktop]$ passwd
Changing password for user student.
Changing password for student.
(current) UNIX password:
New password:
Copyright © 2012, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 2: Introduction to UNIX


Chapter 2 - Page 6
YAT

Retype new password:


passwd: all authentication tokens updated successfully.
[student@OL6-Server Desktop]$
4. Close the terminal window.
5. On the desktop environment, click System.
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2013, Oracle and/or its affiliatesฺ

6. Next, click Log Out <username> to log out of the desktop environment. A logout
confirmation window appears.

s a
Note: Username is student.
) h a
7. Click Log Out or press Enter to continue with logging out.
l ฺ c om
8. Now click Other to try an incorrect username and password.
m ai eฺ
@ g uid
a n 8 t G
zid tude n
e l
d is S
( a
i d an se th
a n Z to u
l r h m nse
b de e lice
e d A rabl
amfollowings e
fincorrect
o h
9. Enter the
a n username and password on the Login screen:

e l MUsername:
o n -trstudent2
Ad Password:
n wrong
10. The following dialog box appears indicating authentication failure.

Click OK or press Enter. The Login screen reappears.

Copyright © 2012, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 2: Introduction to UNIX


Chapter 2 - Page 7
YAT

11. Log in with the correct user credentials.


Username: student
Password: mypass1
12. Now, repeat steps 1, 2, 3, and 4 to reset the default password of the user before moving to
the next task. The default password is student1.
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2013, Oracle and/or its affiliatesฺ

Task 3: Accessing a Terminal Window and Logging in using the Command Line

In Oracle Solaris 11 (S11-Server):


1. Restart the system by clicking on menu item – System. Choose Shut Down and then click
the Restart button.

s a
) h a
l ฺ c om
m ai eฺ
@ g uid
a n 8 t G
zid tude n
e l
d is S
( a
i d an se th
a n Z to u
l r h m nse
b de e lice
e d A rabl
h am nsfe
M o -tra
l
de non
A

Copyright © 2012, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 2: Introduction to UNIX


Chapter 2 - Page 8
YAT
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2013, Oracle and/or its affiliatesฺ

s a
) h a
l ฺ c om
m ai eฺ
@ g uid
a n 8 t G
zidappears,de n
e l
a. Once the system is restarted and graphical login window
d is S t u
( a
i d an se th
a n Z to u
l r h m nse
b de e lice
e d A rabl
h am nsfe
M o -tra
l
de non
A

Copyright © 2012, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 2: Introduction to UNIX


Chapter 2 - Page 9
YAT

Press CTRL+ALT+F1 to switch to the command line login. This switches the view to
console login.
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2013, Oracle and/or its affiliatesฺ

s a
) h a
l ฺ c om
m ai eฺ
@ g uid
a n 8 t G
zid tude n
e l
d is S
( a
an sestudent
b. On the console prompt, login with the username
i d th and password student1.
Z use
c. To confirm the current working directory,
n o uthe pwd command.
S11-Server console m a studente t
l r h login:
e n s
Password: student1
b de e lic
A SatabMay
dCorporation
Last login: l 12 20:11:44 on rad/0
m
Oracle e f e r SunOS 5.11 11.0 November 2011
a
ohYou -have s
annew mail.
M t r
Adel nstudent@S11-Server:~$
on
/home/student
pwd

d. To revert to the graphics mode, Press CTRL+ALT+F7.

Copyright © 2012, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 2: Introduction to UNIX


Chapter 2 - Page 10
YAT
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2013, Oracle and/or its affiliatesฺ

s a
) h a
l ฺ c om
m ai eฺ
@ g uid
a n 8 t G
zid tude n
e l
d is S
( a
i d an se th
a
e. In the Login window, enter the nuserZ credentials.
t o u
Username: studentlrhm
e n se
b de e lic
Password: student1
e
f. Click the dLogAIn button.
r a bl
g. h a
Right-click
e and select the Open Terminal option.
m nthesfDesktop
M o -tview ra the current directory using the pwd command.
l n
h. Again,
de no
A student@S11-Server:~$
student@S11-Server:~$ pwd
/home/student
student@S11-Server:~$

In Oracle Linux 6.2 (OL6-Server):


1. Restart the system by clicking on menu item – System. Choose Shut Down and then click
the Restart button.

Copyright © 2012, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 2: Introduction to UNIX


Chapter 2 - Page 11
YAT
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2013, Oracle and/or its affiliatesฺ

s a
) h a
l ฺ c om
m ai eฺ
@ g uid
a n 8 t G
zid tude n
e l
d is S
( a
i d an se th
a n Z to u
l r h m nse
b de e lice
e d A rabl
h am nsfe
M o -tra
l
de non
A

Copyright © 2012, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 2: Introduction to UNIX


Chapter 2 - Page 12
YAT

2. Once the system is restarted and the graphical login window appears, press CTRL+ALT+F6
on the login window to switch to a text console and perform a non-GUI login.
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2013, Oracle and/or its affiliatesฺ

s a
) h a
l ฺ c om
m ai eฺ
@ g uid
a n 8 t G
zid tude n
This switches the view to console login. e l
d is S
( a
i d an se thand password student1
3. In the console prompt, log in with the username student

a n Z to u
l r h m nse
b de e lice
e d A rabl
h a m nsfe
M o -tra
l
de non
A

Copyright © 2012, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 2: Introduction to UNIX


Chapter 2 - Page 13
YAT

4. To confirm the current working directory, enter the pwd command.


OL6-Server login: student
Password: student1
Last login: Mon May 14 12:19:27 from s11-server.suned.sun.com
[student@OL6-Server ~]$ pwd
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2013, Oracle and/or its affiliatesฺ

/home/student
[student@OL6-Server ~]$
5. To revert to the graphical mode, press CTRL+ALT+F1.
6. You may be prompted to enter the password information for the user student. When done,
the GUI interface is launched.

s a
) h a
l ฺ c om
m ai eฺ
@ g uid
a n 8 t G
zid tude n
e l
d is S
( a
i d an se th
a n Z to u
l r h m nse
b de e lice
e d A rabl
h am nsfe
M o -tra
l
de non
A

Note: If you have already logged out, you will be prompted to enter both the user name and
password.

Copyright © 2012, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 2: Introduction to UNIX


Chapter 2 - Page 14
YAT

Practice 2-2: Displaying System Information Using the Command Line

Overview
In this practice, you will display information about the OS and system.

Assumptions
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2013, Oracle and/or its affiliatesฺ

It is recommended to use one system at a time. Students will perform the exercises in the
/home/student directory. For Oracle Linux change the directory from Desktop to the
/home/student directory. Your instructor will help you on how to change the directory. The
output may vary from system to system.

Tasks
Ensure that the S11-Server is running.
1. To open a terminal window, right-click on the desktop and select the Open Terminal
option.
s a
2. Display information about the OS and system.
) h a
student@S11-Server:~$ uname -a
l ฺ c om
SunOS S11-Server 5.11 11.0 i86pc i386 i86pc
m ai eฺ
@ g uid
Note: Operating system x 86 versions of SUNOS
a n 8 t G
zid tude n
3. Display information about the OS name.
e l
d is S
student@S11-Server:~$ uname -s
( a
SunOS
i d an se th
n Z level.
4. Display information about the OS release
a t o u
student@S11-Server:~$
l r h m uname
e n se-r
5.11
b de e lic
d A rabl
student@S11-Server:~$
e
h a m nsfe
M o Note:-tOS
rarelease level is 5.11
l
de5. Display n
A no the current data and time.
student@S11-Server:~$ date
Monday, April 30, 2012 06:14:21 PM IST
6. Display the current month’s calendar.
student@S11-Server:~$ cal
April 2012
S M Tu W Th F S
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30

Copyright © 2012, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 2: Introduction to UNIX


Chapter 2 - Page 15
YAT

7. Display the calendar with a specific month and year; for example, June 2012.
student@S11-Server:~$ cal 06 2012
June 2012
S M Tu W Th F S
1 2
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2013, Oracle and/or its affiliatesฺ

3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
8. Clear the terminal window.
student@S11-Server:~$ clear
9. Display the current date and host system name.
student@S11-Server:~$ date; hostname
Wednesday, May 23, 2012 03:28:42 PM IST s a
S11-Server ) h a
l ฺ c om
Note: Hostname value for Oracle Linux will be OL6-Server.
m ai eฺ
@ g information.
uid
8
10. Display the calendar for March 2012, the current date, and the OS release
G
an /etc/release
t
student@S11-Server:~$ cal 03 2012; date; dcat
i e n
March 2012
d e lz
S t ud
S M Tu W Th F S n (a this
1 2 3
Z ida use
4 5 6 7 8 9 10 an to
h m s e
11 12 13 14 15 16
d e lr 17 icen
18 19 20 21b22 23 24l
25 26 e27
A 29a30
d 28 b le31
a m s f er
M ohWednesday,
- t r an May 23, 2012 03:29:10 PM IST
Adel non
Oracle Solaris 11 11/11 X86
Copyright (c) 1983, 2011, Oracle and/or its affiliates. All
rights reserved.
Assembled 18 October 2011
student@S11-Server:~$

Note: In OL6-Server run cat /etc/*-release command.

Copyright © 2012, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 2: Introduction to UNIX


Chapter 2 - Page 16
YAT

Practice 2-3: Using the Man Pages

Overview
In this practice, you learn to use the man command to extract additional information about
system commands.
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2013, Oracle and/or its affiliatesฺ

Assumption
It is recommended that one system be used at a time. Students will perform the exercises in the
/home/student directory. For Oracle Linux, change the directory from Desktop to
the/home/student directory. Your instructor will help on how to change the directory.

Tasks
1. Displaying a man page and man page section
a. To display the man page for the exit command, run the man command.
student@S11-Server:~$ man exit
s a
b. To display the man page section 1 for the exit command, run the command as below.) ha
student@S11-Server:~$ man exit –s1
l ฺ c om
c. To display the man page section 2 for the exit command, run the command m ai as below:
eฺ
g i d
student@S11-Server:~$ man exit –s2
a n 8@ t Gu
Note: You can use the keys and related functions of the z
l idcommand
man
u d en
as listed in the
table in the following step.
a e
d is S t
(
n e th
d. Scrolling in a man page.
i d a
Z o ususing man command, use the keys or
To learn how to search for online documentation
nbelow
the functions as mentioned in a
m the
e t
table.
h
lr icman s
en uname –s1
student@S11-Server:~$
d e
b options l
Note: Practice the
d Akey a b le provided in the table below with the above command to
achieve thee r
a m f e
desired functions as mentioned.
o h rans
Keyst
2.l M on-
Function
e
Ad n Space bar Displays the next screen of a man page
Return Displays the next line of a man page
b Move back one full screen of the man page
/pattern Searches forward for a pattern
n Find the next occurrence of the pattern
Q Quits the man command and returns to the shell prompt

Display the man page for the uname command.


student@S11-Server:~$ man uname
3. Display section 1 of the man page for the uname command.
student@S11-Server:~$ man uname –s1

Copyright © 2012, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 2: Introduction to UNIX


Chapter 2 - Page 17
YAT

4. Search the man pages for information on the man command using the keyword “man”.
student@S11-Server:~$ man man | grep man
5. Display section 1 of the man page for the man command and review its contents.
student@S11-Server:~$ man man -s1
6. To quit the man page, press q.
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2013, Oracle and/or its affiliatesฺ

student@S11-Server:~$ man man <Enter>


(Press ‘q’)
Reformatting page. Please Wait... done
7. To logout from the system, type the exit command.
student@S11-Server:~$
student@S11-Server:~$ exit

Note: The exit command will close the terminal


s a
) h a
l ฺ c om
m ai eฺ
@ g uid
a n 8 t G
zid tude n
e l
d is S
( a
i d an se th
a n Z to u
l r h m nse
b de e lice
e d A rabl
h am nsfe
M o -tra
l
de non
A

Copyright © 2012, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 2: Introduction to UNIX


Chapter 2 - Page 18
YAT
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2013, Oracle and/or its affiliatesฺ

s a
) h a
l ฺ c om
Practices for Lesson m ai eฺ
3:
Working with nFiles 8 @gand G uid
d a e n t
Directories lz i ud
d e S t
n (a3 this
Chapter
Z ida use
m an e to
e l rh cens
A bd le li
m ed ferab
o ha rans
e l M n-t
Ad no

Copyright © 2012, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 3: Working with Files and Directories


Chapter 3 - Page 1
YAT

Practices for Lesson 3

Practices Overview
In this practice, you will perform a set of tasks described in the corresponding lesson. Here is
the list of those activities:
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2013, Oracle and/or its affiliatesฺ

• Display user information


• Display directory contents
• Display file types
• Change directories
• Access files
• Copy files and directories
• Move files and directories
• Create files and directories
• Remove files and directories
s a
• Use symbolic links ) h a
• Search files and directories
l ฺ c om
m ai eฺ
@ g uid
a n 8 t G
zid tude n
e l
d is S
( a
i d an se th
a n Z to u
l r h m nse
b de e lice
e d A rabl
h am nsfe
M o -tra
l
de non
A

Copyright © 2012, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 3: Working with Files and Directories


Chapter 3 - Page 2
YAT

UNIX and Linux Essentials - Activities Checklist

Introduction

Introduction to UNIX
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2013, Oracle and/or its affiliatesฺ

Working with Files and Directories

Using the vi Editor

Using Commands within the Default Shell

Using Basic File Permissions

s a
Performing Basic Process Control
) h a
l ฺ c om
Using Advanced Shell Functionalities in Shell Scripts
m ai eฺ
@ g uid
Archiving Files and Remote Transfer
a n 8 t G
zid tude n
e l
d is S
( a
i d an se th
a n Z to u
l r h m nse
b de e lice
e d A rabl
h a m nsfe
M o -tra
l
de non
A

Copyright © 2012, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 3: Working with Files and Directories


Chapter 3 - Page 3
YAT

Practice 3-1: Accessing Files and Directories

Overview
In this practice, you will be directed to run some file and directory access commands. Also, in a
few instances, you will be prompted to answer some questions
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2013, Oracle and/or its affiliatesฺ

Assumptions
It is recommended that one system be used at a time. Students will perform the exercises in the
/home/student directory. For Oracle Linux, change the directory from Desktop to the
/home/student directory. Your instructor will help with changing the directory. In a few
instances, the output may vary on your system.

Preparation
In this practice, you need to use the files and directories available in the /home/student/lab
directory.
s a
Tasks
) h a
1. Open a terminal window by right-clicking on the desktop.
l ฺ c om
2. Select the Open Terminal option.
m ai eฺ
3. Display user information using the id command.
@ g uid
a n 8 t G
student@S11-Server:~$ id
zid tude n
uid=60004(student) gid=10(staff) e l
d is S
student@S11-Server:~$ ( a
i d an se th
Note: The output may differ in Linux environment.
a n Zusingtthe
4. Display your current working directory o upwd command.
l r h m pwdnse
de e lice
student@S11-Server:~$
/home/student b
A abl
e d home
m
5. Change to your
a s f erdirectory from any location using the cd command.
M ohstudent@S11-Server:~$
- t r an cd

Adel nstudent@S11-Server:~$
on
/home/student
pwd

student@S11-Server:~$
6. Display the contents of your current working directory using the ls command.
student@S11-Server:~$ ls
Desktop Documents Downloads lab Public
student@S11-Server:~$

Note: Command output may vary from system to system.


7. Display all files, including any hidden files, using the ls –a command.
student@S11-Server:~$ ls -a
. .local
.. .nautilus

Copyright © 2012, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 3: Working with Files and Directories


Chapter 3 - Page 4
YAT

.audioctl .profile
.bash_history .recently-used.xbel
.bashrc .updatemanager
.config .vboxclient-clipboard.pid
.dbus .vboxclient-display.pid
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2013, Oracle and/or its affiliatesฺ

.dbus-keyrings .vboxclient-seamless.pid
.dmrc .vp
.gconf .xsession-errors
.gconfd .xsession-errors.old
.gnome2 Desktop
.gnome2_private Documents
.gstreamer-0.10 Downloads
.gtk-bookmarks lab
.ICEauthority Public
s a
student@S11-Server:~$
) h a
8.
l ฺ c om
Display a long list of the contents of the current working directory using the ls –l
command.
m ai eฺ
student@S11-Server:~$ ls -l
@ g uid
total 12
a n 8 t G
zid tude n
drwxr-xr-x 2 student staff
e l
d is S
5 Apr 9 19:03 Desktop
drwxr-xr-x 6 student staff
( a 6 Apr 9 19:03 Documents
drwxr-xr-x 2 student staff
i d an se th 2 Apr 9 19:03 Downloads
drwxr-xr-x
n Z to u
17 student root
a
39 Apr 30 19:30 lab
drwxr-xr-x
l r h m nse
2 student staff 2 Apr 9 19:03 Public

de e lice
student@S11-Server:~$
b
d A inrayour
9. Display the file types
e bl current working directory using the ls –F command.
h a m nsfe
student@S11-Server:~$ ls -F

M o Desktop/
- tra Documents/ Downloads/ lab/ Public
l n
de nstudent@S11-Server:~$
o
A 10. Change to the dir1 directory using the cd command.
student@S11-Server:~$ cd lab
student@S11-Server:~/lab$ cd dir1
student@S11-Server:~/lab/dir1$
11. Display a long list of the contents of the current working directory using ls –l command.
student@S11-Server:~/lab/dir1$ ls -l
total 9
drwxr-xr-x 3 root root 5 Apr 30 19:30 coffees
drwxr-xr-x 2 root root 2 Apr 30 19:30 fruit
drwxr-xr-x 2 root root 2 Apr 30 19:30 trees
student@S11-Server:~/lab/dir1$

Copyright © 2012, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 3: Working with Files and Directories


Chapter 3 - Page 5
YAT

12. Change to the fruit directory using the cd command.


student@S11-Server:~/lab/dir1$ cd fruit
student@S11-Server:~/lab/dir1/fruit$ pwd
/home/student/lab/dir1/fruit
student@S11-Server:~/lab/dir1/fruit$
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2013, Oracle and/or its affiliatesฺ

13. Change to the planets directory available under $HOME/dir3 directory using the relative
path name.
student@S11-Server:~/lab/dir1/fruit$ cd ../../dir3/planets
student@S11-Server:~/lab/dir3/planets$ pwd
/home/student/lab/dir3/planets

Now, return to the home directory

student@S11-Server:~/lab/dir3/planets$ cd
s a
student@S11-Server:~$ pwd
) h a
/home/student
l ฺ c om
student@S11-Server:~$
m ai eฺ
14. Change to the dir1 directory by using the absolute path name.
@ g uid
a n 8 t G
student@S11-Server:~$ cd /home/student/lab/dir1
id ude n
student@S11-Server:~/lab/dir1$ pwd elz
/home/student/lab/dir1 ( a d is St
student@S11-Server:~/lab/dir1$ i d th
ancd s~/lab/dir1
e
a
student@S11-Server:~/lab/dir1$ n Z tocdu
l
student@S11-Server:~$
r h m pwdnse
/home/studentde
b l i ce
d A able
student@S11-Server:~$
m e fer
a
h rans directory by using the relative path name.
15. Change
o
to the /etc

e l o n -t
M student@S11-Server:~$ pwd
Ad n/home/student
student@S11-Server:~$ cd ../../../etc
student@S11-Server:/etc$ pwd
/etc
student@S11-Server:/etc$
student@S11-Server:/etc$ cd ~/lab
student@S11-Server:~/lab$
student@S11-Server:/lab$ cd dir1

Now, return to the home directory

student@S11-Server:~/lab/dir1$ cd
student@S11-Server:~$ pwd

Copyright © 2012, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 3: Working with Files and Directories


Chapter 3 - Page 6
YAT

/home/student
student@S11-Server:~$
16. Display the contents of the fruit file by using the cat command.
student@S11-Server:~$ cd lab
student@S11-Server:~/lab$ cat fruit
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2013, Oracle and/or its affiliatesฺ

lemon
orange
apple
banana
pear
mango
tomato
pomegranate
17. Under what circumstances must you refrain from using the cat command?
s a
The cat command should not be used for viewing binary files. ) h a
ฺ c om
18. Display the contents of the fruit and fruit2 files using a single command.
l
student@S11-Server:~/lab$ cat fruit fruit2 m ai eฺ
@ g uid
lemon
a n 8 t G
orange
zid tude n
apple e l
d is S
( a
an se th
banana
i d
n Z to u
pear
mango a
m nse
tomato l r h
e lice
pomegranatebd
d A able
m e fer
o a
h rans
lemon

e l M orange
o n -t
Ad napple
banana
tomato
guava
mango
pomegranate
student@S11-Server:~/lab$

Copyright © 2012, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 3: Working with Files and Directories


Chapter 3 - Page 7
YAT

19. Display the first five lines of the /usr/dict/words file on the screen.
student@S11-Server:~/lab$ head -5 /usr/dict/words
10th
1st
2nd
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2013, Oracle and/or its affiliatesฺ

3rd
4th
student@S11-Server:~/lab$

Note: In the Oracle Linux environment (OL6-Server), the file path is


/usr/share/dict/words.
20. Display the last eight lines of the /usr/dict/words file on the screen.
student@S11-Server:~/lab$ tail -8 /usr/dict/words
Zorn
s a
Zoroaster
) h a
Zoroastrian
l ฺ c om
zounds
m ai eฺ
z's
@ g uid
zucchini
a n 8 t G
zid tude n
Zurich
e l
d is S
Zygote
( a
student@S11-Server:~/lab$
i d an se th
21. How to distinguish between the head
a n Zand tail t o ucommands?
The head command displays
l r h m selines of a file, while the tail command displays
the first 10
e n
de e lic
the last 10 lines of a file.
b
e d A number
22. Determine the total
r a blof lines contained in the /usr/dict/words file using wc –l
command.
h a m nsfe
M o -tra
student@S11-Server:~/lab$ wc -l /usr/dict/words

Adel non25146 /usr/dict/words


Note: Output may vary from system to system
23. What does the ~ symbol represent?
The ~ symbol represents the user’s home directory.

Copyright © 2012, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 3: Working with Files and Directories


Chapter 3 - Page 8
YAT

Practice 3-2: Using File and Directory Commands

Overview
In this practice, you will perform some actions on files and directories using file and directory
commands.
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2013, Oracle and/or its affiliatesฺ

Assumptions
It is recommended that one system be used at a time. Students will perform the exercises in the
/home/student directory. For Oracle Linux, change the directory from Desktop to the
/home/student directory. Your instructor will help with changing the directory. In a few
instances, the output may vary on your system.

Tasks
To use file and directory commands, complete the following steps:
1. Return to your home directory, if you need to, and list the content in that directory.
s a
student@S11-Server:~/lab$ cd
student@S11-Server:~$ pwd ) h a
l ฺ c om
ai eฺ
/home/student
student@S11-Server:~$ ls m
g uid
Desktop Documents Downloads lab
8
Public
n @ G
a n t
student@S11-Server:~$
l id ude
zyour
2. The files and directories are located in the lab directory
a e
d is Shome
in t directory. Therefore,
n
to switch to the lab directory, use the cd command. ( h
student@S11-Server:~$ cd Z ida use t
lab
3. Copy the /dir1/coffees/beans/beans m an e file tointo the dir4 directory, and call it roses.
h s
lr icen cp dir1/coffees/beans/beans dir4/roses
d e
student@S11-Server:~/lab$
b vegetables l
d
4. Create a directory Acalled b l e in dir3.
e
m nsfe r a
o h a
student@S11-Server:~/lab$
a
mkdir dir3/vegetables
5. M
e l o n -tr
Move the dir1/coffees/beans/beans file into the dir2/recipes directory.
Ad nstudent@S11-Server:~/lab$ mv dir1/coffees/beans/beans
dir2/recipes
6. Command options for the reference in the table below:
7.
Option Description
cp -i Prevents you from accidentally overwriting existing files or
directories
-r Includes the contents of a directory, including the contents of all
subdirectories, when you copy a directory
From your lab directory, create a directory called practice1.
student@S11-Server:~/lab$ mkdir practice1
8. Using a single command, copy the file.1 and file.2 files to the practice1 directory.
student@S11-Server:~/lab$ cp file.1 file.2 practice1

Copyright © 2012, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 3: Working with Files and Directories


Chapter 3 - Page 9
YAT

9. Copy dir3/planets/mars file to the practice1 directory, and name the file
addresses.
student@S11-Server:~/lab$ cp dir3/planets/mars
practice1/addresses
10. Create a directory called play in your practice1 directory, and move the
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2013, Oracle and/or its affiliatesฺ

practice1/addresses file to the play directory.


student@S11-Server:~/lab$ mkdir practice1/play
student@S11-Server:~/lab$ mv practice1/addresses practice1/play
11. Using a single command, copy the play directory in the practice1 directory to a new
directory in the practice1 directory called appointments.
student@S11-Server:~/lab$ cp -r practice1/play
practice1/appointments
12. Recursively list the contents of the practice1 directory.
student@S11-Server:~/lab$ ls -R practice1
s a
practice1:
) h a
appointments file.1 file.2 play
l ฺ c om
m ai eฺ
practice1/appointments:
@ g uid
addresses
a n 8 t G
zid tude n
e l
d is S
practice1/play:
( a
addresses
i d an se th
student@S11-Server:~/lab$
a n Z to u
13. In your home directory, create
l r h e n secalled house with a subdirectory called
ma directory
furniture using a d
b e command.
single
lic
d A b l e
e r a
student@S11-Server:~/lab$ cd; mkdir -p house/furniture
memptynsfilefecalled chairs in the new furniture directory.
14. Createaan
h
o student@S11-Server:~$
l M n - tra touch house/furniture/chairs
d e n o
A 15. Using a single command, create three directories called records, memos, and misc in
your home directory.
student@S11-Server:~$ mkdir records memos misc
16. Create a new file called carrot, and rename it to celery.
student@S11-Server:~$ touch carrot
student@S11-Server:~$ mv carrot celery
17. Using a single command, remove the directories called memos and misc from your home
directory.
student@S11-Server:~$ rmdir memos misc
or
student@S11-Server:~$ rm -r memos misc

Copyright © 2012, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 3: Working with Files and Directories


Chapter 3 - Page 10
YAT

18. Try to remove the directory called house/furniture with the rm (no options) command.
Observe what happens.
student@S11-Server:~$ rm house/furniture
rm: house/furniture is a directory
19. Identify the command to remove a directory that is not empty. Remove the
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2013, Oracle and/or its affiliatesฺ

house/furniture directory. List the contents of the house directory to verify that the
furniture directory has been removed.
student@S11-Server:~$ rm -r house/furniture
student@S11-Server:~$ ls house
student@S11-Server:~$
20. Create a new directory named newname, and rename it veggies.
student@S11-Server:~$ mkdir newname
student@S11-Server:~$ mv newname veggies
student@S11-Server:~$
s a
21. Create a file named mycontents that is a symbolic link to the file
) h a
/var/sadm/install/contents.
l ฺ c om
(In Solaris)
m ai eฺ
student@S11-Server:~$ ln -s /var/sadm/install/contents
@ g uid
mycontents
a n 8 t G
zid tude n
(In Linux)
e l
d is S
n ( a
[student@OL6-Server ~]$ ln -s /home/student/lab/myvars
h
newvars
dathe Solaris
Note: Subsequent commands are listed ifor
t
e environment. For Linux, VM use the
Z u
respective file named, newvars asnshown above.
s
m a e to
22. Verify that the symbolic linkh
lr icmore
works. s
en mycontents
d e l
Ab able
student@S11-Server:~$

e d
/dev/vboxguest=../devices/pci@0,0/pci80ee,cafe@4:vboxguest s
m SUNWv fer
o a ans
none
hboxguest
e l o n -tr
M /etc/fs/vboxfs
Ad n/etc/fs/vboxfs/mount=../../../opt/VirtualBoxAdditions/i386/vboxf
d none 0755 root bin SUNWvboxguest

smount
s none SUNWvboxguest
/opt/VirtualBoxAdditions d none 0755 root bin SUNWvboxguest
/opt/VirtualBoxAdditions/1099.vboxclient f none 0755 root bin
1531 6218
4 1324300136 SUNWvboxguest
/opt/VirtualBoxAdditions/LICENSE f none 0644 root bin 20137
29940 13243
00136 SUNWvboxguest
/opt/VirtualBoxAdditions/VBox.sh f none 0755 root bin 1547 59419
132430
0136 SUNWvboxguest

Copyright © 2012, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 3: Working with Files and Directories


Chapter 3 - Page 11
YAT

/opt/VirtualBoxAdditions/VBoxClient=VBoxControl l none
SUNWvboxguest
/opt/VirtualBoxAdditions/VBoxControl f none 0755 root bin 11004
15922 1
324300136 SUNWvboxguest
/opt/VirtualBoxAdditions/VBoxISAExec=VBoxControl l none
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2013, Oracle and/or its affiliatesฺ

SUNWvboxguest
/opt/VirtualBoxAdditions/VBoxService=VBoxControl l none
SUNWvboxguest
/opt/VirtualBoxAdditions/amd64 d none 0755 root bin
SUNWvboxguest
<q> Type q to quit the mycontents file view.
student@S11-Server:~$
23. Remove the symbolic link that you created in Step 21.
student@S11-Server:~$ rm mycontents
s a
student@S11-Server:~$ ls mycontents
) h a
mycontents: No such file or directory
l ฺ c om
student@S11-Server:~$
m ai eฺ
@ g uid
a n 8 t G
zid tude n
e l
d is S
( a
i d an se th
a n Z to u
l r h m nse
b de e lice
e d A rabl
h am nsfe
M o -tra
l
de non
A

Copyright © 2012, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 3: Working with Files and Directories


Chapter 3 - Page 12
YAT

Practice 3-3: Locating Files and Text

Overview
In this practice, you will use grep and related commands to locate files and text in files.

Assumptions
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2013, Oracle and/or its affiliatesฺ

In the below tasks, we will use /etc/system file, which is the system configuration file in
Oracle Solaris. The equivalent file in Oracle Linux is /etc/sysctl.conf. It is recommended
that one system be used at a time. Students will perform the exercises in the /home/student
directory. For Oracle Linux, change the directory from Desktop to the /home/student
directory. In a few instances, the output may vary on your system.

Tasks
1. How do the grep, egrep, and fgrep commands differ?
• The grep command searches the contents of one or more files for a character pattern.
s a
• The egrep command searches the contents of one or more files for one or more
) h a
patterns using extended regular expression metacharacters.
l ฺ c om

ai eฺ
The fgrep command searches a file for a literal string or a group of characters.
2. Search for the text string root in the /etc/group file and display it ongtom
@ Guid
the screen.
student@S11-Server:~$ grep root /etc/group n8 t
i d a e n
root::0:
d e lz
S t ud
other::1:root
n (a this
bin::2:root,daemon
Z ida use
sys::3:root,bin,adm
adm::4:root,daemon ma
n to
h
r cen s e
uucp::5:root el
b d e li
A l
ed ferab
mail::6:root
m
tty::7:root,adm
a ans
hlp::8:root,adm
o -tr
M nuucp::9:root
d e l o n
A n daemon::12:root
student@S11-Server:~$
3. Using the grep command, look for all lines in the file4 file located in the lab directory
under your home directory that does not contain the letter M.
student@S11-Server:~$ cd lab
student@S11-Server:~/lab$ grep -v M file4
The budget for quarter 2 is:
student@S11-Server:~/lab$
4. Display all lines in the dante, file1, and dante_1 files that contain the pattern, “he”.
student@S11-Server:~/lab$ grep he dante file1 dante_1
dante: The Life and Times of Dante
dante:"Dante," instead, and the whole world knows whom you mean.
For
Copyright © 2012, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 3: Working with Files and Directories


Chapter 3 - Page 13
YAT

dante:Dante Alighieri, like Raphael, Michelangelo, Galileo, etc.


is usually
dante:referred to by his first name. There is only one Dante,
as we recognize
dante:one Raphael, one Michelangelo, and one Galileo.
dante:Who is this Dante, whom T.S. Eliot calls "the most
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2013, Oracle and/or its affiliatesฺ

universal of poets
dante:in the modern languages?"
<lot of output …>
student@S11-Server:~/lab$
5. Display all the lines in the file file4 that contains either the pattern, “Sales” or
“Finance”.
student@S11-Server:~/lab$ egrep 'A(Sales|Finance)' file4
student@S11-Server:~/lab$ egrep '(Sales|Finance)' file4
Sales..................12M s a
Finance................4.5M ) h a
l ฺ c om
ai eฺ
6. Which option is peculiar to the grep command but does not apply to the egrep and
fgrep commands? m
g uid
The -w option is peculiar to the grep command alone.
n 8 @ G
a n t
lzid tude
7. Display all the lines that have the pattern “load” in the /etc/system file.
student@S11-Server:~/lab$ grep loadd/etc/system
a e S
( h i s
* Modules appearing in the moddir
i d an se t path which are NOT to be

n Z to u
loaded,
* forceload: a
m ntosebe loaded at boot time, (just before
h
Cause these lrmodules
de e lice
*
mounting b
A aexpects
*
e d
forceload
r bl a filename which includes the directory.
m e
sf loading a module does not necessarily imply that
h* a note
Also
o a n
e l o -tr
M it nwill that

Ad n * forceload: drv/foo

Note: In Oracle Linux VM (OL6-Server), use the pattern “kernel” in the


/etc/sysctl.conf file.
8. Use the grep command to display the number of lines that contain at least one instance of
the word “module”, both in uppercase and lowercase, in the /etc/system file.
student@S11-Server:~/lab$ grep -ic module /etc/system
10

Note: Output may vary from system to system.

Copyright © 2012, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 3: Working with Files and Directories


Chapter 3 - Page 14
YAT

9. Use the grep command to record how many instances of the word Module (uppercase M
only) are there in the /etc/system file.
student@S11-Server:~/lab$ grep -c Module /etc/system
1
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2013, Oracle and/or its affiliatesฺ

Note: Output may vary from system to system.


10. Starting with the lab directory, find all the files that were modified in the last one day.
student@S11-Server:~/lab$ find /home/student -mtime -1
/home/student
/home/student/.gconfd
/home/student/.gconfd/saved_state
/home/student/.vboxclient-clipboard.pid
/home/student/celery
/home/student/.bash_history
s a
/home/student/.gnome2
) h a
<lot of output …>
l ฺ c om
student@S11-Server:~/lab$
m ai eฺ
11. Starting with your home directory, find all files of type f for file.
@ g uid
a n 8 t G
student@S11-Server:~/lab$ cd
student@S11-Server:~$ find ~ -type f lzid de n
e t u
/home/student/.gconfd/saved_state(ad s S
an se t h i
i
/home/student/.vboxclient-clipboard.pid d
n Z to u
/home/student/Desktop/.os-icons-installed
a
r h m nse
/home/student/Desktop/os-next-steps.desktop
l
de e lice
/home/student/Desktop/addmoresoftware.desktop
b
A a...>
<lot ofdoutput bl
e r
fe find all files of type d for directory.
m directory,
12. In yourahome
h n s
o student@S11-Server:~$
l M n - tra find ~ -type d
d e o
n/home/student
A /home/student/.gconfd
/home/student/Desktop
/home/student/Public
/home/student/.gnome2
/home/student/.gnome2/keyrings
<lot of output ...>

Copyright © 2012, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 3: Working with Files and Directories


Chapter 3 - Page 15
YAT

13. Run the find command to search for ordinary files of size 0 (zero) in the /tmp directory.
Include an option prompting you with yes or no option before removing any files.
student@S11-Server:~$ pwd
/home/student
student@S11-Server:~$ find /tmp -type f -size 0 -ok rm A{} \;
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2013, Oracle and/or its affiliatesฺ

find: cannot read dir /tmp/gdm-auth-cookies-zEaqxb: Permission


denied
find: cannot read dir /tmp/hsperfdata_root: Permission denied
< rm ... /tmp/crontab.826 >? No
< rm ... /tmp/.ai.pkg.zone.lock-afdb66cf-1dd1-11b2-a049-
000d560ddc3e >? No
student@S11-Server:~$

Note: Ensure that you answer no when prompted to remove any files. The output may
vary from system to system.
s a
) h a
l ฺ c om
m ai eฺ
@ g uid
a n 8 t G
zid tude n
e l
d is S
( a
i d an se th
a n Z to u
l r h m nse
b de e lice
e d A rabl
h am nsfe
M o -tra
l
de non
A

Copyright © 2012, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 3: Working with Files and Directories


Chapter 3 - Page 16
YAT
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2013, Oracle and/or its affiliatesฺ

s a
) h a
l ฺ c om
Practices for Lesson m aiUsing
4: eฺ
g i d
the vi Editor n8@ t Gu
i d a en
Chapter 4 el z u d
( a d is St
i d an se th
a n Z to u
l r h m nse
b de e lice
e d A rabl
h am nsfe
M o -tra
l
de non
A

Copyright © 2012, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 4: Using the vi Editor


Chapter 4 - Page 1
YAT

Practices for Lesson 4


Practices Overview
In this practice, you will use the vi editor commands to create and modify files in Oracle Solaris
11 VM (S11-Server).
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2013, Oracle and/or its affiliatesฺ

UNIX and Linux Essentials - Activities Checklist

Introduction

Introduction to UNIX

Working with Directories and Files

s a
Using the vi Editor
) h a
l ฺ c om
Using Commands within the Default Shell
m ai eฺ
@ g uid
Using Basic File Permissions
a n 8 t G
zid tude n
e l
d is S
Performing Basic Process Control ( a
i d an se th
n Zand tShell
Using Advanced Shell Functionality
a o uScripts
l r h m nse
deRemote
Archiving Files and
b l i ce
Transfer

d A able
m e fer
. o a
h rans
e l M n-t
Ad no

Copyright © 2012, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 4: Using the vi Editor


Chapter 4 - Page 2
YAT

Practice 4-1: Using the vi Editor

Overview
In this practice, you will perform tasks mentioned below using the in-built tutorial tutor.vi.
You will use file creation and modification commands documented in this tutorial.
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2013, Oracle and/or its affiliatesฺ

Note: Oracle Linux uses vim editor which is an enhanced version of the vi editor. In fact Vim is
the default editor in both Oracle Solaris 11 and Oracle Linux.

Assumptions
The following tasks are performed in the Oracle Solaris 11 VM (S11-Server). The tasks can also
be replicated in the Oracle Linux VM (OL6-Server) with little modifications, mentioned where
applicable. It is recommended to use one system at a time and perform theses task in
/home/student directory. For Oracle Linux, change the directory from Desktop to the
/home/student directory.

Task
s a
Perform the following task:
) h a
c om
1. To open the tutor.vi file, first ensure that you are in the home directory. Now, switch to
l ฺ
the lab directory using the cd command.
m ai eฺ
student@S11-Server:~$ pwd
@ g uid
a n 8 t G
/home/student
zid tude n
student@S11-Server:~$ cd lab
e l
d is S
student@S11-Server:~/lab$ ( a
d
2. To view the files in the read-only mode, iuse
n command.
aview e th
n t us
Zviewotutor.vi
student@S11-Server:~/lab$ a
l r h m
e n sel to move right, down, up, and left respectively.
3. To move the cursor, press
b e lic
h,
dsaving, j, k, and
A that
4. To quit the file without
d b e press ESC to enter into command mode and then use the
l:q!
:q! command.
m e fer Notice a appears at the bottom of the terminal window screen.

o
5. Next, a nscalled example. The terminal window screen will be replaced by the vi
hcreateraafile
l interface. t
M n- top of the terminal window is the blinking cursor. At the bottom of the
At the
e no window, you will see “example” [New File]. Press i to change to input mode
d terminal
A and insert the following text:
Hello World
What is your
Waht id today’s date?
student@S11-Server:~/lab$ vi example
Hello World
What is your
Waht id today’s date?
~
~
~
"example" [New File]

Copyright © 2012, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 4: Using the vi Editor


Chapter 4 - Page 3
YAT

Note: To move to next line to insert the sentence, press Enter.


6. To append text to the line What is your, press ESC to enter command mode. Pressing
the key ‘k’ will place the cursor at the last character of the line. Further, press the key ‘a’ to
append and insert a space with the next string “name?”.
Hello World
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2013, Oracle and/or its affiliatesฺ

What is your name?


Waht id today’s date?
~
~
"example" [New File]
7. To replace character d with character s in the line, What id today’s date?, press ESC
to return to command mode. Then move the cursor to the third line by pressing the key ‘j’.
This will move the cursor down. To move the cursor to the left, press the key ‘h’. Bring the
cursor to the character d in the string “id”. Press the key ‘r,’ and then insert character s.
This will replace the character d with the appropriate character s. s a
Hello World ) h a
What is your name?
l ฺ c om
Waht is today’s date? m ai eฺ
@ g uid
~
a n 8 t G
~
zid tude n
~ e l
d is S
( a
an se th
"example" [New File]
i d
Note: Ensure that you are in the command mode before you press the “r” command.
Z ou
a npress t and move the cursor to the third line. Place
8. To change the word Waht to What,
h m s eESC
lr ‘a’ oficthe
your cursor on the character
d e nword Waht and execute the cw command. Enter the
eword
A b
text hat. This will replace the
l e l
whole Waht to what.
d
Hello eWorld r a b
m
a isanyour e
sf name?
o hWhat r today’s date?
e l M What
o n -tis
Ad n~
~
~
Note: Ensure you are in the command mode before executing the “cw” command.
9. To copy and paste the line Hello World, press ESC to return to the command mode.
Move the cursor to the beginning of the Hello World line by pressing the keys ‘k’
followed by ‘h’ . Execute the yy command to copy the string. Then move the cursor to the
end of the same line by pressing the ‘l’ key and press the p command to paste the string.
The whole line is copied and pasted.
Hello World
Hello World
What is your name?
What is today’s date?
~
Copyright © 2012, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 4: Using the vi Editor


Chapter 4 - Page 4
YAT

~
Note: Ensure you are in the command mode before pressing the yy and p
commands.
10. To delete the additional “Hello World” line, press ESC to enter the command mode.
Move the cursor to the beginning of the second line “Hello World” and press dd
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2013, Oracle and/or its affiliatesฺ

command. The entire line is deleted.


Hello World
What is your name?
What is today’s date?
~
~
~
Note: Ensure you are in command mode before you press dd command.
11. To search for a string “What”, press ECS to enter the command mode. Now press forward
s a
slash (/). Enter the text “What” and press Enter. The cursor automatically moves to the
) h a
terminal window screen. l ฺ om
first string in the file that it encounters. Notice that “/What” appears at the bottom of the
c
m ai eฺ
Hello World
@ g uid
What is your name?
a n 8 t G
What is today’s date?
zid tude n
~ e l
d is S
( a
~
i d an se th
n Z to u
~
/What a
m nse
l r h
b dare ce
e in thelicommand
d A able
Note: Ensure you mode before you press the forward slash key.
12. To search e r occurrence of the same string press n. Now the cursor will move to
ethe
theh am forstring
second
the next
n s f
in file.
o t r
M Hello- Worlda
Adel non What is your name?
What is today’s date?
~
~
~
search hit BOTTOM, continuing at TO

Note: Make sure you are in command mode before you press n command.

Copyright © 2012, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 4: Using the vi Editor


Chapter 4 - Page 5
YAT

13. To customize the session by displaying the line numbers, press ESC to enter the
command mode. Then enter the :set nu command and press Enter. Notice that :set
nu appears at the bottom of the terminal window screen.

Hello World
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2013, Oracle and/or its affiliatesฺ

What is your name?


What is today’s date?
~
~
:set nu
The output displayed is:
1 Hello World
2 What is your name?
3 What is today’s date?
s a
~
) h a
~
l ฺ c om
~
m ai eฺ
:set nu
@ g uid
a n 8 t G
Note: Ensure you are in the command mode before z identeruthe
you n
de:set nu command.
e l
d command t
14. To remove the line numbers, press ECS to entera
( the
i s Smode. Next, type the :set
nonu command and press Enter. The lineanumbers
d t h
n edisappear.
i s
Hello World
a n Z to u
What is your name? m
l r h e n se
What is today’s
b de date? e lic
~
d A a b l
~ me e r
o h~ a ransf
e l M :set
o n -tnonu
Ad n
Note: Make sure you are in command mode before you press :set nonu command.

Copyright © 2012, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 4: Using the vi Editor


Chapter 4 - Page 6
YAT

15. To quit and save the file with the changes, press ESC to enter the command. Then type
:wq and press Enter. Notice that :wq appears at the bottom of the terminal window
screen. The file is saved and the prompt returns. To switch to home directory, type the cd
command.
Hello World
What is your name?
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2013, Oracle and/or its affiliatesฺ

What is today’s date?


~
~
~
:wq

student@S11-Server:~/lab$
student@S11-Server:~/lab$ cd
student@S11-Server:~/$ s a
) h a
Note: Ensure you are in the command mode before you enter :wq command.
l ฺ c om
Note: For more information, refer to the tutor.vi file. m ai eฺ
@ g uid
a n 8 t G
zid tude n
e l
d is S
( a
i d an se th
a n Z to u
l r h m nse
b de e lice
e d A rabl
h a m nsfe
M o -tra
l
de non
A

Copyright © 2012, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 4: Using the vi Editor


Chapter 4 - Page 7
YAT
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2013, Oracle and/or its affiliatesฺ

s a
) h a
l ฺ c om
m ai eฺ
@ g uid
a n 8 t G
zid tude n
e l
d is S
( a
i d an se th
a n Z to u
l r h m nse
b de e lice
e d A rabl
h am nsfe
M o -tra
l
de non
A
YAT
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2013, Oracle and/or its affiliatesฺ

s a
) h a
l ฺ c om
Practices for Lesson m aiUsing
5: eฺ
g u i d
Commands Within
a n 8@ the
t G
zid tuden
Default eShell
l
( a d is S
i d an se th
Chapter 5

a n Z to u
l r h m nse
b de e lice
e d A rabl
h am nsfe
M o -tra
l
de non
A

Copyright © 2012, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 5: Using Commands Within the Default Shell


Chapter 5 - Page 1
YAT

Practices for Lesson 5


Practices Overview
In this practice, you will perform a set of tasks described in this lesson. Here is the list of those
activities:
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2013, Oracle and/or its affiliatesฺ

• Use the shell metacharacters


• Use variables in the Bash shell
• Display the command history
• Use the redirecting commands
• Customize the user’s work environment
UNIX Essentials Activities Checklist

Introduction
s a
Introduction to UNIX ) h a
l ฺ c om
Working with Files and Directories
m ai eฺ
@ g uid
a n 8 t G
Using the vi Editor
zid tude n
e l
d is S
( a
n e th
Using Commands within the Default a
i d
Z o us
Shell

a n t
h m
Using Basic File Permissions s e
d e lr icen
A b Process
l e l
ed ferab
Performing Basic Control
m
ha Advanced ns Shell Functionalities in Shell Scripts
M oUsing
- t r a
Adel non Archiving Files and Remote Transfer

Copyright © 2012, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 5: Using Commands Within the Default Shell


Chapter 5 - Page 2
YAT

Practice 5-1: Using the Shell Metacharacters

Overview
In this practice, you will use shell metacharacters to simplify commands, structure, and output.
Note: Bash is the default shell which is used in both Oracle Solaris 11 and Oracle Linux 6.2.
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2013, Oracle and/or its affiliatesฺ

Assumptions
The following tasks are performed in the Oracle Solaris 11 VM (S11-Server). The tasks can also
be replicated in the Oracle Linux VM (OL6-Server) with little modifications, mentioned where
applicable. In a few instances, the output may vary on your system. It is recommended to use
one system at a time. Students will perform the exercises in the /home/student directory. For
Oracle Linux change the directory from Desktop to the /home/student directory.

Tasks
1. To verify that the default shell, bash, is running, run the echo command on the Oracle
Solaris VM and Oracle Linux VM. s a
student@S11-Server:~$ echo $SHELL ) h a
/usr/bin/bash
l ฺ c om
[student@OL6-Server Desktop]$ echo $SHELL m ai eฺ
/bin/bash @ g uid
a n 8 t G
zid tude n
Metacharacter Symbol Choices or Values e l
d is S
( a
tilde ~ Representsahome
i d e th of current user
n directory
n Z previous
t o us working directory
ma nsanye number of characters
dash - Represents
asterisk * lrh Match
b d e lice
question mark
d A ?able Match single character
squarem e fer[ ]
bracket Match range of characters
a
oh to-the s
n home directory using the ~ metacharacter with the cd command.
auser
M
2. Switch t r
Adel nstudent@S11-Server:~$
on pwd
/home/student
student@S11-Server:~$ cd lab/Documents
student@S11-Server:~/lab/Documents$ cd ~
student@S11-Server:~$
student@S11-Server:~$ pwd
/home/student

Copyright © 2012, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 5: Using Commands Within the Default Shell


Chapter 5 - Page 3
YAT

3. Switch between the student and tmp directory using the – metacharacter.
student@S11-Server:~$ cd /tmp
student@S11-Server:/tmp$ pwd
/tmp
student@S11-Server:/tmp$ cd -
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2013, Oracle and/or its affiliatesฺ

/home/student
student@S11-Server:~$ cd -
/tmp
student@S11-Server:/tmp$ cd
student@S11-Server:~$
4. List all the files and directories in the lab directory that end with the number 2.
student@S11-Server:~$ cd lab
student@S11-Server:~/lab$ ls *2
file.2 file2 fruit2
s a
) h a
dir2:
l ฺ c om
beans notes recipes
m ai eฺ
student@S11-Server:~/lab$
@ g uid
a n 8 t Gby any other
5. List all the files and directories that start with the string “file” and
zid tude n
are followed
character.
e l
d is S
student@S11-Server:~/lab$ ls file? ( a
file1 file2 file3 file4 dan
i e th
n Z o us
student@S11-Server:~/lab$
m a e t
l h
6. List all the files and directories that s
r cen letters m through z using square brackets.
start with
e
bd le li
student@S11-Server:~/lab$ ls [m-z]*
d A b
myvarse
m f e ra
tutor.vi

o a
h rans
e l M practice:
o n -t
Ad nmailbox project projection research results
student@S11-Server:~/lab$ cd
student@S11-Server:~$

Note: The command output may vary from system to system.

Copyright © 2012, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 5: Using Commands Within the Default Shell


Chapter 5 - Page 4
YAT

Practice 5-2: Using Variables in the Bash Shell

Overview
In this practice, you will use variables to store values.

Assumption
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2013, Oracle and/or its affiliatesฺ

It is recommended to use one system at a time. Students will perform the exercises in the
/home/student directory. For Oracle Linux, change the directory from Desktop to the
/home/student directory. The output may vary from system to system.

Tasks
1. Display the value stored inside a shell variable using the echo command.
student@S11-Server:~$ echo $SHELL
/usr/bin/bash
2. List all shell variables and their values using the set command.
s a
student@S11-Server:~$ set ) h a
A__z='"*SHLVL'
l ฺ c om
BASH=/usr/bin/bash
m ai eฺ
@ g uid
BASHOPTS=cmdhist:expand_aliases:extquote:force_fignore:hostcompl
a n 8 t G
ete:interactive_comments:progcomp:promptvars:sourcepath:xpg_echo
zid tude n
………….
e l
d is S
…………. ( a
………….
i d an se th
<lot of output…>
a n Z to u
………….
l r h m nse
………….
b de e lice
e d A rabl
_=/usr/bin/bash

h am nsfe
_AST_FEATURES='UNIVERSE - att'

M o -tra <end>
l
de non
A Note: The output may vary for a different system.
3. Customize shell prompt string by editing the shell variable PS1.
student@S11-Server:~$ PS1="$LOGNAME@`uname -n` \$PWD $ "
student@S11-Server /home/student $
In the above task, the prompt displays the login name of the user, host name, and the
current working directory.
Note: Type the command as it is. These symbols do not represent single quotation marks.
4. To revert to the default prompt, type the following command:
student@S11-Server /home/student $ PS1="$LOGNAME@`uname -n`:~$"
student@S11-Server:~$

Note: This will revert the prompt to default prompt.

Copyright © 2012, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 5: Using Commands Within the Default Shell


Chapter 5 - Page 5
YAT

or
student@S11-Server :~$ exit

Note: This will exit the current terminal session.


5. Launch a new terminal session by right-clicking the desktop and selecting “Open terminal.”
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2013, Oracle and/or its affiliatesฺ

Display current values in the PATH variable.


student@S11-Server:~$ echo $PATH
/usr/bin:/usr/sbin

Note: The PATH variablle is used to store reference to the commands directory.
6. Add home directory of student to the PATH variable using the echo $PATH command.
student@S11-Server:~$ PATH=$PATH:~
student@S11-Server:~$ echo $PATH
/usr/bin:/usr/sbin:/home/student s a
) h a
l ฺ c om
m ai eฺ
@ g uid
a n 8 t G
zid tude n
e l
d is S
( a
i d an se th
a n Z to u
l r h m nse
b de e lice
e d A rabl
h am nsfe
M o -tra
l
de non
A

Copyright © 2012, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 5: Using Commands Within the Default Shell


Chapter 5 - Page 6
YAT

Practice 5-3: Using the Command History

Overview
In this practice, you will view and set values for command history.

Assumption
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2013, Oracle and/or its affiliatesฺ

It is recommended to use one system at a time. Students will perform the exercises in the
/home/student directory. For Oracle Linux, change the directory from Desktop to the
/home/student directory. The output may vary from system to system.

Tasks
1. Check the current size of the history command and set the size to 20 using the
HISTSIZE variable.
student@S11-Server:~$ echo $HISTSIZE
45
s a
student@S11-Server:~$ HISTSIZE=20
) h a
l ฺ c om
Note: The number may vary from system to system.
m ai eฺ
2. Confirm that the command history database size is updated to 20.
@ g uid
a n 8 t G
student@S11-Server:~$ echo $HISTSIZE
zid tude n
20 e l
d is S
( a
i d an| more
3. View the page-wise output of the history command.
e th
student@S11-Server:~$ history
n Z o us
28 cd ~/lab
m a e t
29 cd h
lr icen s
30 pwd b d e l
31 ed
A b l e
m
cd /etc
e r a
o ha32 racdnsf
e l M n33-t pwd
Ad no 34 cd /etc/group
35 cat /etc/group
36 grep other /etc/group
37 grep root /etc/group
38 history
39 history | more
40 history | tail -10
41 history | tail -10
42 history | tail -10
43 history | wc -l
44 history | wc -l
45 HISTSIZE=20
46 history | wc -l

Copyright © 2012, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 5: Using Commands Within the Default Shell


Chapter 5 - Page 7
YAT

47 history | more

Note: The command output may vary from system to system.


4. To view the last 10 commands from the history database, use the option tail -10.
student@S11-Server:~$ history | tail -10
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2013, Oracle and/or its affiliatesฺ

39 history | more
40 history | tail -10
41 history | tail -10
42 history | tail -10
43 history | wc -l
44 history | wc -l
45 HISTSIZE=20
46 history | wc -l
47 history | more
s a
48 history | tail -10
) h a
l ฺ c om
Note: The command output may vary from system to system.
m ai eฺ
@ g # 46).uiPlease
5. To execute a specific command from history, use !46 (Repeats command d
8
an ent
choose the relevant command number that appears on your system. G
i d
student@S11-Server:~$ history | more lz
d e S t ud
30 pwd
n (a this
31 cd /etc
Z i da use
32 cd
a n to
h m s e
33 pwd
d e lr icen
Ab/etc/group
34 cd /etc/group
l e l
m
35 d cat
rab /etc/group
e grepfeother
o ha37 ragrep
36
ns root /etc/group
M - t
Adel non38 history
39 history | more
40 history | tail -10
41 history | tail -10
42 history | tail -10
43 history | wc -l
44 history | wc -l
45 HISTSIZE=20
46 history | wc -l
47 history | more
48 history | tail -10
49 history | more

student@S11-Server:~$ !46
Copyright © 2012, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 5: Using Commands Within the Default Shell


Chapter 5 - Page 8
YAT

history | wc -l
20
student@S11-Server:~$

Note: The command output may vary from system to system.


Unauthorized reproduction or distribution prohibitedฺ Copyright© 2013, Oracle and/or its affiliatesฺ

6. Use the –c option to clear previous history.


student@S11-Server:~$ history -c
7. The following are the various methods for repeating the previous command quickly:
• Use the up arrow to view the previous command and press Enter to execute it.
• Type !! and press Enter from the command line.
• Type !-1 and press Enter from the command line.

s a
) h a
l ฺ c om
m ai eฺ
@ g uid
a n 8 t G
zid tude n
e l
d is S
( a
i d an se th
a n Z to u
l r h m nse
b de e lice
e d A rabl
h am nsfe
M o -tra
l
de non
A

Copyright © 2012, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 5: Using Commands Within the Default Shell


Chapter 5 - Page 9
YAT

Practice 5-4: Using Redirecting Commands

Overview
In this practice, you will perform input, output, and standard error redirection by using the <, >,
and | (pipe) metacharacters.
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2013, Oracle and/or its affiliatesฺ

Assumption
It is recommended to use one system at a time. Students will perform the exercises in the
/home/student directory. For Oracle Linux, change the directory from Desktop to the
/home/student directory. The output may vary from system to system.

Tasks
1. Start a terminal session by right-clicking the desktop and selecting the Open Terminal
option.
2. Redirect the list of files and subdirectories of the home directory into the directory_list
file. s a
student@S11-Server:~$ pwd ) h a
l ฺ c om
ai eฺ
/home/student
student@S11-Server:~$ ls -l > directory_list m
g uid
student@S11-Server:~$ ls
n 8 @ G
a n t
zid tude
Desktop Documents file1 lab
practice1 e l
d is S
directory_list Downloads( a file2 perm
Public
i d an se th
n Z to u
student@S11-Server:~$ cat directory_list
a
total 31
l r h m nse
b de e lice
drwxr-xr-x 2 student staff 5 Apr 9 19:03 Desktop

e d A rabl
-rw-r--r-- 1 student staff 0 May 10 17:05

h am nsfe
directory_list

M o -tra drwxr-xr-x 6 student staff 6 Apr 9 19:03 Documents


l
de non drwxr-xr-x 2 student staff 2 Apr 9 19:03 Downloads
A drwxr-xr-x 2 student staff 2 Apr 9 19:03 Public
student@S11-Server:~$
3. Remove the directory directory_list by using rm command.
student@S11-Server:~$ rm directory_list
student@S11-Server:~$ ls -l
total 17
drwxr-xr-x 2 student staff 5 Apr 9 19:03 Desktop
drwxr-xr-x 6 student staff 6 Apr 9 19:03 Documents
drwxr-xr-x 2 student staff 2 Apr 9 19:03 Downloads
drwxr-xr-x 17 root root 39 May 2 13:51 lab
drwxr-xr-x 2 student staff 2 Apr 9 19:03 Public
student@S11-Server:~$

Copyright © 2012, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 5: Using Commands Within the Default Shell


Chapter 5 - Page 10
YAT

4. Redirect the standard error message to a newly created file called error.
student@S11-Server:~$ cd lab
student@S11-Server:~/lab$ touch error
student@S11-Server:~/lab$ ls /var /test 1> error 2>&1
student@S11-Server:~/lab$ cat error
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2013, Oracle and/or its affiliatesฺ

/test: No such file or directory


/var:
adm
apache2
<lot of output>.........
student@S11-Server:~/lab$
5. Remove the file error by using the rm command.
student@S11-Server:~/lab$ rm error
student@S11-Server:~/lab$ ls
s a
dir1 dir5 file.3 file4 myvars
) h a
dante dir2 Documents file1
l ฺ c om fruit practice
dante_1 dir3 file.1 file2
m ai eฺ fruit2 tutor.vi
Desktop dir4 file.2 file3
@ g uid greetings
a n 8 t G
student@S11-Server:~/lab$
zid using n
deredirection symbol |
l
6. View a list of all the subdirectories located in the /etcedirectory
d is S t u
(pipe). ( a h
i d a-Fn /etce |t grep "/"
Z o us
student@S11-Server:~/lab$ ls
acct/
a n t
h m s e
amd64/
d e lr icen
anthy/
A b l e l
apache2/
m ed ferab
o a ans
avahi/
h<lot
l M n - r output>
tof
Ade nstudent@S11-Server:~/lab$
o cd

Note: The command output may vary from system to system.

Copyright © 2012, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 5: Using Commands Within the Default Shell


Chapter 5 - Page 11
YAT

Practice 5-5: Customizing the User's Work Environment

Overview
In this practice, you will study the user’s profile file to understand the user’s work environment.
Note: In Oracle Solaris, .profile is the profile file. In Oracle Linux, .bash_profile is the
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2013, Oracle and/or its affiliatesฺ

profile file.

Assumption
It is recommended to use one system at a time. Students will perform the exercises in the
/home/student directory. For Oracle Linux, change the directory from Desktop to the
/home/student directory. The output may vary from system to system.

Tasks
1. Confirm that you are currently in the parent directory by using pwd command and then
open the .profile file.
s a
student@S11-Server:~$ pwd
) h a
/home/student
l ฺ c om
student@S11-Server:~$ cat .profile
m ai eฺ
#
@ g uid
n 8
# Simple profile places /usr/bin at front, followed by
a t G
/usr/sbin.
zid tude n
# e l
d is S
( a
i d an se th
# Use less(1) or more(1) as the default pager for the man(1)

n Z to u
command.
# a
m nse
l r h
de e lice
export PATH=/usr/bin:/usr/sbin
b
A abl ]; then
e d/usr/bin/less
er
if [ -f
a mexport s f
helif [ra-fn /usr/bin/more ]; then -ins"
PAGER="/usr/bin/less
o
M n-t
e l
Ad no export PAGER="/usr/bin/more -s"
fi

#
# Define default prompt to <username>@<hostname>:<path><"($|#)
">
# and print '#' for user "root" and '$' for normal users.
#
# Currently this is only done for bash/pfbash(1).
#

case ${SHELL} in
*bash)
typeset +x PS1="\u@\h:\w\\$ "
Copyright © 2012, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 5: Using Commands Within the Default Shell


Chapter 5 - Page 12
YAT

;;
esac

student@S11-Server:~$
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2013, Oracle and/or its affiliatesฺ

Note: Study the entries made to the file with the help of your instructor to understand
options available for customizing a user’s work environment. The file content may vary
from system to system.

s a
) h a
l ฺ c om
m ai eฺ
@ g uid
a n 8 t G
zid tude n
e l
d is S
( a
i d an se th
a n Z to u
l r h m nse
b de e lice
e d A rabl
h am nsfe
M o -tra
l
de non
A

Copyright © 2012, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 5: Using Commands Within the Default Shell


Chapter 5 - Page 13
YAT
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2013, Oracle and/or its affiliatesฺ

s a
) h a
l ฺ c om
m ai eฺ
@ g uid
a n 8 t G
zid tude n
e l
d is S
( a
i d an se th
a n Z to u
l r h m nse
b de e lice
e d A rabl
h am nsfe
M o -tra
l
de non
A
YAT
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2013, Oracle and/or its affiliatesฺ

s a
) h a
l ฺ c om
Practices for Lesson m aiUsing
6: eฺ
g i d
a n 8@ t Gu
Basic File Permissions
Chapter 6 elz
id uden
( a d is St
i d an se th
a n Z to u
l r h m nse
b de e lice
e d A rabl
h am nsfe
M o -tra
l
de non
A

Copyright © 2012, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 6: Using Basic File Permissions


Chapter 6 - Page 1
YAT

Practices for Lesson 6


Practices Overview
In these practices, you will perform a set of tasks described in this lesson. Here is the list of
those activities:
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2013, Oracle and/or its affiliatesฺ

• Changing file ownership


• Changing file permissions
• Using the symbolic mode to change permissions
• Using octal mode to change permissions
• Modifying default permissions
• Viewing the default umask
• Changing the umask setting
UNIX Essentials Activities Checklist
s a
) h a
om
Introduction
l ฺ c
Introduction to UNIX m ai eฺ
@ g uid
a n 8 t G
Working with Files and Directories
zid tude n
e l
d is S
( a
Using the vi Editor
i d an se th
a n Z to u
Using Commands withinhthe
r mDefault
n e
sShell
l
de e lic e
b
e
Using Basicd AFile Permissions
r a bl
h a m nsfe
M oPerforming
- tra Basic Process Control
l
de no n
A
Using Advanced Shell Functionalities in Shell Scripts

Archiving Files and Remote Transfer

Copyright © 2012, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 6: Using Basic File Permissions


Chapter 6 - Page 2
YAT

Practice 6-1: Changing File Ownership

Overview
In this practice, you will view and change file ownership.
Assumptions
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2013, Oracle and/or its affiliatesฺ

The following tasks are performed in the Oracle Solaris 11 VM (S11-Server). The tasks can also
be replicated in the Oracle Linux VM (OL6-Server) with little modifications, mentioned where
applicable. In a few instances, the output may vary on your system. It is recommended to use
one system at a time. Students will perform the exercises in the /home/student directory. For
Oracle Linux, change the directory from Desktop to the /home/student directory.
Tasks
1. To find the owner of the existing Documents directory within the lab directory, use the ls
–l command. Ensure that you are in the lab directory currently.
student@S11-Server:~$ pwd
s a
/home/student
) h a
om
student@S11-Server:~$ cd lab
l ฺ c
student@S11-Server:~/lab$ ls -l
m ai eฺ
total 110
-rwxr-xr-x 1 student root 1319 May 82@
g uid
13:51Gdante
-rwxr-xr-x 1 student root 368idMayan 2 13:51
e n t dante_1
d e lz2 May tu2d13:51 Desktop
drwxr-xr-x 2 student root
( a i s S
drwxr-xr-x 5 student root
d h
an se t 5 May 22 13:51
5 May dir1
drwxr-xr-x i
Z ou
4 student root 13:51 dir2
a n t
drwxr-xr-x
h m s e
3 student root 3 May 2 13:51 dir3

d e lr icen
drwxr-xr-x 2 student root 2 May 2 13:51 dir4
Ab able
drwxr-xr-x l
2 student root 2 May 2 13:51 dir5
d
e fer
drwxr-xr-x 2 root root 4 May 9 14:30 Documents
am
h rans
M o - t
-rwxr-xr-x 1 student root 0 May 2 13:51 file.1

del non
-rwxr-xr-x 1 student root 0 May 2 13:51 file.2
A -rwxr-xr-x 1 student root 0 May 2 13:51 file.3
-rwxr-xr-x 1 student root 1610 May 2 13:51 file1
-rwxr-xr-x 1 student root 105 May 2 13:51 file2
-rwxr-xr-x 1 student root 218 May 2 13:51 file3
-rwxr-xr-x 1 student root 137 May 2 13:51 file4
-rwxr-xr-x 1 student root 57 May 2 13:51 fruit
-rwxr-xr-x 1 student root 57 May 2 13:51 fruit2
-rwxr-xr-x 1 student root 59 May 2 13:51 greetings
-rwxr-xr-x 1 student root 67 May 2 13:51 myvars
drwxr-xr-x 2 student root 7 May 2 13:51 practice
-rwxr-xr-x 1 student root 28709 May 2 13:51 tutor.vi

Note: The command output may vary on your system.

Copyright © 2012, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 6: Using Basic File Permissions


Chapter 6 - Page 3
YAT

For additional details about the command output, refer to the table below.

Symbol Meaning Comments


d directory If begins with – it means a regular file
r read
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2013, Oracle and/or its affiliatesฺ

w write
x executable
ugo user, group, rwx (u) r-x (g) r-x (o) read, write and
and others executable permissions for
respective entity. – means not
permitted.)
2 number of
links
s a
root owner
) h a
root group
l ฺ c om
4 May Day, Month, Last modified date time information
m ai eฺ
9 Year and
@ g uid
14:30 Hours:Minute
a n 8 t G
s
zid tude n
e l
d is S
Docum directory/file
( a
ents name
i d an se th
2. Identify the owner of the content inntheZDocuments u directory.
a t o
r
student@S11-Server:~/lab$
l h m nslse -l Documents
total 4
b de e lice
-rwxrwxr-x
e d A 1rarootbl root 24 May 9 14:30 misc.txt

h a m nsfe1 root
-rwxrwxr-x root 27 May 9 14:29 sample.txt
M o that
Observe - traroot is the owner of the content in the Documents directory.
l n
de3. To nchange
o the ownership of the Documents directory, switch to the root user and run the
A chown command.
student@S11-Server:~/lab$ su
Password:
root@S11-Server:~/lab# chown student Documents
root@S11-Server:~/lab# ls -l
total 110
-rwxr-xr-x 1 student root 1319 May 2 13:51 dante
-rwxr-xr-x 1 student root 368 May 2 13:51 dante_1
drwxr-xr-x 2 student root 2 May 2 13:51 Desktop
drwxr-xr-x 5 student root 5 May 2 13:51 dir1
drwxr-xr-x 4 student root 5 May 2 13:51 dir2
drwxr-xr-x 3 student root 3 May 2 13:51 dir3
drwxr-xr-x 2 student root 2 May 2 13:51 dir4
Copyright © 2012, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 6: Using Basic File Permissions


Chapter 6 - Page 4
YAT

drwxr-xr-x 2 student root 2 May 2 13:51 dir5


drwxr-xr-x 2 student root 4 May 9 14:30 Documents
-rwxr-xr-x 1 student root 0 May 2 13:51 file.1
-rwxr-xr-x 1 student root 0 May 2 13:51 file.2
-rwxr-xr-x 1 student root 0 May 2 13:51 file.3
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2013, Oracle and/or its affiliatesฺ

-rwxr-xr-x 1 student root 1610 May 2 13:51 file1


-rwxr-xr-x 1 student root 105 May 2 13:51 file2
-rwxr-xr-x 1 student root 218 May 2 13:51 file3
-rwxr-xr-x 1 student root 137 May 2 13:51 file4
-rwxr-xr-x 1 student root 57 May 2 13:51 fruit
-rwxr-xr-x 1 student root 57 May 2 13:51 fruit2
-rwxr-xr-x 1 student root 59 May 2 13:51 greetings
-rwxr-xr-x 1 student root 67 May 2 13:51 myvars
drwxr-xr-x 2 student root 7 May 2 13:51 practice
s a
-rwxr-xr-x 1 student root 28709 May 2 13:51 tutor.vi
) h a
l ฺ c om
ai eฺ
Note: The password for root is oracle1.Output may vary from system to system.
m
g uofidits
4. Confirm the ownership of the Documents directory and change the ownership
contents to student, and then run the chown command again.n8@ G
a n t
root@S11-Server:~/lab# ls -l Documentslzid de
e
d is S t u
total 4
( a h May 9 14:30 misc.txt
root an t24
-rwxrwxr-x 1 root
i d e
Z o us 27 May 9 14:29 sample.txt
-rwxrwxr-x 1 root
n root
t
a chowne student
root@S11-Server:~/lab#
l r h m n s Documents/*
e
de e lic ls -l Documents
root@S11-Server:~/lab#
total 4 A b
e d r a bl
a m
-rwxrwxr-x
s f e1 student root 24 May 9 14:30 misc.txt

M oh-rwxrwxr-x
- t r an 1 student root 27 May 9 14:29 sample.txt

Adel nroot@S11-Server:~/lab#
on
exit
exit

student@S11-Server:~/lab$

Note: The output may vary from system to system.

Copyright © 2012, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 6: Using Basic File Permissions


Chapter 6 - Page 5
YAT

Practice 6-2: Changing File Permissions

Overview
In this practice, you will view and change permissions on files.
Assumptions
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2013, Oracle and/or its affiliatesฺ

The following tasks are performed in the Oracle Solaris 11 VM (S11-Server). The tasks can also
be replicated in the Oracle Linux VM (OL6-Server) with little modifications, mentioned where
applicable. In a few instances, the output may vary on your system. It is recommended to use
one system at a time. Students will perform the exercises in the /home/student directory. For
Oracle Linux, change the directory from Desktop to the /home/student directory.
Preparation
Ensure that the umask value is set to 0022 on your system. To verify, run the umask command.
student@S11-Server:~/lab$ umask
0022
s a
If not, set the umask value to 0022 by running the following command:
) h a
$ umask 0022
l ฺ c om
m ai eฺ
Tasks
@ g uid
1. Perform the following commands in the lab directory of your home8directory: G
a n n t
student@S11-Server:~/lab$ mkdir perm zid de
student@S11-Server:~/lab$ cd /etc ad e l t u
( i s S
student@S11-Server:/etc$ ls -l
d angroup e h
t shadow vfstab
motd
i s
Z o u 408 Apr 9 20:12 group
-rw-r--r-- 1 root
n sys
1 rootma syse t
-rw-r--r--
r h
l iceroot
1eroot n s 49 Oct 21 2011 motd
-r--------
b d l 661 Apr 9 19:38 shadow
-rw-r--r--
d A 1aroot
b le sys 438 May 2 13:47 vfstab
m e fer
student@S11-Server:/etc$ cp group motd shadow vfstab ~/lab/perm
a
ohcp: -cannot s
an open shadow: Permission denied
M t r
Adel Note: n Linux, use the file /etc/fstab instead of /etc/vfstab.
noFor
a. When trying to copy the shadow file, the error message cp: cannot open shadow:
Permission denied appears. Why?
Only the owner of this file, in this case, root, has read permission.
student@S11-Server:/etc$ ls -l ~/lab/perm
total 6
-rw-r--r-- 1 student staff 408 May 9 15:11 group
-rw-r--r-- 1 student staff 49 May 9 15:11 motd
-rw-r--r-- 1 student staff 438 May 9 15:11 vfstab
student@S11-Server:/etc$ cd
student@S11-Server:~$ cd lab
student@S11-Server:~/lab$ cp -r /etc/skel perm
student@S11-Server:~/lab$

Copyright © 2012, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 6: Using Basic File Permissions


Chapter 6 - Page 6
YAT

b. Change to the perm directory and list the contents of the directory.
student@S11-Server:~/lab$ cd perm
student@S11-Server:~/lab/perm$ ls -l
total 9
-rw-r--r-- 1 student staff 408 May 9 15:11 group
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2013, Oracle and/or its affiliatesฺ

-rw-r--r-- 1 student staff 49 May 9 15:11 motd


drwxr-xr-x 2 student staff 7 May 9 15:20 skel
-rw-r--r-- 1 student staff 438 May 9 15:11 vfstab
student@S11-Server:~/lab/perm$ cd
student@S11-Server:~$
c. In the following table, fill in the permission sets for each file and write the three-digit octal
number that represents the combined set of permissions.
File or Permissions Octal
Directory
Owner Group Other
Value
s a
group rw- r-- r-- 644 ) h a
l ฺ c om
motd rw- r-- r--
m ai eฺ 644
skel rw- r-x r-x 755
@ g uid
a n 8 t G
vfstab rw- r-- r-- 644
zid tude n
2. Create a new file test1 and a new directory test.del
student@S11-Server:~$ cd lab n
(a this S
student@S11-Server:~/lab$Ztouch ida test1
u se
n
a mkdir to test
student@S11-Server:~/lab$
h m s e
a. What are the defaulte
d lr iceofnthe new file?
permissions
A b l e l
e d r a b
student@S11-Server:~/lab$ ls –l test1

a m nsfe
rw-r--r—
h
b. o a default permissions of the new directory?
trthe
l M What are
n -
Ade nstudent@S11-Server:~/lab$
o ls –ld test
drwxr-xr-x
3. Describe the difference between the symbolic mode and the octal mode.
The symbolic mode uses a combination of letters and symbols to add or remove
permissions for each type of user.
The octal mode uses octal numbers to represent permissions. The octal mode is also
referred to as the absolute mode.
4. Using the symbolic mode, add write permission for the group to the motd file.
student@S11-Server:~/lab$ cd perm
student@S11-Server:~/lab/perm$
student@S11-Server:~/lab/perm$ chmod g+w motd
student@S11-Server:~/lab/perm$ ls -l
total 9

Copyright © 2012, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 6: Using Basic File Permissions


Chapter 6 - Page 7
YAT

-rw-r--r-- 1 student staff 408 May 9 15:11 group


-rw-rw-r-- 1 student staff 49 May 9 15:11 motd
drwxr-xr-x 2 student staff 7 May 9 15:20 skel
-rw-r--r-- 1 student staff 438 May 9 15:11 vfstab
student@S11-Server:~/lab/perm$
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2013, Oracle and/or its affiliatesฺ

5. Using the octal mode, change the permissions on the motd file to -rwxrw----.
student@S11-Server:~/lab/perm$ chmod 760 motd
student@S11-Server:~/lab/perm$ ls -l
total 9
-rw-r--r-- 1 student staff 408 May 9 15:11 group
-rwxrw---- 1 student staff 49 May 9 15:11 motd
drwxr-xr-x 2 student staff 7 May 9 15:20 skel
-rw-r--r-- 1 student staff 438 May 9 15:11 vfstab
student@S11-Server:~/lab/perm$
s a
) h a
om
6. Using the octal mode, add write permission for other on the file named group.
l ฺ c
student@S11-Server:~/lab/perm$ chmod 646 group
m ai eฺ
student@S11-Server:~/lab/perm$ ls -ldi
@ g uid
397 drwxr-xr-x 3 student staff
a n 8 G
6 May 9 15:20 .
t
student@S11-Server:~/lab/perm$
zid tude n
e l
d isdidSyou use?
a
7. Identify the GID and UID for the motd file. Which(command
student@S11-Server:~/lab/perm$
i d th
anls s-ne motd
1 60004 n 10 Z o u 49 May 9 15:11 motd
-rwxrw----
m a e t
h
lr icen
student@S11-Server:~/lab/perm$ s
d e
b memo l your dir4 directory.
8. Create a new file called
A l e in

m ed ferab
student@S11-Server:~/lab/perm$ touch ~/lab/dir4/memo

o
9. Remove
a
h therreada nspermission for the owner from the memo file in the dir4 directory. Use
M the t
-symbolic mode or the octal mode.
Adel either
non student@S11-Server:~/lab/perm$ chmod u-r ~/lab/dir4/memo
or
student@S11-Server:~/lab/perm$ chmod 244 ~/lab/dir4/memo
a. What happens when you try to use the cat command to view the memo file?
You cannot use the cat command, because read permission has been removed from
the user. Even though you are part of the group, the permissions are viewed in the order
in which they appear. The following message appears when you use the cat command:
cat: cannot open /home/student/lab/dir4/memo: Permission denied.

Copyright © 2012, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 6: Using Basic File Permissions


Chapter 6 - Page 8
YAT

b. What happens when you try to copy the memo file?


You cannot copy the file, because the user has no read permission. The following
message appears:
cp: cannot open /home/student/lab/dir4/memo: permission denied
student@S11-Server:~/lab/perm$ cd
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2013, Oracle and/or its affiliatesฺ

student@S11-Server:~$ cp ~/lab/dir4/memo ~/lab


cp: cannot open /home/student/lab/dir4/memo: permission denied
student@S11-Server:~$

s a
) h a
l ฺ c om
m ai eฺ
@ g uid
a n 8 t G
zid tude n
e l
d is S
( a
i d an se th
a n Z to u
l r h m nse
b de e lice
e d A rabl
h am nsfe
M o -tra
l
de non
A

Copyright © 2012, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 6: Using Basic File Permissions


Chapter 6 - Page 9
YAT

Practice 6-3: Modifying Default Permissions

Overview
In this practice, you modify the default permissions of files and directories.

Assumptions
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2013, Oracle and/or its affiliatesฺ

The following tasks are performed in the Oracle Solaris 11 VM (S11-Server). The tasks can also
be replicated in the Oracle Linux VM (OL6-Server) with little modifications, mentioned where
applicable. In a few instances, the output may vary on your system. It is recommended to use
one system at a time. Students will perform the exercises in the /home/student directory. For
Oracle Linux, change the directory from Desktop to the /home/student directory.

Tasks
1. What is the function of the umask utility? What is the default umask that exists on your
system?
s a
The umask utility modifies the default permissions set for files and directories at the time of
creation. To view the default umask value on your system, run the umask command. ) h a
student@S11-Server:~$ umask
l ฺ c om
0022 m ai eฺ
@ g uid
2. Change the umask to 027. Which command did you run?
a n 8 t G
student@S11-Server:~$ umask 027
zid tude n
e l
d Record
( a
3. Create a new file and a new directory in the lab directory.
i s S the access permissions.
n h
ida use t
Which command did you run?
student@S11-Server:~$ cd Z lab
student@S11-Server:~/lab$ m an touch e to testfile
h s
lr icen mkdir testdir
b d e
student@S11-Server:~/lab$
l
A l
student@S11-Server:~/lab$
d b e ls -l testfile

m
-rw-r----- a
e fe1r student staff 0 May 9 15:41 testfile
a s
ohstudent@S11-Server:~/lab$
an 2 student staff ls -ld testdir
M - t r
del non
drwxr-x--- 2 May 9 15:41 testdir
A 4. Change the umask back to 0022.
student@S11-Server:~/lab$ umask 0022
5. Create a new file and a new directory.
student@S11-Server:~/lab$ touch test2file
student@S11-Server:~/lab$ mkdir test2dir
6. Record the access permissions.
student@S11-Server:~/lab$ ls -l test2file
-rw-r--r-- 1 student staff 0 May 9 15:41 test2file
student@S11-Server:~/lab$ ls -ld test2dir
drwxr-xr-x 2 student staff 2 May 9 15:41 test2dir
student@S11-Server:~/lab$ cd
student@S11-Server:~$

Copyright © 2012, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 6: Using Basic File Permissions


Chapter 6 - Page 10
YAT
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2013, Oracle and/or its affiliatesฺ

s a
) h a
l ฺ c om
Practices for Lesson m ai eฺ
7:
@ g uid
Performing Basic
a n 8 G
Process
t
Controlelzid tude
n
( a d is S
i d an se th
Chapter 7

a n Z to u
l r h m nse
b de e lice
e d A rabl
h am nsfe
M o -tra
l
de non
A

Copyright © 2012, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 7: Performing Basic Process Control


Chapter 7 - Page 1
YAT

Practices for Lesson 7


Practices Overview
In this practice, you will use the commands described in this lesson to determine PIDs, view a
process tree, and kill processes.
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2013, Oracle and/or its affiliatesฺ

This practice introduces the tty command, which displays the name of the current terminal
window. The name displayed by the tty command includes a unique identification number
assigned by the Solaris OS to each open terminal window (for example, /dev/pts/2). In the
tasks illustrating the tty command, the unique identification number is displayed as
/dev/pts/n, where n is a numeral.
Here is the list of the tasks that you will perform in this practice:
• List system processes.
• Control system processes.
• Terminate a process.
s a
UNIX Essentials Activities Checklist
) h a
l ฺ c om
Introduction
m ai eฺ
@ g uid
Introduction to UNIX
a n 8 t G
zid tude n
e l
d is S
Working with Directories and Files
( a
n e th
i d a
Using the vi Editor
n Z o us
m a e t
h s
lr the iDefault
en Shell
e
Using Commands within
d l c
d Ab able
m e FilefePermissions
Using Basic r
a
oh -tran s
M
del non
Performing Basic Process Control
A
Using Advanced Shell Functionality and Shell Scripts

Archiving Files and Remote Transfer

Copyright © 2012, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 7: Performing Basic Process Control


Chapter 7 - Page 2
YAT

Practice 7-1: Controlling System Processes

Overview
In this practice, you will determine the PIDs, view a process tree, and kill processes.

Assumptions
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2013, Oracle and/or its affiliatesฺ

Oracle Solaris VM (S11-Server) is currently running. The tasks mentioned below are performed
in Solaris 11 environment (S11-Server) VM. They can also be practiced as it is with Oracle
Linux VM ( OL6-Server) with little modifications (mentioned where applicable). In few cases, the
output on your system may vary. It is recommended to use one system at a time. Students will
perform the exercises in the /home/student directory. For Oracle Linux, change the directory
from Desktop to the /home/student directory.

Tasks
Perform the following steps:
1. Use the following ps commands to list the processes currently running on your system. s a
What information does each command provide? ) h a
student@S11-Server:~$ ps
l ฺ c om
PID TTY TIME CMD
m ai eฺ
3162 pts/1 0:00 bash @ g uid
a n 8 t G
3163 pts/1 0:00 ps
id ude
zterminal. n
e l
d is St
This command prints information for the current user and
( a
student@S11-Server:~$ ps -f
an TTY
C idSTIME s e th
UID PID PPID
n Z u TIME CMD
student 3162 3159 a 0 12:11:03
m e to pts/1 0:00 bash
student 3164 l3162 h s
r ce0n12:11:18 pts/1 0:00 ps -f
d e l i
Ab a afullblisting
This command prints
d le of the ps command.
m e fer
student@S11-Server:~$ ps -e
a
oh PID-trTTY
an s TIME CMD
M
Adel non 0 ? 5 ?
0:04 sched
0:11 zpool-rp
6 ? 0:01 kmem_tas
1 ? 0:00 init
2 ? 0:00 pageout
3 ? 1:05 fsflush
7 ? 0:00 intrd
8 ? 0:00 vmtasks
116 ? 0:00 pfexecd
11 ? 0:05 svc.star
13 ? 1:47 svc.conf
<lot of output> ……

Copyright © 2012, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 7: Performing Basic Process Control


Chapter 7 - Page 3
YAT

This command prints information about every process running.


(In Terminal window t1)
student@S11-Server:~$ ps -ef
UID PID PPID C STIME TTY TIME CMD
root 0 0 0 May 07 ? 0:04 sched
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2013, Oracle and/or its affiliatesฺ

root 5 0 0 May 07 ? 0:11 zpool-rpool


root 6 0 0 May 07 ? 0:01 kmem_task
root 1 0 0 May 07 ? 0:00
/usr/sbin/init
root 2 0 0 May 07 ? 0:00 pageout
root 3 0 0 May 07 ? 1:05 fsflush
root 7 0 0 May 07 ? 0:00 intrd
root 8 0 0 May 07 ? 0:00 vmtasks
root 116 1 0 May 07 ? 0:00
/usr/lib/pfexecd s a
root 11 1 0 May 07 ? 0:05 ) h a
/lib/svc/bin/svc.startd
l ฺ c om
root 13 1 0 May 07 ? 1:47
m ai eฺ
/lib/svc/bin/svc.configd
@ g uid
root 105 1 0 May 07 ?
a n 80:03 t G
/lib/inet/in.mpathd
zid tu0:02
de n
e l
d is S
netcfg 39 1 0
a
May 07 ?
(
/lib/inet/netcfgd
i d an se th
n Z to u
<lot of output> ……
a
student 3159 lrh1
e n se
m 1 12:11:03
d e l i c
? 0:00 gnome-terminal

Ab ab2957
student 3079
d le 0 18:25:26 ? 0:00 python2.6
e fer
/usr/lib/system-config-printer/applet.py
m
o a
h root
student
a n s3166 3162 0 12:11:26 pts/1 0:00 ps -ef

e l M n-tr 3088 3087 0 18:25:30 ? 0:00 hald-runner


Ad o
nstudent@S11-Server:~$ exit
Note: The command output on your system may vary
2. Open another terminal window, t2, and execute the ps -ef command in a terminal
window. Identify the process ID related to the ps -ef command.
(In Terminal window t2)
student@S11-Server:~$ ps -ef
UID PID PPID C STIME TTY TIME CMD
root 0 0 0 May 07 ? 0:04 sched
root 5 0 0 May 07 ? 0:11 zpool-rpool
root 6 0 0 May 07 ? 0:01 kmem_task
root 1 0 0 May 07 ? 0:00
/usr/sbin/init

Copyright © 2012, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 7: Performing Basic Process Control


Chapter 7 - Page 4
YAT

root 2 0 0 May 07 ? 0:00 pageout


root 3 0 0 May 07 ? 1:05 fsflush
root 7 0 0 May 07 ? 0:00 intrd
root 8 0 0 May 07 ? 0:00 vmtasks
root 116 1 0 May 07 ? 0:00
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2013, Oracle and/or its affiliatesฺ

/usr/lib/pfexecd
root 11 1 0 May 07 ? 0:05
<lot of output> ………
…………………………
………………………..
student 3159 1 1 12:11:03 ? 0:02 gnome-
terminal
student 3079 2957 0 18:25:26 ? 0:00 python2.6
/usr/lib/system-config-printer/applet.py
student 3172 3171 0 12:13:43 pts/2 0:00 ps -ef
s a
root 3088 3087 0 18:25:30 ? 0:00 hald-runner ) h a
Close the terminal windows by executing exit command.
l ฺ c om
student@S11-Server:~$ exit
m ai eฺ
@ g uid
a n 8 t G
Note: The PID differs from system to system.
zid command:
de n
3. Open a terminal window, referred as t1, and enter thee l
d is S
following t u
( a
(In terminal window t1,)
i d an se th
n Z to u
student@S11-Server:~$ gcalctool
a
m nse
l r h
b de e lice
e d A rabl
h a m nsfe
M o -tra
l
de non
A

Note: This command launches the calculator, which is terminated by using the kill
command from another terminal window in a subsequent task.

Copyright © 2012, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 7: Performing Basic Process Control


Chapter 7 - Page 5
YAT

4. Open another terminal window, called t2. Use the ps or pgrep command to identify the
PID of the gcalctool command.
student@S11-Server:~$ ps -ef | grep gcalctool
student 3185 3184 1 12:39:50 pts/1 0:00 gcalctool
student@S11-Server:~$ pgrep gcalctool
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2013, Oracle and/or its affiliatesฺ

3185
student@S11-Server:~$
5. In terminal window t2, terminate the gcalctool command by using the PID.
student@S11-Server:~$ kill PID

Note: 3185 is the PID value of the gcalctool.


6. In terminal window 2, enter the tty command to identify the name of this terminal window.
The name appears as /dev/pts/n, where n is a numeral (for example, /dev/pts/4).
student@S11-Server:~$ tty s a
/dev/pts/2 ) h a
This name differs from system to system.
l ฺ c om
7. m ai eฺ
Move back to terminal window 1. Use the pgrep command to find the PID associated with
the name of terminal window 2. @ g uid
a n 8 t G
student@S11-Server:~$ pgrep -t pts/2
zid tude n
3189 e l
d is S
( a
Note: The PID differs from system to i d an se th
Z ou
ncommand
system.
8. In terminal window 1, use the kill
m a e t to terminate terminal window 2.
h
lr ickill s
en 3189
student@S11-Server:~$
d e l
Did it work?
d Ab able
No. me f er
a s
ohthe kill-tcommand
an with the -9 option to terminate terminal window 2.
9. Use
M r
Adel nstudent@S11-Server:~$
on kill -9 3189
Did it work?
Yes.
10. Name the commands used to search for a specific process.
The pgrep command and the ps command with the grep command
11. Run the following kill commands to identify the signal names associated with signal
numbers.
student@S11-Server:~$ kill -l 9
KILL
The signal name is KILL.
student@S11-Server:~$ kill -l 15
TERM
The signal name is TERM.

Copyright © 2012, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 7: Performing Basic Process Control


Chapter 7 - Page 6
YAT

12. What is the difference between a process and a job?


Every program that runs in UNIX creates a process. A process that a shell can manage is
called a job.
13. What is the difference between the pkill command and the kill command?
You can terminate a job using the kill command. You can terminate a specific process
using the pkill command.
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2013, Oracle and/or its affiliatesฺ

14. In a terminal window, enter the command sleep 500 &.


student@S11-Server:~$ sleep 500 &
[1] 3206
student@S11-Server:~$
15. In the same terminal window, use the ps command to identify the shell process running in
that window.
student@S11-Server:~$ ps
PID TTY TIME CMD
s a
3184 pts/1 0:00 bash
) h a
3207 pts/1 0:00 ps
l ฺ c om
3206 pts/1 0:00 sleep
m ai eฺ
16. Open another terminal window, t2 and use the ptree command using the
@ g shelluPID
id from
Step 14 as the argument. 8
an ent G
i d
student@S11-Server:~$ ptree 3184
d e lz
S t ud
3181 gnome-terminal
n (a this
3184 ksh
Z ida use
an e to
3206 sleep 500

l m
rh command
n s
Note: ptree equivalent e
d e lic e in Oracle Linux is pstree.The output may vary
between systems. b
A abl
e d
m
17. In this terminal
a f er use the kill command with the PID as the argument that was
window,
s
M ohin Step
used
- t r a15.nDoes the window close?
Adel Yes non student@S11-Server:~$ kill -9 3184

Copyright © 2012, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 7: Performing Basic Process Control


Chapter 7 - Page 7
YAT
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2013, Oracle and/or its affiliatesฺ

s a
) h a
l ฺ c om
m ai eฺ
@ g uid
a n 8 t G
zid tude n
e l
d is S
( a
i d an se th
a n Z to u
l r h m nse
b de e lice
e d A rabl
h am nsfe
M o -tra
l
de non
A
YAT
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2013, Oracle and/or its affiliatesฺ

s a
) h a
l ฺ c om
Practices for Lesson m aiUsing
8: eฺ
g i
u in d
Advanced Shell
a n 8@ G
Features
t
Shell Scripts
l z id uden
( a de is St
i d an s8 e th
Chapter

a n Z to u
l r h m nse
b de e lice
e d A rabl
h am nsfe
M o -tra
l
de non
A

Copyright © 2012, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 8: Using Advanced Shell Features in Shell Scripts


Chapter 8 - Page 1
YAT

Practices for Lesson 8


Practices Overview
In this practice, you will perform a range of tasks described in this lesson. Here is the list of
those activities:
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2013, Oracle and/or its affiliatesฺ

• Manage jobs in the Bash shell


• Create an alias
• Use Bash shell functions
• Set Bash shell options
• Create and run shell scripts
• Pass values to a shell script
• Use the test command
• Execute conditional commands

s a
UNIX and Linux Essentials - Activities Checklist
) h a
Introduction l ฺ c om
m ai eฺ
@ g uid
Introduction to UNIX
a n 8 t G
zid tude n
e l
d is S
Working with Files and Directories
( a
i d an se th
Using the vi Editor a n Z to u
l r h m nse
b d e lice
Using Commands
d A within
a b lethe Default Shell
m e fer
o a
h ranFile s
l M n - t
Using Basic Permissions

Ade no
Performing Basic Process Control

Using Advanced Shell Functionalities in Shell Scripts

Archiving Files and Remote Transfer

Copyright © 2012, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 8: Using Advanced Shell Features in Shell Scripts


Chapter 8 - Page 2
YAT

Practice 8-1: Using Advanced Bash Shell Functionality

Overview
In this practice, you will perform some tasks using the job control commands described in this
lesson.
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2013, Oracle and/or its affiliatesฺ

Assumptions
It is recommended to use one system at a time. Students will perform the exercises in the
/home/student directory. For Oracle Linux, change the directory from Desktop to the
/home/student directory. The output may vary from system to system. Bash is the default
shell in both Oracle Solaris 11 and Oracle Linux. To confirm, run the following commands at the
respective prompt:
Oracle Solaris 11 VM (S11-VM)
student@S11-Server:~$ echo $SHELL
/usr/bin/bash
s a
student@S11-Server:~$
) h a
Oracle Linux VM (OL6-VM)
l ฺ c om
[student@OL6-Server ~]$ echo $SHELL
m ai eฺ
/bin/bash
@ g uid
[student@OL6-Server ~]$
a n 8 t G
zidVM. The n
e tasks can be
dsame
Note: The following tasks are performed in the S11-Server
e l
d ithe t u
a
replicated on the OL6-Server VM. Only in some instances,
( s Soutput may vary slightly.
d an se t h
Tasks i
Z ou
Write the commands to perform eachaof nthe t tasks in the space provided.
following
h m s e
1. Run the following command
d e lr in theicbackground:
en
l
Ab able sleep 500 &
student@S11-Server:~$
d
e fer
[1] 3244
a m
h by using nsthe jobs command that the sleep command executed in the previous
o
2. Confirm
t r a
Mstep is currently
- running.
Adel non student@S11-Server:~$ jobs
[1]+ Running sleep 500 &
3. Bring the job to the foreground, and then put it back in the background. To stop a command
and get back to the prompt, use CTRL+Z.
student@S11-Server:~$ fg %1
sleep 500
^Z
[1]+ Stopped sleep 500
student@S11-Server:~$ bg %1
[1]+ sleep 500 &
student@S11-Server:~$

Copyright © 2012, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 8: Using Advanced Shell Features in Shell Scripts


Chapter 8 - Page 3
YAT

4. What are job control commands? Name any two commands and mention their function.
Job control commands enable you to place jobs in the foreground or background, and to
start or stop jobs. Two of the job control commands are the jobs command and the bg %n
command.
• The jobs command lists all jobs that are currently running or are stopped in the
background.
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2013, Oracle and/or its affiliatesฺ

• The bg command runs the current or specified job in the background (n is the job ID).
5. Terminate a job by running the kill command. To reconfirm, run the jobs command at
the prompt.
student@S11-Server:~$ kill %1
student@S11-Server:~$ jobs
[1]+ Terminated sleep 500
6. Enable the noclobber option, and use the set command to verify that this is enabled.
student@S11-Server:~$ set -o noclobber
s a
student@S11-Server:~$ set -o | more
) h a
allexport off
l ฺ c om
braceexpand on
m ai eฺ
emacs on
@ g uid
errexit off
a n 8 t G
errtrace off
zid tude n
functrace off e l
d is S
( a
hashall on
i d an se th
n Z to u
histexpand on
history a
m nse
on
l r h
b de e lice
ignoreeof off

d A rabl
interactive-comments on
e
m nsfe
keyword off
h a
o -tra monitor on
l M
de non
noclobber on
A noexec off
noglob off
nolog off
notify off
nounset off
onecmd off
physical off
pipefail off
--More--
[1]+ Stopped
<lot of output>……… ^[Z] or q

Note: To stop the command output, use CTRL + Z or q.

Copyright © 2012, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 8: Using Advanced Shell Features in Shell Scripts


Chapter 8 - Page 4
YAT

7. Display all predefined aliases.


student@S11-Server:~$ alias

Note: If you do not see an alias list, it implies that there is no predefined alias on the
system.
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2013, Oracle and/or its affiliatesฺ

8. Create an alias named cls that clears the terminal screen.


student@S11-Server:~$ alias cls=clear
9. Create an alias named dir that displays a long listing of all the files and directories in the
current directory.
student@S11-Server:~$ alias dir='ls -l'
10. Create an alias named h that lists your command history.
student@S11-Server:~$ alias h=history
11. Unalias the history command and the clear command.
s a
student@S11-Server:~$ unalias h
) h a
om
student@S11-Server:~$ unalias cls
l ฺ c
12. Display all defined functions.
m ai eฺ
student@S11-Server:~$ typeset –f
@ g uid
a n 8 t G
ziddata.tude n
e l
Note: In Oracle Linux, the output may display a lot more
dthe following
(
13. Create and test a function called data that performsa i s S activities:
a. Clears the terminal screen
d an se t h
i
b. Displays date and time
a n Z to u
c. Displays users logged
l r hin
e se
mto thensystem
d. Displays the path
b deof thee current
l ic working directory
e d A working
e. Lists current
r a bldirectory in a long format
a m nsfe
student@S11-Server:~$
h function data { clear; date; who; pwd; ls
M o -tra
-l; }
l on that the function is created, run typeset –f again.
de14. To nconfirm
A student@S11-Server:~$ typeset -f
data ()
{
clear;
date;
who;
pwd;
ls -l
}
Note: In Oracle Linux (OL6-Server) VM, the output may vary slightly.

Copyright © 2012, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 8: Using Advanced Shell Features in Shell Scripts


Chapter 8 - Page 5
YAT

15. Use vi to edit the .profile file in your home directory:


student@S11-Server:~$ vi ~/.profile

Note: The equivalent profile file in Oracle Linux is .bash_profile.


Add the following line entries in the .profile file:
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2013, Oracle and/or its affiliatesฺ

set -o vi
alias h=’history’
alias cls=’clear’
alias lf=’pwd ; ls -lF’
#
# Simple profile places /usr/bin at front, followed by
/usr/sbin.
#
# Use less(1) or more(1) as the default pager for the man(1)
command. s a
# ) h a
export PATH=/usr/bin:/usr/sbin
l ฺ c om
m ai eฺ
if [ -f /usr/bin/less ]; then
@ g uid
a n 8 t G
export PAGER="/usr/bin/less -ins"
zid tude n
elif [ -f /usr/bin/more ]; then
e l
d is S
export PAGER="/usr/bin/more -s"
( a
fi
i d an se th
a n Z to u
#
l r h m nse
# Define default
b de e liceto <username>@<hostname>:<path><"($|#)
prompt

d A '#'
">
# and eprint a l user "root" and '$' for normal users.
bfor
# am fe r
h n
o # Currently s
l M n - tra this is only done for bash/pfbash(1).
Ade n#o
ENV=$HOME/.bash
export ENV
case ${SHELL} in
*bash)
typeset +x PS1="\u@\h:\w\\ $ "
;;
esac
set -o vi
alias h='history'
alias cls='clear'
alias If='pwd; ls -lF'

Note: The file content may vary from system to system.


Copyright © 2012, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 8: Using Advanced Shell Features in Shell Scripts


Chapter 8 - Page 6
YAT

16. Run the following commands:


In Oracle Solaris VM
student@S11-Server:~$ . ~/.profile
In Oracle Linux VM
[student@OL6-Server]:~$ . ~/.bash_profile
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2013, Oracle and/or its affiliatesฺ

17. Test your new aliases and functions.


student@S11-Server:~$ h
1 which bash
2 clear
3 pwd
4 clear
5 sleep 500 &
6 jobs
7 fg %1 s a
8 bg %1 ) h a
9 gedit /opt/ora/4.doc
l ฺ c om
10 kill %1
m ai eฺ
11 set -o noclobber @ g uid
a n 8 t G
12 set -0 | more
zid tude n
13 set -o | more e l
d is S
(
14 gedit /opt/ora/1.doc a
15 alias i d an se th
a n Z to u
16 alias cls=clear

l r h m nse
17 alias dir='ls -l'
b de e lice
18 alias h=history

e d A rabl
19 unalias h

h am nsfe20 unalias cls


M o -tra 21 typeset -f
l
de non 22 function data {clear; date; who; pwd; ls -l;}
A 23 function data { clear; date; who; pwd; ls -l; }
24 vi ~/.kshrc
25 gedit /opt/ora/2.doc
26 typeset -f
27 gedit /opt/ora/3.doc
28 vi ~/.profile
29 . ~/.profile
30 h
student@S11-Server:~$ cls

student@S11-Server:~$ lf
/home/student
total 33
Copyright © 2012, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 8: Using Advanced Shell Features in Shell Scripts


Chapter 8 - Page 7
YAT

drwxr-xr-x 2 student staff 5 Apr 9 19:03 Desktop/


-rw-r--r-- 1 student staff 630 May 10 17:05
directory_list
drwxr-xr-x 6 student staff 6 Apr 9 19:03 Documents/
drwxr-xr-x 2 student staff 2 Apr 9 19:03 Downloads/
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2013, Oracle and/or its affiliatesฺ

drwxr-xr-x 20 student root 47 May 10 17:11 lab/


drwxr-xr-x 2 student staff 2 Apr 9 19:03 Public/
student@S11-Server:~$ exit

Note: This will close the terminal. The command output may vary for systems and
environments. In some cases, first use of exit command may throw a message
“There are stopped jobs”. Please ignore. Run the exit command again.

s a
) h a
l ฺ c om
m ai eฺ
@ g uid
a n 8 t G
zid tude n
e l
d is S
( a
i d an se th
a n Z to u
l r h m nse
b de e lice
e d A rabl
h am nsfe
M o -tra
l
de non
A

Copyright © 2012, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 8: Using Advanced Shell Features in Shell Scripts


Chapter 8 - Page 8
YAT

Practice 8-2: Using Shell Scripts

Overview
In this practice, you will create, edit, and run shell scripts using some of the test and conditional
statements.
Assumptions
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2013, Oracle and/or its affiliatesฺ

It is recommended to use one system at a time. Students will perform the exercises in the
/home/student directory. For Oracle Linux, change the directory from Desktop to the
/home/student directory. The output may vary from system to system.

Tasks
1. In this task, you will create and run a simple shell script called info.sh, which displays
date, time, username, and current directory.
a. Switch to the lab directory in your home directory. Open the vi editor and create a shell
script file, info.sh as shown. Ensure that you are in the lab directory of your home
s a
directory.
) h a
om
Note: For your benefit, the file info.sh is already available in the lab directory. You can
l ฺ c
use this file in Solaris environment. However, for Oracle Linux you would require to edit the
m ai eฺ
file and replace /usr/bin/bash at the top of the script with /bin/bash.
@ g uid
student@S11-Server:~$ cd lab
a n 8 t G
student@S11-Server:~/lab$
id ude n
student@S11-Server:~/lab$ vi info.sh elz
( a d is St
an se th
#!/usr/bin/bash
i d
ntheZ date,
#info.sh
t o utime, username and the current
# This script displays a
m nse
directory.
l r h
echo "Datede
b and timel i ceis:"
dated A
a b le
e
echo sfe
m r
a
oh echo an"Your username is: `whoami` \n"
M - t r
Adel nonecho "Your current directory is: \c"
pwd

"info.sh" 10 lines, 232 characters

Note: Exit the file by pressing the ESC key followed by executing the vi quit
command :q. The first entry #!/usr/bin/bash indicates that the script should be
run in the bash shell.
b. Grant execute permission to the script by running the chmod command. (Confirm by
running the ls -l command).
student@S11-Server:~/lab$ chmod +x info.sh
student@S11-Server:~/lab$ ls -l info.sh
-rwxr-xr-x 1 student staff 232 May 11 19:34 info.sh
Copyright © 2012, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 8: Using Advanced Shell Features in Shell Scripts


Chapter 8 - Page 9
YAT

c. To execute the script, run the command as shown below:


student@S11-Server:~/lab$ ./info.sh
Date and time is:
Friday, May 11, 2012 07:57:09 PM IST
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2013, Oracle and/or its affiliatesฺ

Your username is: student

Your current directory is: /home/student/lab


2. In this task, you will pass values to the greetings shell script, which is available in the
lab directory.
a. View greetings by running the cat command.
student@S11-Server:~/lab$ cat greetings
#!/bin/sh
echo $1 $2 #echo the first two parameters passed
s a
b. Add execute permissions to greetings. ) h a
student@S11-Server:~/lab$ chmod u+x greetings
l ฺ c om
c. Run greetings with the hello and world values. m ai eฺ
@ g uid
student@S11-Server:~/lab$ ./greetings hello world
a n 8 t G
hello world
zid tude n
student@S11-Server:~/lab$ e l
d is S
( a
n e th
3. In this task, you will practice using the testacommand.
Test whether the value of the LOGNAME i d
Zvariable uissstudent.
n
a echo t o
student@S11-Server:~/lab$
l r h m
e n se $LOGNAME
student
b de e lic
d A abl
student@S11-Server:~/lab$ test "$LOGNAME" = "student"
e
m nsfe r
h a
student@S11-Server:~/lab$
o 0 -tra
echo $?

l M
A on
de nstudent@S11-Server:~/lab$
Note: The test command does not return any output. For a true condition, the exit
status of the test command is set to 0.
Now test, whether the value of the LOGNAME variable is user.
4. In this task, you will practice using the conditional statements. Using the conditional if
statement, test if we are in a leap year or now.
a. Create a shell script called leaptest.sh using the vi editor.
Note: For your benefit leaptest.sh script is available for use in the lab directory. You
can use this file as it is in Solaris environment. For Oracle Linux, replace /usr/bin/bash
at the top of the script with /bin/bash.

Copyright © 2012, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 8: Using Advanced Shell Features in Shell Scripts


Chapter 8 - Page 10
YAT

student@S11-Server:~/lab$ vi leaptest.sh
#!/usr/bin/bash
# This script will test if the year is a leap year.

year=`date +%Y`
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2013, Oracle and/or its affiliatesฺ

if [ $[$year % 400] -eq "0" ]; then


echo "This is a leap year. February has 29 days."
elif [ $[$year % 4] -eq 0 ]; then
if [ $[$year % 100] -ne 0 ]; then
echo "This is a leap year, February has 29 days."
else
echo "This is not a leap year. February has 28 days."
fi
s a
else
) h a
echo "This is not a leap year. February has 28 days."
l ฺ c om
fi
m ai eฺ
@ g uid
~
a n 8 t G
~
zid tude n
"leaptest.sh" 17 lines, 466 characters e l
d is S
( a
b. Add execute permission to the script. an
i d e th
student@S11-Server:~/lab$Zchmod u+x
n t o us leaptest.sh
a
the datese
m
c. Find the current year using
l r h
e whether e n command and then subsequently run the
leaptest script todfind
b e l ic the current year is a leap year.
e d A rabl
student@S11-Server:~/lab$ date
a m nMay
Friday,
h s fe11, 2012 09:11:58 PM IST
M o student@S11-Server:~/lab$
- tra ./leaptest.sh
l
de nThis n
o is a leap year, February has 29 days.
A student@S11-Server:~/lab$ cd
student@S11-Server:~$

Copyright © 2012, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 8: Using Advanced Shell Features in Shell Scripts


Chapter 8 - Page 11
YAT
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2013, Oracle and/or its affiliatesฺ

s a
) h a
l ฺ c om
m ai eฺ
@ g uid
a n 8 t G
zid tude n
e l
d is S
( a
i d an se th
a n Z to u
l r h m nse
b de e lice
e d A rabl
h am nsfe
M o -tra
l
de non
A
YAT
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2013, Oracle and/or its affiliatesฺ

s a
) h a
l ฺ c om
Practices for Lesson m ai eฺ
9:
@ g uid
Archiving Files
a n 8and G
Remote
t
Transfer zid tude n
e l
d is S
( a
i d an se th
Chapter 9

a n Z to u
l r h m nse
b de e lice
e d A rabl
h am nsfe
M o -tra
l
de non
A

Copyright © 2012, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 9: Archiving Files and Remote Transfer


Chapter 9 - Page 1
YAT

Practices for Lesson 9


Practices Overview
In this practice, you will perform a range of tasks described in this lesson. A list of those
activities is as follows:
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2013, Oracle and/or its affiliatesฺ

• Create an archive file on a disk


• View an archive file on a disk
• Retrieve archive data from a disk
• Compress files
• View compressed files
• Uncompress files
• Establish a remote login session
• Copy files or directories to and from another system
• Transfer files between systems
s a
) h a
om
UNIX and Linux Essentials - Activities Checklist
l ฺ c
Introduction m ai eฺ
@ g uid
a n 8 t G
Introduction to UNIX
zid tude n
e l
d is S
( a
Working with Files and Directories
i d an se th
a n Z to u
Using the vi Editor rhm
l e n se
b de e lic
e d A rwithin
Using Commands a bl the Default Shell
h a m nsfe
M o -tra
l
de nUsingon Basic File Permissions
A
Performing Basic Process Control

Using Advanced Shell Functionalities in Shell Scripts

Archiving Files and Remote Transfer

Copyright © 2012, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 9: Archiving Files and Remote Transfer


Chapter 9 - Page 2
YAT

Practice 9-1: Archiving and Retrieving Files

Overview
In this practice, you will perform tasks to create, view, and retrieve archived files.

Assumptions
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2013, Oracle and/or its affiliatesฺ

Ensure that the S11-Server VM is up and running. The tasks and steps below are performed in
the Oracle Solaris environment, but can be replicated in the Oracle Linux environment. The
command's output may vary slightly on your systems. Students will perform the exercises in the
/home/student directory. For Oracle Linux, change the directory from Desktop to the
/home/student directory. Output may vary from system to system. Run chmod 775 on the
lab directory, before starting this practice.
student@S11-Server:~$ chmod -R 775 lab
[student@OL6-Server ~]$ chmod -R 775 lab

s a
) h a
Tasks
l ฺ c om
1. What type of file does the tar command create?
m ai eฺ
A tar file
@ g uid
2. n 8 t G
Archive the lab directory in your home directory to a file called lab.tar by using the tar
a
command.
zid tude n
e l
d is S
student@S11-Server:~$ pwd
( a
/home/student
i d an se th
n Z to u
student@S11-Server:~$ ls -l
a
total 24
l r h m nse
b de e lice
lrwxrwxrwx 1 student staff 26 May 17 07:56 contents -

d A rabl
> /var/sadm/install/contents
e
drwxr-xr-x
m nsfe 2 student staff 5 Apr 9 19:03 Desktop
h a drwxr-xr-x
o -tra 6 root staff 6 Apr 9 19:03 Documents
l M
de non
drwxr-xr-x 2 student staff 2 Apr 9 19:03 Downloads
A drwxr-xr-x 2 student staff 2 May 17 07:55 house
drwxr-xr-x 17 student staff 42 May 17 18:54 lab
drwxr-xr-x 2 student staff 2 Apr 9 19:03 Public
drwxr-xr-x 2 student staff 2 May 17 07:44 records

student@S11-Server:~$ tar cvf lab.tar lab


a lab/ 0K
a lab/dir4/ 0K
a lab/.recently-used 0K
a lab/dante_1 1K
a lab/.gnome/ 0K
a lab/Documents/ 0K
a lab/Documents/misc.txt 1K

Copyright © 2012, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 9: Archiving Files and Remote Transfer


Chapter 9 - Page 3
YAT

a lab/Documents/sample.txt 1K
a lab/tutor.vi 29K
a lab/.ICEauthority 1K
a lab/file.3 0K
a lab/.sh_history 1K
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2013, Oracle and/or its affiliatesฺ

a lab/.gconf/ 0K
a lab/file2 1K
a lab/.metacity/ 0K
a lab/dir3/ 0K
a lab/dir3/planets/ 0K
a lab/dir3/planets/pluto 1K
a lab/dir3/planets/mars 1K
a lab/.rhosts 1K
a lab/Desktop/ 0K
s a
a lab/.kshrc 1K
) h a
a lab/.profile 1K
l ฺ c om
a lab/.gnome2/ 0K
m ai eฺ
a lab/.gconfd/ 0K
@ g uid
a lab/greetings 1K
a n 8 t G
a lab/.Xauthority 1K
zid tude n
a lab/leaptest.sh 1K e l
d is S
( a
a lab/file1 2K
i d an se th
n Z to u
a lab/info.sh 1K
a a
m nse
lab/.nautilus/ 0K
l r h
de e lice
a lab/file3 1K
b
d A rabl
a lab/dir2/ 0K
e
m nsfe
a lab/dir2/recipes/ 0K
h a
o -tra a lab/dir2/notes 0K
l M
de non
a lab/dir2/beans/ 0K
A a lab/practice/ 0K
a lab/practice/research 0K
a lab/practice/mailbox 0K
a lab/practice/project 0K
a lab/practice/projection 0K
a lab/practice/results 0K
a lab/file.2 0K
a lab/hello.sh 1K
a lab/dir5/ 0K
a lab/dante 2K
a lab/file4 1K
a lab/myvars 1K
a lab/dir1/ 0K

Copyright © 2012, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 9: Archiving Files and Remote Transfer


Chapter 9 - Page 4
YAT

a lab/dir1/coffees/ 0K
a lab/dir1/coffees/nuts 0K
a lab/dir1/coffees/beans/ 0K
a lab/dir1/coffees/beans/beans 12K
a lab/dir1/coffees/brands 0K
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2013, Oracle and/or its affiliatesฺ

a lab/dir1/fruit/ 0K
a lab/dir1/trees/ 0K
a lab/file.1 0K
a lab/.gnome2_private/ 0K
a lab/fruit 1K
a lab/fruit2 1K
a lab/.gtkrc-1.2-gnome2 1K

student@S11-Server:~$ ls -l /home/student
s a
total 201
) h a
lrwxrwxrwx 1 student staff 26 May
l ฺ c om 17 07:56 contents -
> /var/sadm/install/contents
i
aDesktop
drwxr-xr-x 2 student staff 5 Apr m
9 19:03
g i d eฺ
drwxr-xr-x 6 root staff 6 Apr
899@19:03 G u
Documents
a n t
drwxr-xr-x 2 student staff 2 Apr
l z id 17 u d en
19:03 Downloads
drwxr-xr-x 2 student staff 2 May
a d e S t 07:55 house
drwxr-xr-x 17 student staff
n ( this 17 18:54 lab
42 May
a
-rw-r--r-- 1 student staff
n Zid o use
90112 May 22 09:40 lab.tar
drwxr-xr-x 2 student staff
m a e t2 Apr 9 19:03 Public
drwxr-xr-x h
lr icen
2 student staff s 2 May 17 07:44 records
b d e l
d A a b le steps to create and archive the file to another drive or an
m edrive. fThe
Note: You can
er command’s output may vary from system to system.
follow similar
a
external
oh a-new s
andirectory under the lab directory of your home directory called retrieve.
M t r
del Usenothencd command to move to the new directory. You can use the new directory to
3. Create
A practice retrieving files from archives. Retrieve the contents of the lab.tar file that you
just created.
student@S11-Server:~$ cd lab
student@S11-Server:~/lab$ mkdir retrieve
student@S11-Server:~/lab$ cd retrieve
student@S11-Server:~/lab/retrieve$ tar xvf /home/student/lab.tar
x lab, 0 bytes, 0 tape blocks
x lab/dir4, 0 bytes, 0 tape blocks
x lab/.recently-used, 0 bytes, 0 tape blocks
x lab/dante_1, 368 bytes, 1 tape blocks
x lab/.gnome, 0 bytes, 0 tape blocks
x lab/Documents, 0 bytes, 0 tape blocks
x lab/Documents/misc.txt, 21 bytes, 1 tape blocks
Copyright © 2012, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 9: Archiving Files and Remote Transfer


Chapter 9 - Page 5
YAT

x lab/Documents/sample.txt, 28 bytes, 1 tape blocks


x lab/tutor.vi, 28709 bytes, 57 tape blocks
x lab/.ICEauthority, 520 bytes, 2 tape blocks
x lab/file.3, 0 bytes, 0 tape blocks
x lab/.sh_history, 526 bytes, 2 tape blocks
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2013, Oracle and/or its affiliatesฺ

x lab/.gconf, 0 bytes, 0 tape blocks


x lab/file2, 105 bytes, 1 tape blocks
x lab/.metacity, 0 bytes, 0 tape blocks
x lab/dir3, 0 bytes, 0 tape blocks
x lab/dir3/planets, 0 bytes, 0 tape blocks
x lab/dir3/planets/pluto, 42 bytes, 1 tape blocks
x lab/dir3/planets/mars, 68 bytes, 1 tape blocks
x lab/.rhosts, 2 bytes, 1 tape blocks
x lab/Desktop, 0 bytes, 0 tape blocks
s a
x lab/.kshrc, 507 bytes, 1 tape blocks
) h a
x lab/.profile, 724 bytes, 2 tape blocks
l ฺ c om
x lab/.gnome2, 0 bytes, 0 tape blocks
m ai eฺ
x lab/.gconfd, 0 bytes, 0 tape blocks
@ g uid
x 8
lab/greetings, 59 bytes, 1 tape blocks
a n t G
x
zid tude
lab/.Xauthority, 200 bytes, 1 tape blocksn
x e l
d is S
lab/leaptest.sh, 466 bytes, 1 tape blocks
( a
x
an se th
lab/file1, 1610 bytes, 4 tape blocks
i d
n Z to u
x lab/info.sh, 232 bytes, 1 tape blocks
x a
m nse
lab/.nautilus, 0 bytes, 0 tape blocks
l r h
de e lice
x lab/file3, 218 bytes, 1 tape blocks
b
d A rabl
x lab/dir2, 0 bytes, 0 tape blocks
e
m nsfe
x lab/dir2/recipes, 0 bytes, 0 tape blocks
h a
o -tra x lab/dir2/notes, 0 bytes, 0 tape blocks
l M
de non
x lab/dir2/beans, 0 bytes, 0 tape blocks
A x lab/practice, 0 bytes, 0 tape blocks
x lab/practice/research, 0 bytes, 0 tape blocks
x lab/practice/mailbox, 0 bytes, 0 tape blocks
x lab/practice/project, 0 bytes, 0 tape blocks
x lab/practice/projection, 0 bytes, 0 tape blocks
x lab/practice/results, 0 bytes, 0 tape blocks
x lab/file.2, 0 bytes, 0 tape blocks
x lab/hello.sh, 32 bytes, 1 tape blocks
x lab/dir5, 0 bytes, 0 tape blocks
x lab/dante, 1319 bytes, 3 tape blocks
x lab/file4, 137 bytes, 1 tape blocks
x lab/myvars, 67 bytes, 1 tape blocks
x lab/dir1, 0 bytes, 0 tape blocks

Copyright © 2012, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 9: Archiving Files and Remote Transfer


Chapter 9 - Page 6
YAT

x lab/dir1/coffees, 0 bytes, 0 tape blocks


x lab/dir1/coffees/nuts, 0 bytes, 0 tape blocks
x lab/dir1/coffees/beans, 0 bytes, 0 tape blocks
x lab/dir1/coffees/beans/beans, 12288 bytes, 24 tape blocks
x lab/dir1/coffees/brands, 0 bytes, 0 tape blocks
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2013, Oracle and/or its affiliatesฺ

x lab/dir1/fruit, 0 bytes, 0 tape blocks


x lab/dir1/trees, 0 bytes, 0 tape blocks
x lab/file.1, 0 bytes, 0 tape blocks
x lab/.gnome2_private, 0 bytes, 0 tape blocks
x lab/fruit, 57 bytes, 1 tape blocks
x lab/fruit2, 57 bytes, 1 tape blocks
x lab/.gtkrc-1.2-gnome2, 96 bytes, 1 tape blocks
student@S11-Server:~/lab/retrieve$

s a
student@S11-Server:~/lab/retrieve$ ls
) h a
lab
l ฺ c om
student@S11-Server:~/lab/retrieve$ ls -l
m ai eฺ
total 5
@ g uid
drwxr-xr-x 17 student staff 8
42 May 17 18:54 lab
a n t G
student@S11-Server:~/lab/retrieve$ cd
zid tude n
student@S11-Server:~$ e l
d is S
( a
n e th
i d a
n Z between
Note: The command output may vary
t o usthe system and the environment.
a
l r h m
e n se
b de e lic
e d A rabl
h am nsfe
M o -tra
l
de non
A

Copyright © 2012, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 9: Archiving Files and Remote Transfer


Chapter 9 - Page 7
YAT

Practice 9-2: Compressing and Restoring Files

Overview
In this practice you compress, view, and uncompress files.

Assumptions
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2013, Oracle and/or its affiliatesฺ

It is recommended to use one system at a time. Students will perform the exercises in the
/home/student directory. For Oracle Linux, change the directory from Desktop to the
/home/student directory. The command output may vary on your system.

Tasks
1. In the lab directory of your home directory, use the compress command to compress the
dante and file1 files.
student@S11-Server:~$ pwd
/home/student
s a
student@S11-Server:~$ cd lab
) h a
student@S11-Server:~/lab$ compress dante
l ฺ c om
student@S11-Server:~/lab$ compress file1
m ai eฺ
@ g uid
Note: Oracle Linux does not support compress command.
a n 8Use gzip t Gto compress
zidthe output n
defile extension will be
and gunzip to uncompress. For Linux environment,
e l
d is S t u
.gz instead of .z
( a
2. What are the new names for the compressed
i d anversions
e ththe dante and file1 files?
of
n Zls -lo us
student@S11-Server:~/lab$
m a e t
total 129 h
lr iceroot n s
-rwxr-xr-x d 1estudent
l
b e
368 May 2 13:51 dante_1
-rwxr-xr-x
e d A r1astudent
bl staff 896 May 2 13:51 dante.Z
am nsfe2 student root
drwxr-xr-x
h 2 May 2 13:51 Desktop
M o - tr
drwxr-xr-xa 5 student root 5 May 2 13:51 dir1
l o n
de ndrwxr-xr-x 4 student root 5 May 2 13:51 dir2
A drwxr-xr-x 3 student root 3 May 2 13:51 dir3
drwxr-xr-x 2 student root 3 May 9 15:28 dir4
drwxr-xr-x 2 student root 2 May 2 13:51 dir5
drwxr-xr-x 2 student root 4 May 9 14:30 Documents
-rw-r--r-- 1 student staff 248 May 10 17:11 err
-rw-r--r-- 1 student staff 248 May 10 17:10 error
-rw-r--r-- 1 student staff 248 May 10 17:09 file
-rwxr-xr-x 1 student root 0 May 2 13:51 file.1
-rwxr-xr-x 1 student root 0 May 2 13:51 file.2
-rwxr-xr-x 1 student root 0 May 2 13:51 file.3
-rwxr-xr-x 1 student staff 1035 May 2 13:51 file1.Z
-rwxr-xr-x 1 student root 105 May 2 13:51 file2
-rwxr-xr-x 1 student root 218 May 2 13:51 file3
Copyright © 2012, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 9: Archiving Files and Remote Transfer


Chapter 9 - Page 8
YAT

-rwxr-xr-x 1 student root 137 May 2 13:51 file4


-rwxr-xr-x 1 student root 57 May 2 13:51 fruit
-rwxr-xr-x 1 student root 57 May 2 13:51 fruit2
-rwxr-xr-x 1 student root 59 May 2 13:51 greetings
-rwxr--r-- 1 student staff 32 May 11 20:39 hello.sh
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2013, Oracle and/or its affiliatesฺ

-rwxr-xr-x 1 student staff 232 May 11 19:34 info.sh


-rwxr--r-- 1 student staff 466 May 11 21:11
leaptest.sh
-rwxr-xr-x 1 student root 67 May 2 13:51 myvars
drwxr-xr-x 3 student staff 6 May 9 15:20 perm
drwxr-xr-x 2 student root 7 May 2 13:51 practice
drwxr-xr-x 3 student staff 3 May 12 18:52 retrieve
drwxr-xr-x 2 student staff 2 May 9 15:41 test2dir
-rw-r--r-- 1 student staff 0 May 9 15:41 test2file
s a
drwxr-x--- 2 student staff 2 May 9 15:41 testdir
) h a
om
-rw-r----- 1 student staff 0 May 9 15:41 testfile
l ฺ c
ai eฺ
-rwxr-xr-x 1 student root 28709 May 2 13:51 tutor.vi
student@S11-Server:~/lab$ m
g uid
The new names for compressed versions are dante.Z and file1.Z.
n 8 @ G
aa
3. Identify the commands you can use to view the contents ofid e n t
l z u d
file that was compressed with
the compress command.
( a de is St
• uncompress -c filename
i d an se th
• zcat filename
n Z ou
• gzcat filename m a e t
h s
lr icen uncompress -c dante.Z
d e l and Times of Dante
student@S11-Server:~/lab$
b Thele Life
A
m ed ferab
o ha rans
l M n - t by Dante Pocai

Ade no
Mention "Alighieri" and few may know about whom you are talking.
Say
<output truncated> …………

student@S11-Server:~/lab$ zcat dante.Z


The Life and Times of Dante

by Dante Pocai

Mention "Alighieri" and few may know about whom you are talking.
Say

Copyright © 2012, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 9: Archiving Files and Remote Transfer


Chapter 9 - Page 9
YAT

<output truncated> …………

student@S11-Server:~/lab$ gzcat dante.Z


The Life and Times of Dante
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2013, Oracle and/or its affiliatesฺ

by Dante Pocai

Mention "Alighieri" and few may know about whom you are talking.
Say
<output truncated> …………
4. Use the gzip command to compress the file2 and dante_1 files.
student@S11-Server:~/lab$ gzip file2 dante_1
5. What are the new names for the compressed versions of the file2 and dante_1 files?
s a
• file2.gz
) h a
• dante_1.gz
l ฺ c om
student@S11-Server:~/lab$ ls -l m ai eฺ
@ g uid
total 132
a 8
n 2 13:51
t Gdante_1.gz
-rwxr-xr-x 1 student staff 268 dMay
e n
zi May tu2d13:51
d e l
-rwxr-xr-x 1 student staff
2 student root n ( a
896
S dante.Z
2isMay 2 13:51 Desktop
drwxr-xr-x
a t h
drwxr-xr-x 5 student root
Z id use 5 May 2 13:51 dir1
drwxr-xr-x 4 student n
a roote to 5 May 2 13:51 dir2
drwxr-xr-x h m
lr iceroot
3 student n s 3 May 2 13:51 dir3
d e l
drwxr-xr-x b 2 student root
d A b l e 3 May 9 15:28 dir4

m e fe2rastudent root
drwxr-xr-x 2 May 2 13:51 dir5

o a
h rans
drwxr-xr-x 2 student root 4 May 9 14:30 Documents

e l o n -t
M -rw-r--r-- 1 student staff 248 May 10 17:11 err
Ad n-rw-r--r-- 1 student staff 248 May 10 17:10 error
-rw-r--r-- 1 student staff 248 May 10 17:09 file
-rwxr-xr-x 1 student root 0 May 2 13:51 file.1
-rwxr-xr-x 1 student root 0 May 2 13:51 file.2
-rwxr-xr-x 1 student root 0 May 2 13:51 file.3
-rwxr-xr-x 1 student staff 1035 May 2 13:51 file1.Z
-rwxr-xr-x 1 student staff 112 May 2 13:51 file2.gz
-rwxr-xr-x 1 student root 218 May 2 13:51 file3
-rwxr-xr-x 1 student root 137 May 2 13:51 file4
-rwxr-xr-x 1 student root 57 May 2 13:51 fruit
-rwxr-xr-x 1 student root 57 May 2 13:51 fruit2
-rwxr-xr-x 1 student root 59 May 2 13:51 greetings
-rwxr--r-- 1 student staff 32 May 11 20:39 hello.sh

Copyright © 2012, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 9: Archiving Files and Remote Transfer


Chapter 9 - Page 10
YAT

-rwxr-xr-x 1 student staff 232 May 11 19:34 info.sh


-rwxr--r-- 1 student staff 466 May 11 21:11
leaptest.sh
-rwxr-xr-x 1 student root 67 May 2 13:51 myvars
drwxr-xr-x 3 student staff 6 May 9 15:20 perm
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2013, Oracle and/or its affiliatesฺ

drwxr-xr-x 2 student root 7 May 2 13:51 practice


drwxr-xr-x 3 student staff 3 May 12 18:52 retrieve
drwxr-xr-x 2 student staff 2 May 9 15:41 test2dir
-rw-r--r-- 1 student staff 0 May 9 15:41 test2file
drwxr-x--- 2 student staff 2 May 9 15:41 testdir
-rw-r----- 1 student staff 0 May 9 15:41 testfile
-rwxr-xr-x 1 student root 28709 May 2 13:51 tutor.vi
student@S11-Server:~/lab$
6. What is the difference between the gzip and zip command?
s a
The gzip command reduces the file size, whereas the zip command compresses one or
) h a
om
more files into a single zip archive.
l ฺ c
7.
ai eฺ
Use the zip command to compress the file3, fruit2, and tutor.vi files to the
files.zip file. m
@ g uid
student@S11-Server:~/lab$ zip files.zip file38fruit2 Gtutor.vi
a n t
adding: file3 (deflated 26%)
l z id uden
adding: fruit2 (deflated 14%)
( a de is St
adding: tutor.vi (deflated 74%)
i d an se th
n Z the
8. Which command would you use for viewing
o u
compressed archive file called
files.zip?
m a e t
l r h e n sunzip
b d e lic
student@S11-Server:~/lab$ -l files.zip
Archive: A
d files.zip
a b le
m e ferDate Time Name
Length
o a
h--------- a ns ---------- ----- ----
M - t r
Adel non 218 05-02-2012 13:51 file3
57 05-02-2012 13:51 fruit2
28709 05-02-2012 13:51 tutor.vi
--------- -------
28984 3 files
9. Do the original versions of the file3, fruit2, and tutor.vi files still exist after
compression?
Yes.
10. Uncompress the dante.Z and file1.Z files.
student@S11-Server:~/lab$ uncompress dante.Z
student@S11-Server:~/lab$ uncompress file1.Z
Do the dante and file1 files still have a .Z extension in their file names?
No.
student@S11-Server:~/lab$ ls -l
Copyright © 2012, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 9: Archiving Files and Remote Transfer


Chapter 9 - Page 11
YAT

total 153
-rwxr-xr-x 1 student staff 1319 May 2 13:51 dante
-rwxr-xr-x 1 student staff 268 May 2 13:51 dante_1.gz
drwxr-xr-x 2 student root 2 May 2 13:51 Desktop
drwxr-xr-x 5 student root 5 May 2 13:51 dir1
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2013, Oracle and/or its affiliatesฺ

drwxr-xr-x 4 student root 5 May 2 13:51 dir2


drwxr-xr-x 3 student root 3 May 2 13:51 dir3
drwxr-xr-x 2 student root 3 May 9 15:28 dir4
drwxr-xr-x 2 student root 2 May 2 13:51 dir5
drwxr-xr-x 2 student root 4 May 9 14:30 Documents
-rw-r--r-- 1 student staff 248 May 10 17:11 err
-rw-r--r-- 1 student staff 248 May 10 17:10 error
-rw-r--r-- 1 student staff 248 May 10 17:09 file
-rwxr-xr-x 1 student root 0 May 2 13:51 file.1
s a
-rwxr-xr-x 1 student root 0 May 2 13:51 file.2
) h a
-rwxr-xr-x 1 student root
l ฺ c om 0 May 2 13:51 file.3
-rwxr-xr-x 1 student staff
m ai eฺ 1610 May 2 13:51 file1
-rwxr-xr-x 1 student staff
@ g uid 112 May 2 13:51 file2.gz
-rwxr-xr-x 1 student root
a n 8 t G 218 May 2 13:51 file3
zid tude n
-rwxr-xr-x 1 student root
e l
d is S
137 May 2 13:51 file4
-rw-r--r--
( a
1 student staff 7983 May 12 19:24 files.zip
-rwxr-xr-x
i d an se th
1 student root 57 May 2 13:51 fruit
-rwxr-xr-x
a n Z to u
1 student root 57 May 2 13:51 fruit2
-rwxr-xr-x
l r h m nse
1 student root 59 May 2 13:51 greetings

b de e lice
-rwxr--r-- 1 student staff 32 May 11 20:39 hello.sh

e d A rabl
-rwxr-xr-x 1 student staff 232 May 11 19:34 info.sh

h am nsfe
-rwxr--r-- 1 student staff 466 May 11 21:11

M o -tra leaptest.sh
l
de non
-rwxr-xr-x 1 student root 67 May 2 13:51 myvars
A drwxr-xr-x 3 student staff 6 May 9 15:20 perm
drwxr-xr-x 2 student root 7 May 2 13:51 practice
drwxr-xr-x 3 student staff 3 May 12 18:52 retrieve
drwxr-xr-x 2 student staff 2 May 9 15:41 test2dir
-rw-r--r-- 1 student staff 0 May 9 15:41 test2file
drwxr-x--- 2 student staff 2 May 9 15:41 testdir
-rw-r----- 1 student staff 0 May 9 15:41 testfile
-rwxr-xr-x 1 student root 28709 May 2 13:51 tutor.vi
student@S11-Server:~/lab$
11. Which command would you use to uncompress the file2 and dante_1 files?
The gunzip file2.gz dante_1.gz command
student@S11-Server:~/lab$ gunzip file2.gz dante_1.gz
Do the file2 and dante_1 files still have a .gz extension in their file names?
Copyright © 2012, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 9: Archiving Files and Remote Transfer


Chapter 9 - Page 12
YAT

No.
student@S11-Server:~/lab$ ls -l
total 153
-rwxr-xr-x 1 student staff 1319 May 2 13:51 dante
-rwxr-xr-x 1 student staff 368 May 2 13:51 dante_1
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2013, Oracle and/or its affiliatesฺ

drwxr-xr-x 2 student root 2 May 2 13:51 Desktop


drwxr-xr-x 5 student root 5 May 2 13:51 dir1
drwxr-xr-x 4 student root 5 May 2 13:51 dir2
drwxr-xr-x 3 student root 3 May 2 13:51 dir3
drwxr-xr-x 2 student root 3 May 9 15:28 dir4
drwxr-xr-x 2 student root 2 May 2 13:51 dir5
drwxr-xr-x 2 student root 4 May 9 14:30 Documents
-rw-r--r-- 1 student staff 248 May 10 17:11 err
-rw-r--r-- 1 student staff 248 May 10 17:10 error
s a
-rw-r--r-- 1 student staff 248 May 10 17:09 file
) h a
-rwxr-xr-x 1 student root
l ฺ c om 0 May 2 13:51 file.1
-rwxr-xr-x 1 student root
m ai eฺ 0 May 2 13:51 file.2
-rwxr-xr-x 1 student root
@ g uid 0 May 2 13:51 file.3
-rwxr-xr-x 1 student staff
a n 8 t G 1610 May 2 13:51 file1
zid tude n
-rwxr-xr-x 1 student staff
e l
d is S
105 May 2 13:51 file2
-rwxr-xr-x
( a
1 student root 218 May 2 13:51 file3
-rwxr-xr-x
i d an se th
1 student root 137 May 2 13:51 file4
-rw-r--r--
a n Z to u
1 student staff 7983 May 12 19:24 files.zip
-rwxr-xr-x
l r h m nse
1 student root 57 May 2 13:51 fruit

b de e lice
-rwxr-xr-x 1 student root 57 May 2 13:51 fruit2

e d A rabl
-rwxr-xr-x 1 student root 59 May 2 13:51 greetings

h am nsfe
-rwxr--r-- 1 student staff 32 May 11 20:39 hello.sh

M o -tra -rwxr-xr-x 1 student staff 232 May 11 19:34 info.sh


l
de non -rwxr--r-- 1 student staff 466 May 11 21:11
A leaptest.sh
-rwxr-xr-x 1 student root 67 May 2 13:51 myvars
drwxr-xr-x 3 student staff 6 May 9 15:20 perm
drwxr-xr-x 2 student root 7 May 2 13:51 practice
drwxr-xr-x 3 student staff 3 May 12 18:52 retrieve
drwxr-xr-x 2 student staff 2 May 9 15:41 test2dir
-rw-r--r-- 1 student staff 0 May 9 15:41 test2file
drwxr-x--- 2 student staff 2 May 9 15:41 testdir
-rw-r----- 1 student staff 0 May 9 15:41 testfile
-rwxr-xr-x 1 student root 28709 May 2 13:51 tutor.vi
student@S11-Server:~/lab$

Copyright © 2012, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 9: Archiving Files and Remote Transfer


Chapter 9 - Page 13
YAT

12. Which command would you use to unarchive the file3, fruit2, and tutor.vi files
from the zip file created in Step 7?
student@S11-Server:~/lab$ unzip files.zip
Archive: files.zip
replace file3? [y]es, [n]o, [A]ll, [N]one, [r]ename: A
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2013, Oracle and/or its affiliatesฺ

inflating: file3
inflating: fruit2
inflating: tutor.vi
student@S11-Server:~/lab$ cd
student@S11-Server:~$
Does the files.zip file still exist in the directory?
Yes

s a
) h a
l ฺ c om
m ai eฺ
@ g uid
a n 8 t G
zid tude n
e l
d is S
( a
i d an se th
a n Z to u
l r h m nse
b de e lice
e d A rabl
h am nsfe
M o -tra
l
de non
A

Copyright © 2012, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 9: Archiving Files and Remote Transfer


Chapter 9 - Page 14
YAT

Practice 9-3: Performing Remote Connections and File Transfers

Overview
In this practice, you will use some of the remote connection and file transfer commands
explained in this lesson.
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2013, Oracle and/or its affiliatesฺ

Assumptions
In this practice, S11-Server VM is used as the host system and OL6-Server VM is used as the
remote system. Both the VMs have pre-installed and enabled ssh service to be able to perform
remote connections and secure file transfers. You can follow similar commands in the reverse
order as well.
Ensure both VMs, S11-Server and OL6-Server, are up and running before beginning this
practice.
Launch the calculator on the remote system, OL6-Server, prior to beginning the practice, by
performing the gcalctool command.
s a
Note: The above action is required in order to perform task 4 in this practice where you will try
to perform a command on a remote system to kill the process related to this application. ) h a
Open a terminal and run the command as shown below.
l ฺ c om
m ai eฺ
[student@OL6-Server ~]$ gcalctool
@ g uid
a n 8 t G
zid tude n
e l
d is S
( a
i d an se th
a n Z to u
l r h m nse
b de e lice
e d A rabl
h am nsfe
M o -tra
l
de non
A

Tasks
1. Open a terminal in the S11-Server VM and use the ssh command to log in to OL6-Server
in your virtual network.
student@S11-Server:~$ ssh student@OL6-Server
student@ol6-server's password:
Last login: Sat May 12 19:58:12 2012 from s11-
server.suned.sun.com
[student@OL6-Server ~]$ pwd
/home/student
Copyright © 2012, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 9: Archiving Files and Remote Transfer


Chapter 9 - Page 15
YAT

[student@OL6-Server ~]$

Or

student@S11-Server:~$ ssh –l student OL6-Server


Unauthorized reproduction or distribution prohibitedฺ Copyright© 2013, Oracle and/or its affiliatesฺ

student@ol6-server's password:
Last login: Sat May 12 19:58:46 2012 from s11-
server.suned.sun.com
[student@OL6-Server ~]$ pwd
/home/student
Note: Password for the user student is student1. The default directory on the remote
machine will be the user’s home directory. In this case, it is /home/student. The output
message may differ on your system.
2. Use the uname command to display the host name of the current system.
s a
[student@OL6-Server ~]$ uname -n
) h a
OL6-Server
l ฺ c om
[student@OL6-Server ~]$
m ai eฺ
3. Use the ps command to identify the PID of the gcalctool command on
@ g the remote
uid
system. 8
an ent G
i d
[student@OL6-Server ~]$ ps -ef | grep lgcalctool
d e z
S t udgcalctool
student 3357 3343 0 12:17 pts/0
n (a this 00:00:00

Z ida to usystem.
se
Note: The output may differ from n system
a using o
tthe
4. Terminate the gcalctool h m
r cen
command s e PID.
e l
bd le l~]$
[student@OL6-Server
A
i kill 3357
Are you able etod terminate
r a bthe process? Why or why not?
m
alog in toathe f e
sremote
o h
If you
r n system as root, you can terminate the process. If you log in to the
l remote t
Mremote nsystem,
- then also youuser
system as the same (same UID) as the user who started the process on the
d e n o can terminate the process. However, if you log in to the
A remote system as some other user, you cannot terminate the process because you do not
own the process and do not have the appropriate permission.
5. Log out of the remote system.
[student@OL6-Server ~]$ exit
logout
Connection to OL6-Server closed
6. Display the host name of your current system to determine whether you have returned to
your host system.
student@S11-Server:~$ uname -n
S11-Server
7. Which remote connection command allows you to run a program remotely?
The ssh command

Copyright © 2012, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 9: Archiving Files and Remote Transfer


Chapter 9 - Page 16
YAT

8. Which command copies files remotely?


The scp command
9. Copy the dante file from the lab directory of your home directory to the
/home/student/lab/dir1 directory on the remote host.
student@S11-Server:~$ cd lab
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2013, Oracle and/or its affiliatesฺ

student@S11-Server:~/lab$ ls
dante file.1 info.sh
dante_1 file.2 leaptest.sh
Desktop file.3 myvars
dir1 file1 perm
dir2 file2 practice
dir3 file3 retrieve
dir4 file4 test2dir
dir5 files.zip test2file
s a
Documents fruit testdir
) h a
err fruit2 testfile
l ฺ c om
error greetings tutor.vi
m ai eฺ
file hello.sh
@ g uid
student@S11-Server:~/lab$ pwd
a n 8 t G
/home/student/lab
zid tude n
student@S11-Server:~/lab$ scp dante OL6- e l
d is S
( a
an se th
Server:/home/student/lab/dir1
i d
n Z to u
student@ol6-server's password:
dante a 0%
m nse 0 --:-dante
l r h
de e lice
100% 1319 00:00
b
d A rabl
student@S11-Server:~/lab$
e
mPassword fefor the user student on the remote system, OL6-Server is
h a
Note:
o student1. n s
l M n - tra Command output may vary from system to system.
Ade no
10. Copy the dante file from the remote system back to the dir2 directory on your local
system.
student@S11-Server:~/lab$ scp OL6-
Server:/home/student/lab/dir1/dante /home/student/lab/dir2
student@ol6-server's password:
dante 100% |***************| 1319 00:00
student@S11-Server:~/lab$
11. Copy the practice directory in the lab directory of your home directory to the home directory
of the remote system, OL6-Server.
student@S11-Server:~/lab$ scp -r practice OL6-
Server:/home/student
student@ol6-server's password:
mailbox 100% |***************| 0 00:00
research 100% |***************| 0 00:00
Copyright © 2012, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 9: Archiving Files and Remote Transfer


Chapter 9 - Page 17
YAT

project 100% |***************| 0 00:00


results 100% |***************| 0 00:00
projection 100% |***************| 0 00:00

Which command allows you to copy remote directories?


Unauthorized reproduction or distribution prohibitedฺ Copyright© 2013, Oracle and/or its affiliatesฺ

scp –r
12. Retrieve the file myvars from the remote system, OL6-Server to the /home/student
directory on the host, S11-Server.
student@S11-Server:~/lab$ cd
student@S11-Server:~$ pwd
/home/student
student@S11-Server:~$ sftp OL6-Server
Connecting to OL6-Server...
student@ol6-server's password:
s a
sftp> pwd
) h a
Remote working directory: /home/student
l ฺ c om
sftp> get lab/myvars
m ai eฺ
Fetching /home/student/lab/myvars to myvars
@ g uid
/home/student/lab/myvars 100% 67
a n 80.1KB/s
t G 00:00
sftp> exit
zid tude n
student@S11-Server:~$ ls e l
d is S
( a
Desktop lab
i d an se th
n Z to u
directory_list myvars
Documents perma
m nse
l r h
de e lice
Downloads practice1
b
d A rabl
file1 Public
e
m nsfe
file2
h a
o -trastudent@S11-Server:~$
l M
de Whichnon
a. remote connection command transfers files securely from system to system,
A including binary files?
The sftp command
b. How do you end an ftp session?
Type exit or quit or bye at the sftp> prompt.
13. Transfer multiple files from the /home/student/lab directory starting with the string “file”
from a remote system to your current working directory on your local system.
student@S11-Server:~$ sftp OL6-Server
Connecting to OL6-Server...
student@ol6-server's password:
sftp> cd lab
sftp> mget file*
Fetching /home/student/lab/file.1 to file.1
Fetching /home/student/lab/file.2 to file.2

Copyright © 2012, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 9: Archiving Files and Remote Transfer


Chapter 9 - Page 18
YAT

Fetching /home/student/lab/file.3 to file.3


Fetching /home/student/lab/file1 to file1
/home/student/lab/file1 100% 1610 1.6KB/s 00:00
Fetching /home/student/lab/file2 to file2
/home/student/lab/file2 100% 105 0.1KB/s 00:00
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2013, Oracle and/or its affiliatesฺ

Fetching /home/student/lab/file3 to file3


/home/student/lab/file3 100% 218 0.2KB/s 00:00
Fetching /home/student/lab/file4 to file4
/home/student/lab/file4 100% 137 0.1KB/s 00:00
sftp> exit
student@S11-Server:~$ ls -l
total 42
drwxr-xr-x 2 student staff 5 Apr 9 19:03 Desktop
-rw-r--r-- 1 student staff 630 May 10 17:05
s a
directory_list
) h a
om
drwxr-xr-x 6 student staff 6 Apr 9 19:03 Documents
l ฺ c
ai eฺ
drwxr-xr-x 2 student staff 2 Apr 9 19:03 Downloads
-rwxr-xr-x 1 student staff m
g uid
0 May 14 13:29 file.1
-rwxr-xr-x 1 student staff 0 May 8 14@ 13:29Gfile.2
an 14 e13:29
0idMay n t file.3
-rwxr-xr-x 1 student staff
d e lz May tu14d13:29 file1
-rwxr-xr-x 1 student staff
( a 1610
i s S
-rwxr-xr-x 1 student staffan
d e h
t May 14 13:29 file2
105
i s
Z o u 218 May 14 13:29 file3
1 student n staff
-rwxr-xr-x
m a e t
-rwxr-xr-x
l h
r cen
1 student s
staff 137 May 14 13:29 file4
drwxr-xr-x b21
e
d student li root 52 May 12 19:39 lab
d A b l
1astudent
e
m e
-rwxr-xr-x
e r staff 67 May 14 13:15 myvars

o a ansf 2 student staff


hdrwxr-xr-x 7 May 9 15:15 perm

e l o n -tr
M drwxr-xr-x 2 student staff 2 May 9 14:25 practice1

Ad ndrwxr-xr-x 2 student staff 2 Apr 9 19:03 Public


student@S11-Server:~$
14. Transfer the file myvars from /home/student to the student home directory on the
remote system, OL6-Server.
student@S11-Server:~$ sftp OL6-Server
Connecting to OL6-Server...
student@ol6-server's password:
sftp> put myvars
Uploading myvars to /home/student/myvars
myvars 100% 67 0.1KB/s 00:01
sftp> exit
student@S11-Server:~$

Copyright © 2012, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 9: Archiving Files and Remote Transfer


Chapter 9 - Page 19
YAT

Which command is used to transfer the file to a remote system?


put
15. Transfer multiple files starting with the string “file” from the home directory
/home/student of the local system to a directory on the remote system by using the
mput command.
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2013, Oracle and/or its affiliatesฺ

student@S11-Server:~$ sftp OL6-Server


Connecting to OL6-Server...
student@ol6-server's password:
sftp> mput file*
Uploading file.1 to /home/student/file.1
file.1 100% 0 0.0KB/s 00:00
Uploading file.2 to /home/student/file.2
file.2 100% 0 0.0KB/s 00:00
Uploading file.3 to /home/student/file.3
s a
file.3 100%
Uploading file1 to /home/student/file1
0 0.0KB/s 00:00
) h a
l ฺ c om
ai eฺ
file1 100% 1610 00:00 1.6KB/s
Uploading file2 to /home/student/file2 m
g u00:00 id
file2 100% 105
8 @
0.1KB/s
G
Uploading file3 to /home/student/file3
i d an ent
e lz t d
u0.2KB/s
file3
a d
100% 218
( this S 00:00
n
Uploading file4 to /home/student/file4
a
file4
n Zid o use
100% 137 0.1KB/s 00:00

m a e t
sftp> exit h
lr icen s
b d e l ls -l
student@S11-Server:~$
d A b l e
total e42
m e r a
o a ansf 2 student staff
hdrwxr-xr-x 5 Apr 9 19:03 Desktop

e l o n -tr
M -rw-r--r-- 1 student staff 630 May 10 17:05
Ad n directory_list
drwxr-xr-x 6 student staff 6 Apr 9 19:03 Documents
drwxr-xr-x 2 student staff 2 Apr 9 19:03 Downloads
-rwxr-xr-x 1 student staff 0 May 14 13:29 file.1
-rwxr-xr-x 1 student staff 0 May 14 13:29 file.2
-rwxr-xr-x 1 student staff 0 May 14 13:29 file.3
-rwxr-xr-x 1 student staff 1610 May 14 13:29 file1
-rwxr-xr-x 1 student staff 105 May 14 13:29 file2
-rwxr-xr-x 1 student staff 218 May 14 13:29 file3
-rwxr-xr-x 1 student staff 137 May 14 13:29 file4
drwxr-xr-x 21 student root 52 May 12 19:39 lab
-rwxr-xr-x 1 student staff 67 May 14 13:15 myvars
drwxr-xr-x 2 student staff 7 May 9 15:15 perm
drwxr-xr-x 2 student staff 2 May 9 14:25 practice1
Copyright © 2012, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 9: Archiving Files and Remote Transfer


Chapter 9 - Page 20
YAT

drwxr-xr-x 2 student staff 2 Apr 9 19:03 Public


student@S11-Server:~$ exit
16. Close the terminal windows.

17. Power off the S11-Server and OL6-Server virtual machines.


Unauthorized reproduction or distribution prohibitedฺ Copyright© 2013, Oracle and/or its affiliatesฺ

s a
) h a
l ฺ c om
m ai eฺ
@ g uid
a n 8 t G
zid tude n
e l
d is S
( a
i d an se th
a n Z to u
l r h m nse
b de e lice
e d A rabl
h am nsfe
M o -tra
l
de non
A

Copyright © 2012, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 9: Archiving Files and Remote Transfer


Chapter 9 - Page 21
YAT
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2013, Oracle and/or its affiliatesฺ

s a
) h a
l ฺ c om
m ai eฺ
@ g uid
a n 8 t G
zid tude n
e l
d is S
( a
i d an se th
a n Z to u
l r h m nse
b de e lice
e d A rabl
h am nsfe
M o -tra
l
de non
A

You might also like