Lab 4

 Part 1 - Using grep to find patterns 

When dealing with files, it is convenient to be able to search for patterns within the text. This is often used in code development for example. In Part 1 of Lab 4, we will be learning how to use grep.


The following is an example on using the comand grep: 

1) First, run cd /tmp command in terminal. 



2) Use dmesg to create a file, so we can search for some information about your system. To do so we can use the following command: dmesg > dmesg1.txt. 

  • As you can see in the picture above, after typing the command dmesg > dmesg1.txt, a new file is created located in tmp folder. 


3) Let's see if we can determine what network device is being used. Run the following command: grep network dmesg1.txt

  • The grep command filter searches a file for a particular pattern of characters, and displays all lines that contain that pattern. 

4)  The output in the last step might not be very informative. But what if case is an issue? Now let's try the following command: grep -i network dmesg1.txt



5)  The -i tells grep to ignore  case. You should now see which network driver your system is using. Now try the following command: grep -i cdrom dmesg1.txt



6)  The command grep returns a code based on the results of the search. Run the above command again. To run the previous command easily, we can use the up arrow shortcut just like what we've learned in the previous lab session. 




7) Now run the following command: echo $?



8)  Assuming the text was found, the return will be 0. Now  search for a string that should not be there. Type the following command: grep -i jimlewis dmesg1.txt



9) Run the following command again: echo $?

10) The return code should not be 0.






  Part 2 - Compressing files using ZIP and TAR  
It's no secret that a Linux system has a lot of files. A typical code development project might have over 1000 files, spread across several directories. And how do we back all of that stuff up? The answer is file packaging and compression. Here we will show two favorites, ZIP and TAR.

Here we will experiment with the zip and unzip command: 
1) Run the following command: cd /tmp



2) Let's make a temporary directory: mkdir lbooktemp



3) Run the following command: cd lbooktemp



4)  Now let's create some files: ls > f1.txt; route > f2.txt; dmesg > f3.txt
     After typing the command, we should see three 3 files created in the lbooktemp folder. 



5)  Then, create some more files by typing the following command: ifconfig > ifconfig.dat; dmesg > dmesg.dat




6) Let's package and compress the first ones into a single file by typing the following command: zip lbook1.zip f1.txt f2.txt f3.txt



7) As you can see, the syntax for ZIP is "zip zipped-file files-to-zip". We could have also used wildcards above: zip lbook1.zip *.txt

8) Now let's include the others: zip lbook1.zip *.txt *.dat

9) The unzip program is used to extract files out of a zipped file(also called an archive). Make another directory using the command: mkdir test




10) Run the following command: cp lbook1.zip test




11) Run the following command: cd test



12) Now unzip the file. To do this, we can use the command unzip lbook1.zip




13) Perform an ls -la command. You should see the files as before. 




14) You can also view the contents of a ZIP file, without extracting anything by running the following command: unzip -l zipped-file






I use ZIP when there are just a few files, or when I am sending them to someone running a non-Linux OS. While ZIP can be used to span directories, it doesn't normally deal well with some Linuz files. tar is a better alternatives.  

1) We can use the same files as before: cd/tmp/lbooktemp




2) Run tar cvzf lbook1.tar.gz *.txt. This will create a gzip compressed archive file



3) Now run file lbook1.tar.gz. It should show something like the following output: lbook1.tar.gz: POSIX tar archive (GNU)



4) To extract, first copy it to the test directory: cp lbook1.tar.gz test




5) Run the following command: cd test





6) Run the following command: tar xvzf lbook1.tar.gz




7) Perform an ls -la command. You should see the files again. 



8) To view a tar archive, use the t(for tell) option: tar tvzf lbook1.tar.gz


  • The tar command is used to compress a group of files into an archive. The command is also used to extract, maintain, or modify tar archives. 

9) Now let's TAR when the whole directory: cd /tmp



10) Run the following command: tar cvzf lbooktemp1.tar.gz lbooktemp


11) This will get the entire directorym even the hidden files if any exist. 



 Part 4 - Other helpful commands such as stat, sum, touch, and more 
There are many more commands available in Linux that deal with flies. In this section I will show how to use a few of them. 

The folowing are some of the commands I find myself using all the time: 

1. Run the following command: cd /tmp



2. Create a file: dmesg > file1.txt
    


3. Now run ls -la and remember the info. We will use this later. 



    
4. Use the stat command to see pratically everything you would ever want to know about the file: stat file1.txt


  • The stat command is a useful utility for viewing file or file system status. It has an option to specify a custom format instead of the default, for displaying information. 

5. Now suppose you have sent the file to someone that is running a Linux  system and want to ensure it did not get corupted along the way. Run the following command: sum file1.txt


  • The sum command gives you information about file checksum and the number of blocks a file is comprised of.

6. The first number is the checksum and the second is the number of blocks for that file. If the other person runs sum on his copy of the file and sees the same info, the flies are the same.

7.  We have created a lot of files by using the redirection operator. You can also use the touch command: touch file2.txt


  • The touch command is primarily used to change file timestamps, but if the file doesn't exist, then the tool creates it. 

8. Since file2.txt did not already exist, touch will create it as an empty file. In fact, let's prove that: file file2.txt



9. So what happens if we run touch on an existing file? Does it empty it? No, it updates the time and date on it. Run the following command: ls -la file1.txt




10. Now run the following command: touch file1.txt




11. Run ls -la again. You should notice it now shows the current date and time on that file. 




12. Suppose you want to just view a text file. Run the following command: less file1.txt
  • less command is linux utility which can be used to read contents of text file one page per time. It has faster access because if file is large, it don't access complete file, but access it page by page.

13. When using the less command press the Space bar to scroll down. Press Q to exit. 




14.  Say we want to see just the first few lines in that file: head file1.txt

  • The head command lets users view the first part of files. 

15.  The head command shows the first 10 lines by default. How bout the last 10 lines? Run the following command: tail file1.txt

  • tail is a command which prints the last few numbers of lines of a certain file, then termintates.