Starting from:

$30

Lab2–Linux-CommandLine Practice


Lab2–Linux-CommandLine Practice
CPSC 2311
Introduction 

The goal of this lab is to familiarize yourself with some new and some old Linux command line environment, including various tools that will be useful as a programmer. 

This lab may require you to perform a couple google searches in order to complete some of the task. 


Due: 
Friday, September 4, 2020, midnight.  Why Friday, because this lab should not take that much time to complete.

Lab Instructions 
Part 1

Step 1
If you do not already have a lab folder this this semester, open a terminal and create a lab folder called Labs2311 using mkdir. Now navigate to the lab folder. Create a folder for Lab2 and move into the folder. Copy the lab files from canvas to this folder. Each time you run the terminal, you are basically running a program called a shell. In many Linux distributions, including the lab machines, the default shell is called bash (short for Bourne-Again Shell)

For step 1 complete each of the task listed below.

    Task A. 
Bash can be customized by editing it’s configuration file. The name of the configuration file is .bashrc (note the period at the beginning), which holds the bash run commands. The .bashrc file is located in your home directory. Below, write a command to edit this file without leaving the current directory. (Hint: Bash uses the tilde character ~ as a shortcut for your home directory).

Type the working command here: vi ~/.bashrc

Now run the command to open the .bashrc file.

Task B.
    Bash allows for the use of aliases. Aliases are shortcuts to commands and are useful for shortening long commands that you find yourself using often. At the bottom of the .bashrc you opened, add an alias maketar that contains the flags to create a tar.gz file. There most likely are several alias shortcuts already in this file, use them as an example.
 
Now create an alias untar with the appropriate flags to untar a tar.gz file.  
Place a screen shot of your new maketar and untar aliases here.

 

Task C
    Save the file and quit. Back in the shell, you will need to run the command source on the file in order for bash to update with the new changes. Once done, in the terminal you can run the command alias to see a list of aliases that bash currently has defined. Put the results of running the alias command below.

To test your maketar command, if you are not already in your Lab2 folder, move into it. Use the command touch to create a file in your Lab2 folder and use your maketar alias to tar the content in the Lab2 folder.  Name your tar.gz file lab2_test.tar.gz.

This should create a lab2_test.tar.gz file that contains the content of your lab2 folder (even if is only 1 file). 

Place a screenshot of this process here.

Now test the untar alias by untaring your lab2_test.tar.gz file. – create a screen shot of the files untarred and place it in this document. 

 

Step 2
Many programs that can be installed on Linux also come installed with manuals. The program man will open the manual page for a specified program. Use man to learn more about commands you aren’t familiar with.

    Task A
Give a brief description of what each of these commands do.
            rm- remove file and directories
            cp-copy files and directories
            mv-move(rename) files
            cat-concatenate files and print on the standard output
            chmod-change file mode bits
            diff-compare files line by line
            watch-execute a program periodically, showing output full screen
            wc-print newline, word, and byte counts for each file 


    Task B
What do each of the flags mean in the command ls -lha
            l- long format, displaying Unix file types, permissions, number of hard links, owner, group, size, last-modified date and filename
            h- print sizes in human readable format. 
a-    lists all files in the given directory, including those whose names start with "." (which are hidden files in Unix)

Step 3
The pipe operator | can be used to connect one program’s output to another program’s input. Programs can be piped together indefinitely. For example, ls | sort -r will put the output of ls into sort which will then sort the listing in reverse order.

Task A
    grep is a really useful tool that can search files for a specific string. Write a command that will search the file provided, data_col, for all occurrences of the letter “B”. Use the man page for grep if you need help.

Type the working command here: grep “B” data_col
Post a screen shot of the working command in this document. 
 

Task B
    We want an easy way to count how many occurrences there are. Since grep puts each occurrence on a new line, we only have to count how many new lines are in the output. Again, use the man page for grep. Write a command that takes the output from Step 3, Task A and counts the lines.

Type your working command here: grep -c ^ data_col


Task C
awk is another useful tool. It does many things, but has a particular use that is commonly helpful: it can print columns from its input. Write a command that will print out only the values in the second column of data_col. You may find this link on printing fields with awk useful: 
https://www.gnu.org/software/gawk/manual/gawk.html#Print-Examples

awk ‘{print $2}’ data_col

Task D
sed is a stream editor program that can be used to replace strings in text using regular expressions. However, the replacement we want to perform is simple enough that regular expressions are not needed. The format to describe what text should be replaced looks like this: 

sed “s/[original]/[replacement]/g”, where [original] is the original text and [replacement] is what to replace it with.

    With that in mind, write a command that takes the output from Step 3, Task A, and replaces all instances of the letter B with the letter Z, and saves it to a file called updated.txt. 

sed “s/[B]/[Z]/g” > updated.txt

Step 4

Compile main1.c producing an executable called main1.
Compile main2.c producing an executable called main2.

Each of the following commands will run the specified program with subtle differences.   Enter each command and describe what happens.

./main1 – This is main1!

./main1 & 

 

./main1 && ./main2

 

Step 5
The exclamation mark (!) can be used to re-run a previous command.  If the last command that I executed was ./main1, which of the following would re-run this command.  Write TRUE if it would re-run, and False if it would not.(T/F)

!./m - T
!. - F
!s - F
!! - F
!m - F
!./main - T





Submission Instructions 
Using your newly created maketar tar all files for this lab and submit them through handin. (http://handin.cs.clemson.edu) 
Be sure to check your tarred files after submission to handin. 


More products