linux lab 3 file system
TRANSCRIPT
-
8/9/2019 Linux Lab 3 File System
1/7
Linux Lab The File System (2) Name:
Date:
Linux Lab This lesson will cover the following UNIX commands: man, who, more, pipe, cat, ls, cp, mv, redirect, append, andrm.
Wildcard charactersare introduced. man Command
In these labs, you will be learning about different commands. Commands tell UNIX to run a program by that name. The commands were
written by programmers to make using the system easier and faster. More information on the commands can be found in your UNIX text.
You can also obtain information about commands on-line on the computer. System documentation is stored on-line and is accessed byissuing the command:
man
where is the name of the command and represents the enter key on the terminal. Generally, the symbol will be omitted in
these lab assignments but the appropriate key (enter/return) must be pressed in order for the command to be issued.
who Command
In order to try out the mancommand, issue the command:
man who
You will get on-line information on what the whocommand does. The first information you will see is:
Reformatting page. Please wait ---- done.
User commands who (1)
Name
who - who is on the system
The "Name" gives the definition of the command. Next, you will see more information on what whodoes and some options that are
available. There will be a --More (5%) message at the bottom of the page. To see more information, press the space bar key. To quit
reading the manpages before the end of the file is reached, type qfollowed by the enterkey. You are back to the prompt line when you see
a $prompt on the left of your screen.
Read what the whocommand does. Explain what the whocommand does?
_____________________________________________________________________________
_____________________________________________________________________________
Next, issue the command:who
The who command will tell you who is on the system at the same time as you are. It lists the person's login names, terminal line, login time,and terminal name (this is the location of the person at that time). If there are many people on the system at the time that you try this, you
may have several names go by quickly on your screen. In order to see all of the names on one screen at a time, the morecommand is used.
more and the pipe Command
You can use the morecommand to display files on your terminal one screen at a time. It can be used with a | (vertical bar) with the who
command so you can scroll one page at a time. In response to the more prompt, press the space bar to continue to the next page. The more
command can also be used to allow you to view a long text file on the screen. An example of this would be if you had a text file called
chapter1, you could type in more chapter1and it would be listed one screen at a time.
The symbol for a pipeis a vertical bar (|). A pipe connects the standard output of one command directly with the standard input of another
command. It connects commands together. Standard outputis defined as the computer screen. In order to see how moreworks, type in the
following:
man who > sample
cat sample
cat sample | more
You will see one screen of information at a time. When you want to see the next screen, press the space bar. Something different happens if
you press the enterkey. Try it. What happens when you press the enterkey?
____________________________________________________________________________
Next, issue the command:
who am i
to obtain your login id, your terminal name, and the date and time of the login to the present session.
Your login id is ________________________________________________________________
-
8/9/2019 Linux Lab 3 File System
2/7
Your terminal line is: ____________________________________________________________
The date and time of your login to this session is ________________________________________
The name of your terminal location is ________________________________________________
As you learn more commands, you will see that there are different optionsthat are used with commands. There is always a space after the
command and the use of an option. Options start with the -(minus sign) and then some letter. These add more flexibility to commands. Next
is an example of the whocommand with an option.
Type in: who -q
The -qlists the login names of the people horizontally who are on the system and also gives you the total number of users. How many users
are on the system now? ___________________________________
cat Command
The catcommand has two functions. These two functions are:
1.To display the contents of text files. In the first lab, you worked on a file called practice. In order to see how catdisplays the contents of a
file, type in the following:
cat practice
The file practice will be listed. The file is only listed and you cannot make changes in it. In order to make changes, you would have to use
the vi editor. If you wanted to change the file, "practice," you would type in vi practice.
2.To concatenate (link or join) one or more files together and then send the result to the standard output. The standard output is theterminal screen.
In order to try out some different ways to use the catcommand, make three new files. Use the vieditor to create these files. Type in:
vi filea
Be sure to press thei key for insert to start the files.
The filea should contain this line:
THIS IS filea.
After you have typed in this line, press the esckey. Then use the :wqto save the file. Next, use the vi editor to type in:
vi fileb
It should contain this line:
this is fileb.
Save this file using the :wqcommand and make a third file. Type in:
vi filec
It should contain this line:
THIS is filec.
Save this file. Now you will try different ways to display the contents of these files on the terminal by using the catcommand. Display the
contents of each file in turn by issuing the commands:
cat filea
cat fileb
cat filec
Now issue the commands:cat filea; cat fileb; cat filec
How does the result from this sequence of commands compare with those above?
____________________________________________________________________________
____________________________________________________________________________
____________________________________________________________________________
Issue the command:
cat filea fileb filec
What appears on the screen? ____________________________________________________________________________
____________________________________________________________________________
-
8/9/2019 Linux Lab 3 File System
3/7
Use of Wildcard Characters
It is possible to abbreviate file name references for existing files by using either of two characters, namely *or ?, as a substitute for
characters in a file name. These characters are referred to as wildcard characters when used as a substitute for characters in a name. Issue the
command:
cat file*
What appears on the screen? ____________________________________________________________________________
____________________________________________________________________________
____________________________________________________________________________
Issue the command:
cat file?
What appears on the screen? ____________________________________________________________________________
____________________________________________________________________________
____________________________________________________________________________
Now issue the command:
cat fil*
What appears on the screen? ____________________________________________________________________________
____________________________________________________________________________
____________________________________________________________________________
Issue the command:
cat fil?
What appears on the screen? ____________________________________________________________________________
Note that one of these wildcard characters matches zero or more characters within a filename. The other matches only a single character
within the name of an existing file. After trying the above commands:
what does the *do? ____________________________________________________________
what does the ? do? ____________________________________________________________
Redirect Symbol
When you use the catcommand, the output goes to the terminal screen. The catcommand can be used to redirectthe standard output to a
fileinstead of the screen when the redirect symbol ">" is used in conjunction with the target filename. Issue the command:
cat file? > newfile
A new file with the name newfileshould have been created. What are the contents of newfile?
____________________________________________________________________________
What command did you use to display newfile?_________________________________________
If the file did not exist before you did the redirect, UNIX will create it. If the file already exists, its contents will be replaced. The original
contents are gone and you cannot get them back!
One way to prevent a file from being overwritten is to use the append (>>) command instead of the redirect (>) command.
Append Command
Occasionally, you may want to add a file to the endof another file. This is called appending a file onto another file. In order to do this, youuse the >> (two greater than signs). This would be used with the cat command. To see how the append command works, type in the
following:
cat practice >> newfile
List out the newfileagain. What happened to newfileas a result of the previous catcommand?
____________________________________________________________________________
If the file did not already exist, it would be created. If you already have a file by that name, it will be added to the bottom of that file. Using
the appendoption can be a way to protect files you have made. When a file does not already exist, there is no difference which method you
use, the redirect or append. However, if you are not sure about if a file exists, then using the appendis safer.
ls Command
-
8/9/2019 Linux Lab 3 File System
4/7
-
8/9/2019 Linux Lab 3 File System
5/7
destination-file already exists, it is overwritten and any information in it is destroyed. After one file has been copied to another, the two files
should be exactly the same. An example of the copy command is:
cp source destination
Now, use the vi editor to create a new file called source. It should contain the following line:
This is a test file for cp--the copy command.
Save the file and exit from vi. Now examine the contents of this file by issuing the command:
cat source
Next copy the file named sourceinto a new file named destination. Show the complete command you used to accomplish this:
____________________________________________________________________________
Issue the command:
cat source
Did the file sourcechange after the copy was made? ____________________________________
Next issue:
cat destination
Is the file destinationthe same as the file source? _____________________________________
Issue the command:
ls -l
What files were listed? ____________________________________________________________________________
____________________________________________________________________________
____________________________________________________________________________
Earlier, you created a file called newfile. Use the cat command to look at the file again. Now issue the following command:
cp source newfile
Now look at the file newfile. What happened to the contents of newfile?
____________________________________________________________________________
mv Command
Sometimes you want to change the name of a file or move a file from one directory to another. This can be accomplished by the mv
command. It is similar to the cpcommand. The first argument is the name of the file to be renamed (or moved) and the second argument isthe new name (or new destination). An example is:
mv file1 file2
The file1 name will be changed to file2. As in the case of the cpcommand, if the second file already exists, it will be overwritten when the
name is changed. Also remember with the mvcommand, the first file no longer exists. The mvcommand can also be used to move a file to
another directory. You will learn about directories in a later lab.
Issue the command:
mv source moved
Next issue the command:
ls -l
What happened to the sourcefile? ________________________________________________
The Remove Command
You can delete files that you no longer want by using the command, rm. Delete the following files from your directory by issuing the
command:
rm moved destination
Issue the command:
ls -l
What files are now listed? ____________________________________________________________________________
____________________________________________________________________________
-
8/9/2019 Linux Lab 3 File System
6/7
One problem when you use the rmcommand is that you dont get a second chance to decide if you want to delete a file. When you type in
the rmand the name of the file, that file is gone! When you use a personal computer, you are asked if you really want to delete a file.
You can use the option -iwith the rm if you want to be given an opportunity to decide if you want to keep the file. Here is an example:
Assuming the file moved exists, and the user types in the following:
rm -i moved
the system will respond with:
rm: remove moved: (yes/no)?
If you use the -ioption you will get a chance to decide if you want to delete a file. The file is listed and then you can type in either a "y" for
yes or "n" for no. In order to get some practice with this, type in the following:
rm -i newfile
When you are given a choice, type iny.
At the end of this lab, you should still have the following files:
practice
filea
fileb
filec
In order to see if you have these files, type in:
ls -l
Were these files listed?________________________________________
Try ls command using following options
Output in multiple columns (-x)
Identifying directories and executables (-F)
Showing hidden files (-a)
Recursive listing (-R)
Listing file attributes (-l)
in order to modification time (-t)
files access time (-u)
move to homework directory
Try chmod command using following options
Try to assign permissions to files
uxb% echo hell web > small
chmod go-r combine; ls l combine
-
8/9/2019 Linux Lab 3 File System
7/7
chmod u-rwx combine; ls l combine
chmod a+r, u+w combine; ls l combine
chmod a=r combine; ls l combine
chmod =r combine; ls l combine