Post Lab activity1

13
Laboratory Activity 1 Name: __________________________________ Date: _____________ Unix Basic Commands 1.0 Unix / Linux Command Line Interface (CLI) Basics – Basic Commands This section of the course provides a discussion and hands-on guide on the basic CLI commands in Unix/Linux. The basic CLI commands are: how to log-in, log-out, interpreting the command prompt, how to enter commands, basic file commands and management, and simple file permission. The objectives of this section are: • Familiarize the students on how to gain access to a Unix/Linux server. • Familiarize the students on what is the prompt and how to enter commands. • Familiarize the students on file identifiers and, relative and absolute file names. • Familiarize the students on basic file commands and management. 1.1. Logging In To access a Unix/Linux server, a Telnet or SSH client software can be used to connect to the server. Most operating system today has some form of this software like “putty.exe”. A login prompt for a Unix server can look like: Login: <username> Password: Before you try to log-in to a Unix server, ask your instructor for a user account, and the IP address or server name where your account is valid. After acquiring the user account and the server you are assigned to, accomplish the following: Step 1 On your Windows operating system, use an ssh client program to connect to the Unix server. Using the “putty” client program, enter the IP address or name of the Linux server in the “Host Name” text box and click the “Open” button. 1

Transcript of Post Lab activity1

Page 1: Post Lab activity1

Laboratory Activity 1

Name: __________________________________ Date: _____________

Unix Basic Commands

1.0 Unix / Linux Command Line Interface (CLI) Basics – Basic CommandsThis section of the course provides a discussion and hands-on guide on the basic CLI commands in Unix/Linux. Thebasic CLI commands are: how to log-in, log-out, interpreting the command prompt, how to enter commands, basic filecommands and management, and simple file permission. The objectives of this section are:• Familiarize the students on how to gain access to a Unix/Linux server.• Familiarize the students on what is the prompt and how to enter commands.• Familiarize the students on file identifiers and, relative and absolute file names.• Familiarize the students on basic file commands and management.

1.1. Logging InTo access a Unix/Linux server, a Telnet or SSH client software can be used to connect to the server. Most operatingsystem today has some form of this software like “putty.exe”. A login prompt for a Unix server can look like:

Login: <username>Password:

Before you try to log-in to a Unix server, ask your instructor for a user account, and the IP address or server namewhere your account is valid. After acquiring the user account and the server you are assigned to, accomplish thefollowing:

Step 1 On your Windows operating system, use an ssh client program to connect to the Unix server. Using the“putty” client program, enter the IP address or name of the Linux server in the “Host Name” text box andclick the “Open” button.

1

Page 2: Post Lab activity1

Step 2 At the login prompt, type your assigned username and password.2a. Were you able to login? How were you able to login?

_________________________________________________________________________

_________________________________________________________________________

2b. Did you have a user account when you logged-in?

_________________________________________________________________________

_________________________________________________________________________

2c. What is the IP address or server name of the Unix server you logged into?

_________________________________________________________________________

_________________________________________________________________________

1.2. The Command PromptAfter you have logged-in, notice that a prompt will appear on the screen. A prompt is an interface that allows user to input commands. Different shells use different faces of prompts (shells will be tackled later in the course). In this server, you will be greeted with:

Notice the different parts of your prompt. For the example prompt, it has displayed the username, current directory youare in, the computer name and the prompt sign.

Step 3 Write down the prompt on your screen._________________________________________________________________________

_________________________________________________________________________

3a. Is your username displayed on the prompt? What is your username?_________________________________________________________________________

_________________________________________________________________________3b. Is a computer name displayed on the prompt? What is the computer name?_________________________________________________________________________

_________________________________________________________________________

2

Page 3: Post Lab activity1

3c. What is the prompt sign on your screen?

_________________________________________________________________________

_________________________________________________________________________

1.3. Entering CommandsCommands can be entered to the Unix server via the command prompt. This is usually done by typing a series of characters after the prompt sign. A usual format in entering commands is:

[dwight@netopsy1 greg]$ command <options> <argument>

Options for the command are switches that tell a command to do something differently. Arguments are informationsometimes needed by the command to be able to execute properly. Most of the time, options and arguments are not needed in a command.

There are also commands wherein you do not need to type anything. These commands are: ctrl-c, ctrl-d, ctrl-s, and ctrl-d. The table below gives a short description.

Step 4 On the command prompt, type “man –S 1 pwd” and press enter. This command will be tackled later in the course, for now just accept it. 4a. What is the option in the command?_________________________________________________________________________

_________________________________________________________________________

4b. What is the argument in the command?_________________________________________________________________________

_________________________________________________________________________

4c. Press “q” to exit the command._________________________________________________________________________

_________________________________________________________________________

Step 5 On the command prompt, type “top” and press enter.

5a. Press the control and “c” key at the same time. What happened? Did it have the same effect as pressing “q”?Why”_________________________________________________________________________

_________________________________________________________________________

3

Page 4: Post Lab activity1

Step 6 On the command prompt, type ”man chmod” and press enter.6a. Press the spacebar. Did the screen scroll up?

_________________________________________________________________________

_________________________________________________________________________

6b. Press the control and “s” key at the same time and then press the spacebar. Did the screen scroll up? Why?

_________________________________________________________________________

_________________________________________________________________________

6c. This time press the control and “q” key at the same time. What happened?

________________________________________________________________________

_________________________________________________________________________

6d. What does the command “ctrl-s” and “ctrl-q” do?

________________________________________________________________________

_________________________________________________________________________

1.4. Changing PasswordOne of the first things you must do on your user account is to change your password. Password ensures that no one will have unauthorized access to your user account. A user must have to be authenticated using a password before access to server will be given. To change your password, use the command: passwd.

You will be prompted by the server of your old password and then will be prompted to input a new password. You will have to re-type your new password for verification. You can change your password anytime. Password must have at least six characters with any alphanumeric combination (at least for Linux) but is case sensitive.

Step 7 At the command prompt, type “passwd”.

7a. Enter your current password when prompted for the old password.7b. Enter the password “h3ll0”, when prompted for the new password.7c. Was the password rejected? Why was it rejected?

________________________________________________________________________

_________________________________________________________________________

7d. Enter the password “r3ch3tt” when prompted for the new password.7e. Was the password was accepted? Why was it accepted?

________________________________________________________________________

_________________________________________________________________________7f. What is the message when the password was successfully changed?

________________________________________________________________________

_________________________________________________________________________

4

Page 5: Post Lab activity1

Step 8 At the command prompt, type “passwd“ again. Type the current password as “R4ch3tt”.8a. Was the password rejected? Why was it rejected?

________________________________________________________________________

_________________________________________________________________________

8b. What is the message when the password was not successfully changed?

________________________________________________________________________

_________________________________________________________________________

Step 9 At the command prompt, type “passwd“ again. Enter the current password. Use your desired password whenprompted for a new password. Remember your new password.

9a. Were you able to give a new password on your first try? What were the messages from the server?

________________________________________________________________________

_________________________________________________________________________

1.5. Basic File ManagementUnix file system, like other file systems of other operating systems, consists of files and each file has a file name. There are three kinds of files on your Unix system: ordinary, special and directories.

The Unix file system is organized as a hierarchy of directories where each directory can contain files or subdirectories. This hierarchy is called a tree where the root (/) is the top of the tree.

Each file in a directory has a file identifier (this is not synonymous to file name), which is a sequence of characters thatuniquely identifies a file or directory. A file identifier can have at most 255 characters (this includes extension); any printable character can be used as to name a file but cannot contain “/”. Although the characters “*”, “?”, “>”, “>” or “|”, it is not recommended because it has special meaning. Remember that filenames in Unix are case sensitive.

File names in Unix, contains a sequence of file identifiers that designates a file. Slashes “/” are used to separate thesequence of file identifiers. There are two kinds of file name: absolute and relative file name. Absolute file name specifies the sequence of directories starting from the root (it actually starts with a “/”) and can be used to refer to a fileanywhere in the tree. Relative file name’s starting point is the current directory or working directory. Relative file name can use symbols “.” and “..” which represents the current directory and parent directory respectively. Another symbol “~”, represents the home directory. Home directory is the personal directory of a user where your files can be stored.

In order to do basic file management, there are several basic commands that can be used. Usual commands needed are to list files in a directory, change to another directory, create a new directory, move a file or directory, copy a file or directory, and remove a file or directory. Most of the commands can use absolute or relative filenames. The commandsare presented in a table below.

5

Page 6: Post Lab activity1

1.5.1. Changing to different directoriesThe “cd” command is used to change the working directory. Every time you log in via Telnet, you will be in your homedirectory. Most of the time, your directory is located in the “/home” parent directory. The hierarchy can be shown as:

The hierarchy shown is very simple. This is not the whole hierarchy for the Unix system. Also, not all hierarchy is the same for all Unix versions.

Step 10 While you are in the home directory, type the command “pwd”.10a. What was the message on the screen?________________________________________________________________________

_________________________________________________________________________

10b. What does the “pwd” command do?________________________________________________________________________

_________________________________________________________________________

Step 11 Type the command “cd /etc” and press enter.11a. Did the prompt display differently? Write down the display.________________________________________________________________________

_________________________________________________________________________

11b. Type the command “pwd”. What is your current working directory?________________________________________________________________________

_________________________________________________________________________

6

Page 7: Post Lab activity1

Step 12 From the “/etc” directory, type the command “cd /home/<username>”.12a. Did you go to your “home” directory? How were you able to determine?

________________________________________________________________________

_________________________________________________________________________

12b. What was the file name used (absolute or relative file name)? Why?

________________________________________________________________________

_________________________________________________________________________

12c. On the command prompt, type “cd ../../etc” and press enter. Were you able to go to the “/etc” directory?What was the file name used (absolute or relative file name)? Why?

________________________________________________________________________

_________________________________________________________________________

12d. From the “/etc” directory, type the command “cd ~”. Were you able to go to the “home” directory? Which “cd”command is this similar to?

________________________________________________________________________

_________________________________________________________________________

1.5.2. Listing DirectoriesThe “ls” command is used to list directories. There are a lot of options you can do with the command. It is able to display all files, directories only and other more options.

Step 13 On the command prompt, type the command “cd ..” and press enter.13a. Enter the “ls” command. What did you see?

________________________________________________________________________

_________________________________________________________________________

13b. Enter the “ls -A” command. What is the option? Was it different from last time?

________________________________________________________________________

_________________________________________________________________________

13c. Enter the “ls -a” command. What is the option? Was it different from last time? What does it do?

________________________________________________________________________

_________________________________________________________________________

The “ls” command with option “A”, shows all files in the directory including hidden files. Hidden files are files thatstart with “.”. The “a” option has the same function as “A” option except that it also displays the “.” (current) and “..”(parent) directories.

7

Page 8: Post Lab activity1

13d. Enter the “ls -l” command. What is the option? Was it different from last time? What does it do?

________________________________________________________________________

_________________________________________________________________________

The first column on the “ls -l” shows the file permission of the file or directory (again, will be discussed later).Notice that a “d” as the first character shows which are the directories.

13e. Enter “cd /etc” and then the “ls -d rc0.d” command. What is the option? What does it do? Is it different from the “lsrc0.d” command?

________________________________________________________________________

_________________________________________________________________________

The “d” displays if the “rc0.d” directory is there or not. While the “ls” command without the “d” option will displaythe contents of the directory “rc0.d”.

13f. Enter the “ls -R” command. What is the option? What does it do? How is it different from “ls”.

________________________________________________________________________

_________________________________________________________________________

The “R” option displays the directories recursively.

Step 14 Metacharacters are characters with special meaning in Unix. One of the metacharacters is “*”.14a. Go to the “/home/dir1” directory, type the command “ls f*”. What files were listed?

________________________________________________________________________

_________________________________________________________________________

14b. Enter the command “ls p*”. What files were listed?

________________________________________________________________________

_________________________________________________________________________

14c. Enter the command “ls file?”. What files were listed?

________________________________________________________________________

_________________________________________________________________________

14d. What is the difference between the commands “ls f*” and “ls file?”?

________________________________________________________________________

_________________________________________________________________________

14e. Enter the command “ls [f..p]ile1”. What files were listed?

________________________________________________________________________

_________________________________________________________________________

8

Page 9: Post Lab activity1

14f. Enter the command “ls [f..p]ile2”. What files were listed?

________________________________________________________________________

_________________________________________________________________________

14g. Enter the command “ls [f..p]ile?”. What files were listed?

________________________________________________________________________

_________________________________________________________________________

14h. What does the metacharacter “[..]” do?

________________________________________________________________________

_________________________________________________________________________

The metacharacter “*” is a wildcard for multiple characters while “?” is wildcard for a single character. Themetacharacter “[..]” is used to specify a range of characters.

1.5.3. Creating DirectoriesDirectories in the Unix operating system can be created using the “mkdir” command. Naming directories has the same rules as naming files. Exercises on filenames will be tackled later.

Step 15 Go to your home directory by entering the command “cd ~”. Then create a directory “coffees” using the command “mkdir coffees”.15a. Was the directory created?

________________________________________________________________________

_________________________________________________________________________

15b. Enter the command “cd coffees” and press enter. Next, enter the command “mkdir beans/robusta”. Wereyou able to create the directory?

________________________________________________________________________

_________________________________________________________________________

15c. Enter the command “mkdir -p beans/robusta”. Were you able to create the directory? Why do you think so?What is the “p” option for?

________________________________________________________________________

___________________________________________________________________________________________

The “p” option allows creation of directories with its parent directory.

1.5.4. FilenamesEmpty files are files with a size of zero bytes. The “touch” command can be used to create empty files. The “touch” command will be used to practice creating files with different filenames.

In Unix, filenames can have up to 255 printable characters except for the “/” character. Although “*” and “?” can be used, these characters have special meaning.

9

Page 10: Post Lab activity1

Step 16 While in the directory “coffees/beans/robusta”, enter the command “touch robusta.txt”.16a. Were you able to create the file? What is the size of the file? How were you able to determine the file size?

________________________________________________________________________

___________________________________________________________________________________________

16b. Enter the command “touch robusta/txt“. Were you able to create the file? Why? What was the message?

________________________________________________________________________

___________________________________________________________________________________________

16c. Enter the command “touch robusta1“. Were you able to create the file? Why?

________________________________________________________________________

___________________________________________________________________________________________

16d. Enter the command “mkdir robusta.doc“. Were you able to create the directory? Why?

________________________________________________________________________

___________________________________________________________________________________________

16e. Enter the command “ls“. Can you tell which one is a directory? What command should be used to be able todetermine which one is a directory?

________________________________________________________________________

___________________________________________________________________________________________

16f. Enter the command “ls robusta*“. Is there a difference from the earlier command? Can you tell which one isthe directory? Why?

________________________________________________________________________

___________________________________________________________________________________________

1.5.5. Copying FilesThe cp command is used to copy files. The command can be used to copy one source to a destination or multiple sources to a directory.Step 17 Enter the command “cp robusta.txt robusta.html”.17a. What happened after entering the command? Did you see the file “robusta.html”?

________________________________________________________________________

___________________________________________________________________________________________

17b. Enter the command “cp robusta.txt robusta.doc”. What happened after entering the command? Did yousee the file “robusta.doc“? Why?

________________________________________________________________________

___________________________________________________________________________________________

10

Page 11: Post Lab activity1

17c. Enter the command “cp robusta.txt robusta.html”. Were you prompted if the file was to be overwritten?Why?

________________________________________________________________________

___________________________________________________________________________________________

17d. Enter the command “cp –i robusta.txt robusta.html”. Were you prompted if the file was to beoverwritten? Why?

________________________________________________________________________

___________________________________________________________________________________________

1.5.6. Moving FilesIn the Unix operating system, moving of files can be done by using the command “mv”. The command can also be used to rename files.

Step 18 Enter the command “mkdir -p ../arabica” and press enter. Next, enter the command “toucharabica.txt”. Use the command “ls” to verify the file is existent.

18a. Enter the command “mv arabica.txt ../arabica”. Is the file “arabica.txt” still there? Where is it located now?What did the command “mv” do?

________________________________________________________________________

____________________________________________________________________________________

18b. Enter the command “touch arabica.txt” under the directory “<homedirectory>/coffees/beans/robusta”. Next, type the command “mv arabica.txt ../arabica”. What happened?Were you prompted that you will be overwriting a file?

________________________________________________________________________

_____________________________________________________________________________________

18c. Enter the command “touch arabica.txt” under the directory “<homedirectory>/coffees/beans/robusta”. Next, type the command “mv –i arabica.txt ../arabica”. Whathappened? Were you prompted that you will be overwriting a file?

________________________________________________________________________

_____________________________________________________________________________________

Answer no to the prompt.

18d. Enter the command “mv arabica.txt arabica.doc”. What happened? Is the file still there? Why?

________________________________________________________________________

_____________________________________________________________________________________

11

Page 12: Post Lab activity1

The command can be used to rename files.18e. What is the difference between the command “cp arabica.txt ../arabica” and “mv arabica.txt../arabica”. Explain.

________________________________________________________________________

_____________________________________________________________________________________

1.5.7. Removing FilesRemoving or deleting files can be accomplished by using the command “rm”. Be careful in using this command since deleted file are unrecoverable.

Step 19 Go to the “<home directory>/coffees/beans/robusta” directory. Type the command “rm arabica.doc”.19a. Was the file still there? Can it be found in another directory? Why?

________________________________________________________________________

_____________________________________________________________________________________

Step 20 Enter the command “rm *”.20a. What happened? Which file or directory was retained?

________________________________________________________________________

_____________________________________________________________________________________

20b. Why was it retained?

________________________________________________________________________

_____________________________________________________________________________________

Step 21 Go to the “robusta.doc” directory. Create the following directory “bean_quality” and “bean_quality/perfect_cup”.21a. Go to the “<home directory>/coffees/beans/robusta”. Enter the command “rm robusta.doc”. Were youable to remove the directory? Why?

________________________________________________________________________

_____________________________________________________________________________________

21b. Enter the command “rm -r robusta.doc”. Were you able to remove the directory? Why? What is the option “r” for?

________________________________________________________________________

_____________________________________________________________________________________

Step 22 Go to the “arabica” directory. Enter the command “rm –i arabica.txt”.22a. Was the file erased immediately? What was the message?

________________________________________________________________________

_____________________________________________________________________________________

12

Page 13: Post Lab activity1

22b. Answer “y” to the prompt. Was the file removed? What is the option “i” for?

________________________________________________________________________

_____________________________________________________________________________________

22c. Go to the “<home directory>/coffees” directory and enter the command“rm -ir robusta.doc”. Were prompted before removal? What was the message?

________________________________________________________________________

_____________________________________________________________________________________

Answer “n” to the prompt.

1.6. Logging OutThe “exit” command can be used to log out of the Unix server.

Step 23 At the prompt type the command “exit”.23a. What happened? Were you able to log out? Is there an alternative command? (You may research this on books or in the Internet)

________________________________________________________________________

_____________________________________________________________________________________

13