The objective of this lab is to help students familiarize
more with the Linux environment and practice some common commands.
This lab assignment has the following steps:
- Clean up your directory
Login to a linux machine. Start a Console window. You should
be prompted with a window containing the following:
When you first login, your current working directory is your home
directory. Your home directory has the same name as your username,
and it is where your personal files and subdirectories are saved. Your
home directory can also be referred to by the tilde ~ character. So typing:
~$ cd ~
will move you to your home directory.
Exercise:The hierarchy that you should have in your home directory is as follows:
Checkout the hierarchy of your files.
If you have a hierarchy other than the above hierarchy make the necessary
changes using the commands we learned in previous labs (ls, mv, cp, mkdir, rm).
Check out the man pages or previous labs if you need a review.
Copy and paste the result of the command ls -R after you have tidied
up your files so they match this folder hierarchy.
- Redirecting input to a file and from a file.
In Linux, to redirect the output to a file, you can use the > character
like this: ls > file_name. The above redirects the output of the
ls command to a file called file_name. Because the output is
redirected to a file, you don't see any results of ls on your screen.
Now, in your home directory, do ls -alt > myfiles.
This should create a new file called myfiles that contains all files in
your directory, including several you have never seen before.
Type: more myfiles to see this list. The a
flag shows all files, including hidden files. In linux, hidden files
and directories begin with a period. These files are not normally displayed
when you do an ls. The l flag to ls displays a long
listing for each file, will details like the file permissions, creation
time, and size. Finally, the t flag sorts the files by time
with the most recently modified file first.
Copy and paste the contents of myfiles into the text box below.
- Redirecting input and output to/from a program
To work in this section write a simple c++ program that
will read two numbers as inputs and it will then calculate and
print the sum of those two numbers. Compile the program and name
the executable file sum.exe.
Sometimes you will need to do some processing on the
information a specific program is going to generate. To have the output
of a program redirected to some file, do the following:
./sum.exe > results.txt
The previous example will type the results of the program in the results.txt file.
Also, you can read the inputs from a data file. Create an ascii file called data.txt
using nano and store the values 2 and 4 in the file. Now execute the following command:
./sum.exe < data.txt
Finally, you can combine the previous two steps into one to make the program
reads it's inputs from a file and store the output in another file:
./sum.exe < data.txt > results.txt
The previous approach is used to capture the output of a
program for later analysis. Also, if the program takes a lot of inputs
and the user wants to run the program to many times, he/she can read the
values from a file which will save time and effort.<\p>
The script command is usually used to capture what
the user does in a specific period of time. The capture of screen will
start the moment the user types script and will continue until the user
types Cntl D. When the user types Cntl D, the screen capture will be
saved in a file called typescript.
To work in this section you will need to use the previously compiled program sum.exe.
The Ctrl D in the above sequence means to press the
control key and the D key on the keyboard simultaneously, the same
motion as when you hit shift D to type an uppercase letter.
Copy the content of the typescript file into the text box below.
The grep command is widely used to search for some strings
in files or in the output of some other commands.
Try the following series of command and observe what the output looks like:
ls -lR | grep cpp
What did you see? Type your output in the textbox below.
Now try to look for string patterns in a file. Create your file called text
using the nano editor and store some text in the file. Make sure to add
your name in the file. Make sure to include more than one line of text
in the file.
Type command grep 'your name' text and print your output in the text box below
- Access Permission
Before starting this section, create a new directory called test,
and inside that directory create a file called information.txt by just
typing something (anything) in using nano.