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

Lab Assignment_5-Solution (5)

Uploaded by

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

Lab Assignment_5-Solution (5)

Uploaded by

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

1st year of the first cycle Higher School of Computer Science

Course : Introduction to operating system 1 Sidi Bel Abbes (ESI-SBA)

Lab assignment N°5


FIND and GREP commands

Module 1 :

1. Use the script utility to capture your session.


Solution.
script Lab5

2. Search in your current directory for a file using its name (select a name of your choice, use a
default search). What do your remark ?
Solution.
find gx

We remark that the search is performed only in the current directory (i.e., in the first level of
the path).

3. Modify the previous command by adding the appropriate option, so that you avoid the default
search of find.
Solution.
find -name gx

4. Find all shell script (*.sh) files in your current directory.


Solution.
find -name ‘*.sh’

5. Search file called httpd.conf in all directories. What do you remark ?


Solution.
find / -type f -name httpd.conf

As we search in the root ‘/’, we observe that in some parts we have no access right to display
the file, and this is quite logic as we are not the root user, and also you are not navigating in our
home directory (in which we have the full access rights).

6. Avoid displaying results with access rights errors.


Solution.
find / -type f -name httpd.conf 2> /dev/null

7. Search for only directories and avoid all files.


Solution.
find . –type d

8. Search for all symbolic links and avoid what else.


Solution.
find . -type l

9. Search for two days or above modified files in ~/Downloads.


Solution.
find ~/Downloads -mtime +2

10. Search for ordinary files only in all directories, without displaying access rights errors, in which
their name starts with ‘pass’, their size is more than 1K and less than 4K, they are with 664
permissions and you are the owner.
Solution.
find / -type f -name ‘pass*’ –size +1k –size -4K -perm 664 -user user01 2>/dev/null

11. Repeat the previous command, so that you list only files and avoid all directories.
Solution.
find / ! -type d -name ‘pass*’ –size +1k –size -4K -perm 664 -user user01 2>/dev/null

12. Run ls -l command on all ‘*.c’ files in all directories to get extended information.
Solution.
Find / -name ‘*.c’ –type f –exec ls -l {} \;

13. Change the access rights of your files to 666 and your directories to 777 in your home directory.
Solution.
find . \(-type f-exec chmod 664 {} \; \) , \( -type d -exec chmod 775 {} \; \)

14. Exit out the script. Save the output file so you can continue with Module 2.
Solution.
exit

Module 2 :
1. Start script again and append to the existing output file of step 14.
Solution.
script -a lab5
2. Search for a given string of characters, such as games in your home directory. Modify the
command so that you figure out the line number.
Solution.
grep -r games ~
grep -rc games ~
3. In what follows, perform pattern research within /etc/passwd file :
 Search for a given pattern, such as sshd.
Solution.
grep sshd /etc/passwd

 Match the pattern root at the beginning of a line.


Solution.
grep ‘^root’ /etc/passwd

 Match the pattern sync anywhere on a line.


Solution.
grep sync /etc/passwd

 Match the pattern sync at the end of a line.


Solution.
grep ‘sync$’ /etc/passwd

 Match any character followed by a ‘y’ on a line.


Solution.
grep ‘.y’ /etc/passwd

 Display the lines those match either sshd, root or operator.


Solution.
grep -E ‘sshd|root|operator’ /etc/passwd

 Display the lines those match a, b or c at the beginning of a line.


Solution.
grep -E ‘^[abc]’ /etc/passwd
egrep ‘^[^d-z]’ /etc/passwd

 Display the lines those match either ‘uu’ or ‘uuu’.


Solution.
egrep ‘u\{2,3\}’ /etc/passwd
egrep ‘uu \|uuu’ /etc/passwd

 Display the lines those match either ‘nob’ or ‘non’.


Solution.
egrep ‘no(b|n)’ /etc/passwd

4. Exit out of script.


Solution.
exit

You might also like