You are on page 1of 6

Assignment 1

answers
Question 1:

If a files permissions is rw, and the directory which the file is in has r only,
you can perform ls on the directory and see all files name in the directory, but
you can not view the file, not edit the file, nor delete the file.
However, if the directory has x only, you would not be able to perform ls on
the directory, but you are still able to view, edit the file if you somehow knew
the name of the file, not delete the file.
Question 2:
a) What is the difference between ab.cd and ab*cd?
ab.cd will match a string with only one character between ab and cd.
Example: ab1cd will match
ab*cd will match a string when there is zero or more characters between ab and
cd.
Example: ab12345cd will match
b) what is the difference between ab.*cd and abb*cd ?

ab.*cd will accept for any string inbetween ab and cd.


Abb*cd will accept if ab is followed by b and then followed
by zero or any characters until cd.
c) What is the difference between ^abc and ^abc$?
^abc will accept all the characters matching abc at the start in the
given file
^abc$ will return if the file is starting with abc.
d) What is the difference between [abc]d and [^abc]d?
[abc]d will return all the characters with previous characters any of a or
b or c followed by d
[^abc]d will return all the characters with any other previous characters
excluding a or b or c followed by d
e) What is the difference between [A-Z][_a-zA-Z0-9]* and [A-Z_][a-zAZ0-9]*?
[A-Z][_a-zA-Z0-9]* First one will accept capital letters followed by
any digit and underscore.
[A-Z_][_a-zA-Z0-9]*? Only difference is it can accept even if a string
starts with underscore
Question 3:
a) Give the command-line of using find to search for all SUID
and SGID programs.
find $DIRECTORY -perm /u=s,g=s
b) Give the command-line of using find to list all the subdirectories in
the current directory
ls -Fl to list all the subdirectories in the current directory
c) Use find to produce a long ls listing of all filles in /usr/bin that are
more than 750Kb long. Give all the arguments and options in the
following command-line
find /usr/bin -size +750k exec ls -l {} \;

Question 4:
Create a big file (If you dont know how to create a big file,
try ls -l /usr/bin > bigfile). Make a copy of it using cp, and
call them big1 and big2.
a) The command-line to compress big1 using gzip.
Command gzip -c big1> big1.gz
b) The command-line to compress big2 using bzip2
Command bzip2 big2
c) The compression ratio of each compression method Using command diff
or zdiff to compare the compressed files and diff to compare the original
file.

d) Give the command-line of checking the file type of compressed files and
the original.
Command: file <original file> and file <compressed file>
e) The command-line to list top 10 lines of the content of big1.gz and
big2.bz2
Command: zcat big1.gz | head -10
Command: bzcat big2.bz2 | head -10

Question 5:
a) Use tar to create an archive (don't use the z or j option) of all the files
in the current directory.
Command: tar -cvf myfile.tar /home/CSC8421/

b) Compress the tar file with gzip.

c) View the contents of archive with gunzip and tar


gunzip -c files.tar.gz | tar tfv d) View without using gunzip but use the tar instead. (Hint: find the right
option to use from the man page).
Solution: tar -tzf files.tar.gz
e) Create a subdirectory of the current directory.
Solution: mkdir ./subdirectory_name
f) Use tar to unpack the archive into that directory at the current directory.
Solution: gunzip -zxf files.tar.gz -C suddir
Question 6:
This file is a text file.

> sed n "/the/p" file


Matches the substring the in the file. And prints the whole line that has got the
substring the in it.
p is for printing.

> sed -n "s/[A-Z]/&/gp" file


This command line will do all lines containing the upper case letters in the
file are replaced by itself [A-Z] is a Regular expression to match any strings
containing a upper case latter from A to Z in it.
& is the pattern itself.

> sed "32,45s/[()]//g" file


This command line removes all braces ( and ) from line 32 to line 45 in the
file.

[()] is a Regular expression to match any strings containing either ( or ).

> sed "/^$/d" file


This command line Removes all the blank lines in file.

^$ is a Regular expression to match a blank line.


^ is is to match begining of the line and $ is to match end of the line.

> sed "s/\([0-9]\)-\([0-9]\)/\1\2/g" file


This command line eliminate - in all strings that have digits connected by -
in the file.

\([0-9]\) is the first part of Regular expression , \([0-9]\) is the second part of
Regular expression . The regular expression is a pattern to any strings like s13t, 212-313, etc. \1 is what the first part of the Regular expression , \2 is the
second part. After the execution, the matched strings will be put back, but the
- is removed. For instance, string a1-3t is substituted by a13t, and 212-313
by 212313.

You might also like