Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Lab 11 Part 1 Reading Read about the find utility (Sobell, pages 804–809). Proce

ID: 3571287 • Letter: L

Question

Lab 11 Part 1

Reading

Read about the find utility (Sobell, pages 804–809).

Procedure

1. The find (Sobell, page 804) utility locates files based on simple or complex criteria you specify. For example, find can list all files in /usr/bin that were modified more than 900 days ago.

$ find /usr/bin -mtime +900

/usr/bin/test_ppa

/usr/bin/detect_ppa

/usr/bin/lftpget

...

Try specifying 600 or 1200 in place of 900. Use a minus sign (–) in place of the plus sign (+) to specify files that were modified more recently than the number of days you specify. To specify an exact number of days, omit the +

or – sign. Send the output through a pipeline to wc –l (Sobell, page 1000) to count the number of files (lines) that find displays.

$ find /usr/bin -mtime +300 | wc –l

2. Use find with the –name (Sobell, page 806, and see the examples on Sobell, page 808) criterion to list the names of files in /bin that begin with the letter c.

3. Use find with the –size (Sobell, page 806) criterion to list the names of files in your home directory that are smaller than 100 bytes.

4. Use find to list all the files in the /usr hierarchy. Send the output through a pipeline to head to list only the first 10 files in the list. It may take a while before find displays output.

5. Use find with the –name (Sobell, page 806) and –type (Sobell, page 806) criteria to list all files in the /bin directory that have the characters sh in their names and are symbolic links.

6. Use find with the –exec (Sobell, page 805) criterion to run an ls –l command on the files returned by the find statement in the previous step.

Lab 11 Part 2

Reading

Read about the grep utility (Sobell, pages 833–837).

Procedure

1. Use grep to display lines that contain the string model in the /proc/cpuinfo file.

$ grep model /proc/cpuinfo

Display the line(s) in the same file that contain(s) the string model name. How can you ensure that grep interprets a string that contains a SPACE as a single argument?

2. Use grep with the –c (count; Sobell, page 833) option to display the number of lines that contain the string Remote in the /etc/services

file.

3. Use grep to determine how many lines in /etc/services contain the string service.

Add the –w (word; Sobell, page 834) option to determine how many lines contain the word s e r v i c e.

4. How many lines in /etc/services contain the word service, ignoring the case of the letters in the word?

5. How many lines in /etc/services do not contain the string service?

6. List each line in /etc/services that contains the word service. Precede each line by its line number in the file.

7. List 10 lines in files in the /usr/share directory hierarchy that contain the word 27 (i.e., not 27 embedded in a longer number).

8. Within the /usr/share directory hierarchy, list the number of times the word 27 occurs in files in which it occurs at least once. Hint: Connect the output of one grep command to the input of another grep command using a pipe; the first grep command should display the count for all files in the hierarchy and the second should get rid of those with a count of 0,

Explanation / Answer

2. Use find with the –name (Sobell, page 806, and see the examples on Sobell, page 808) criterion to list the names of files in /bin that begin with the letter c.

find /bin -name "c*" -type f

3. Use find with the –size (Sobell, page 806) criterion to list the names of files in your home directory that are smaller than 100 bytes.

find . -type f -size -100c
//c is for bytes,k is for Kilobytes

4. Use find to list all the files in the /usr hierarchy. Send the output through a pipeline to head to list only the first 10 files in the list. It may take a while before find displays output.

find /usr -type f | head
//type f means refular files

5. Use find with the –name (Sobell, page 806) and –type (Sobell, page 806) criteria to list all files in the /bin directory that have the characters sh in their names and are symbolic links.

find /bin -name "*sh*" -type l
//type l use find the symbolic link files, "*sh*" files containg sh character anywhere.

6. Use find with the –exec (Sobell, page 805) criterion to run an ls –l command on the files returned by the find statement in the previous step.

find . -name "linux" -exec ls -l {} ;
//will list all the files containing linux

1. Use grep to display lines that contain the string model in the /proc/cpuinfo file.

$ grep model /proc/cpuinfo

Display the line(s) in the same file that contain(s) the string model name. How can you ensure that grep interprets a string that contains a SPACE as a single argument?

grep "model name" /proc/cpuinfo

//give the strings with space in double quotes ("")

2. Use grep with the –c (count; Sobell, page 833) option to display the number of lines that contain the string Remote in the /etc/services
file.

grep -c "Remote" /etc/services
//c option is count option for grep utility

3. Use grep to determine how many lines in /etc/services contain the string service.
Add the –w (word; Sobell, page 834) option to determine how many lines contain the word s e r v i c e.

grep -wc "service" /etc/services

//w option is for strong type checking,c option is for count

4. How many lines in /etc/services contain the word service, ignoring the case of the letters in the word?

grep -ic "service" /etc/services

//i option is to ignore the case, c option is for count

5. How many lines in /etc/services do not contain the string service?

grep -vc "service" /etc/services

//v option is for ignoring the grep match

6. List each line in /etc/services that contains the word service. Precede each line by its line number in the file.

grep -ni "service" /etc/services

//n is for preceding line by its line number

7. List 10 lines in files in the /usr/share directory hierarchy that contain the word 27 (i.e., not 27 embedded in a longer number).


grep -R "27" /usr/share/ | head

//R is for recursive search

8. Within the /usr/share directory hierarchy, list the number of times the word 27 occurs in files in which it occurs at least once. Hint: Connect the output of one grep command to the input of another grep command using a pipe; the first grep command should display the count for all files in the hierarchy and the second should get rid of those with a count of 0,

grep -cR "27" /usr/share/ | grep -vw "0"

Please let me know if you face any issue, will be glad to help. Have a good day.