0% found this document useful (0 votes)
61 views

Lesson 2 - Exploring Linux Command-Line Tools - Part 2 - 2-Đã M Khóa

The document provides instructions for exploring Linux command line tools including logging in, using grep to search files, manipulating streams and redirects, and other commands. It instructs the user to log in with a provided username and password, use grep to search the /etc/passwd file and another created file for various patterns, use redirection and pipes to count words and files, and replace text in a file using sed.

Uploaded by

makhanhson
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
61 views

Lesson 2 - Exploring Linux Command-Line Tools - Part 2 - 2-Đã M Khóa

The document provides instructions for exploring Linux command line tools including logging in, using grep to search files, manipulating streams and redirects, and other commands. It instructs the user to log in with a provided username and password, use grep to search the /etc/passwd file and another created file for various patterns, use redirection and pipes to count words and files, and replace text in a file using sed.

Uploaded by

makhanhson
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Exsercise 3: Exploring Linux Command-Line

tools (Cont.)
I. Log in to the system

1. Log in to the CentOS system with the username/password: student/lpic1@123

II. The grep Command

1. Find the line in the /etc/passwd file for user name that start with student.
2. Find all lines in the /etc/passwd file that begin with the letter st.
3. Find all lines in /etc/passwd that contain a digit 0-9.
4. Repeat the search in the previous instruction, but this time display only the number
of lines that contain the pattern.
5. Use the ps and grep commands to display the processes initiated by users other
than yourself.
6. Create a file with the content as follow, name it anything you want:

Fred apples 20
Susy oranges 5
Mark watermellons 12
Robert pears 4
Terry oranges 9
Lisa peaches 7
Susy oranges 12
Mark grapes 39
Anne mangoes 7
Greg pineapples 3
Oliver rockmellons 2
Betty limes 14

Do the below excercises with this file.

7. Find all the lines with string mellon


8. Find all the lines end with the character 2
9. Find all the people with the name begin with the letter from A to L
10. Count the number of lines that contain apple

III. Use streams, pipes and redirects

11. Using the cat command and redirection, create a file called junk containing a
few lines of text. Use <ctrl-d> at the beginning of a new line when you have
finished entering text and want to return the shell $ prompt.
12. Append more lines of text to the file you have created using the cat command
and redirection.
13. Using the ls command, list the files in your current directory. Make a note of the
number of files. _____

This document is created by Nguyen Hoang [email protected] Page 1


14. List the files in your current directory, but this time redirect the output to the file
temp.
15. Use the appropriate command to count the number of words in the temp file. Is
this the same count as in instruction 11? __________ If not, why
not?__________ Display the contents of temp. Remove the file.
16. This time use a pipe to count the number of files in your current directory. Was
the result what you expected this time? __________ Is it the same as in
instruction 11?
17. Display all the content of the file you created at excercise 6, but find and replace
all the oranges to apples
18. Using xargs and $() to generate the ls -l command to all the file inside your home
directory

This document is created by Nguyen Hoang [email protected] Page 2


Exsercise Instructions
I. Log in to the system

1. Log in to the system

Log int to the CentOS system with the user name and password provided:
student/lpic1@123

II. The grep Command

1. Find all lines in the /etc/passwd file for user names that start with student.

• $ grep student /etc/passwd

2. Find all lines in the /etc/passwd file that begin with the letter st.

• $ grep '^st' /etc/passwd

3. Find all lines in /etc/passwd that contain a digit 0-9.

• $ grep '[0-9]' /etc/passwd

4. Repeat the search in the previous instruction, but this time display only the
number of lines that contain the pattern.

• $ grep -c '[0-9]' /etc/passwd

5. Use the ps and grep commands to display the processes initiated by users
other than yourself.

• $ ps -ef | grep -v student

6. Create a file with the content as follow, name it anything you want:

Fred apples 20
Susy oranges 5
Mark watermellons 12
Robert pears 4
Terry oranges 9
Lisa peaches 7
Susy oranges 12
Mark grapes 39
Anne mangoes 7
Greg pineapples 3
Oliver rockmellons 2
Betty limes 14

$ vim <your file>

This document is created by Nguyen Hoang [email protected] Page 3


Input the above content
:wq!

Do the below excercises with this file.

7. Find all the lines with string mellon


$ grep ‘mellon’ <your file>

8. Find all the lines end with the character 2


$ grep ‘2$’ <your file>

9. Find all the people with the name begin with the letter from A to L
$ grep ‘^[A-L]’ <your file>

10. Count the number of lines that contain apple


$ grep -c ‘apple’ <your file>

III. Use streams, pipes and redirects

11. Using the cat command and redirection, create a file called junk containing a
few lines of text. Use <ctrl-d> at the beginning of a new line when you have
finished entering text and want to return the shell $ prompt.

• $ cat > junk


Type in several lines of junk for your file

<ctrl-d> on a new line to return to the shell prompt

12. Append more lines of text to the file you have created using the cat command
and redirection.

• $ cat >> junk (no spaces between the >>)

13. Using the ls command, list the files in your current directory. Make a note of
the number of files. _____

• $ ls

14. List the files in your current directory, but this time redirect the output to the
file temp.
• $ ls > temp
15. Use the appropriate command to count the number of words in the temp file.
Is this the same count as in instruction 11? __________ If not, why
not?__________ Display the contents of temp. Remove the file.

• $ wc -w temp
• $ cat temp
• $ rm temp

This document is created by Nguyen Hoang [email protected] Page 4


16. This time use a pipe to count the number of files in your current directory.
Was the result what you expected this time? __________ Is it the same as in
instruction 11? __________

• $ ls | wc -w

17. Display all the content of the file you created at excercise 6, but find and
replace all the oranges to apples
$ cat <your file> | sed ‘s/oranges/apples/g’

18. Using xargs and $() to generate the ls -l command to all the file inside your
home directory that have string te in the file name
$ ls | grep te | xargs ls -l
$ ls -l $(ls | grep te)

This document is created by Nguyen Hoang [email protected] Page 5

You might also like