0% found this document useful (0 votes)
4 views86 pages

MOS ASSIGNMENT Part 1

The document provides an extensive overview of basic and advanced commands in the Bash shell, including their functionalities and options. It covers commands for navigating directories, listing files, managing processes, and accessing help documentation. Additionally, it explains the use of environment variables and aliases, along with examples of how to create and utilize them effectively.

Uploaded by

s211021158
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views86 pages

MOS ASSIGNMENT Part 1

The document provides an extensive overview of basic and advanced commands in the Bash shell, including their functionalities and options. It covers commands for navigating directories, listing files, managing processes, and accessing help documentation. Additionally, it explains the use of environment variables and aliases, along with examples of how to create and utilize them effectively.

Uploaded by

s211021158
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 86

Part 1: Exploring the Bash Shell

1. The simplest Linux commands can be used without options.

a) Enter ls. What does this command do? Enter pwd. What does this command do?

- Command ls: used to list down the content details, including the hidden files. File
names in a simple list format. If no files, it will return nothing.
- Command pwd: Prints the current working directory.

b) Type cd .. . What does this command do? How does your prompt change? Type ls
again. How does this differ from part a?

- Command cd . . : Changes the directory one level up in the hierarchy


- Prompt Change: The directory in the terminal changes to the parent directory.
After type ls again:
- Difference: Will see the files from the parent directory instead of the previous
working directory.

c) Type each of these commands individually and briefly explain what each does.
hostname arch uname date
Now type all four of these instructions in one command, separating each by a
semicolon (;). Does the output differ as compared to executing each instruction
individually?

By typing individually :

· hostname: Outputs the name of the machine : LAPTOP-94F3S420


· arch: Shows the system architecture : x86_64
· uname: Displays information about the system : Linux
· date: Shows the current date and time : Mon Nov 18 19:49:59 +08 2024
Type all 4 in one command separating each by a semicolon (;):

-Executes all commands in sequence; their outputs appear consecutively. This is


efficient for combining tasks.

d) Enter whoami and then who. How do the two outputs differ?

Command whoami: Displays the current username logged into the terminal.
Command who: Lists all logged-in users, including login times and TTY (terminal
session) details.(it is not returning any output because there are no other users logged
into the system)
Difference: whoami is specific to your session; who includes everyone on the system.

e) Type cd ~/FILES followed by more addresses.txt and less addresses.txt. Type q


to exit from the display shown by the less command. Type more /etc/passwd and less
/etc/passwd. (may need to move to root directory to do this) How do the two
instructions, more and less, differ in terms of the output presented and exiting from
them?

Output for less addresses.txt:


Output for more /etc/passwd:

Output for less /etc/passwd:

· more: allows forward navigation only.


· less : supports both forward and backward navigation.

f) Type which arch, which ip, which systemd. What are the responses to each? Use
whereis to locate each of these three programs, as in whereis arch. How does the
output from whereis differ for arch? The executable, systemd, is stored in
/usr/lib/systemd. Why did which not find it?
 which arch: This command searches for the executable file arch in the directories
listed in the PATH environment variable. It typically returns the full path to the
executable.
 which ip: This command searches for the executable file ip in the PATH
environment variable.
 which systemd: This command might not find systemd because it's often not a
standalone executable. It's part of a larger systemd system.

· whereis arch: This command searches for the binary, source, and manual pages
related to arch.
· whereis ip: This command searches for the binary, source, and manual pages
related to ip.
· whereis systemd: This command might not find systemd directly as an executable,
but it might locate related files or directories.

why which didn't find systemd:

-systemd is a complex system that integrates various components. It's not a


standalone executable like arch or ip. Instead, it's a suite of tools and daemons that
work together to manage the system. The which command is designed to find
standalone executables in the PATH, so it's not suitable for finding systemd
components.

2. We will now explore commands that use options. Type cd ~/DUMMY-


DIRECTORY.

a. Type ls. Now type ls –l. How do the two outputs differ? List three things that ls -l
shows you that ls did not.

· ls: Lists files.


· ls -l: Provides detailed information like file permissions, owner, size, and
modification time.
· Key Differences in ls -l:

1. File permissions.
2. Ownership.
3. File size and modification time.
b. Type ps to view active processes. How many processes are listed? ps has many
options, some are specified without a hyphen. Try ps T. How does this output differ
from ps with no options? Three other options are a, u and x, which will list more
processes. Try each of ps a, ps u and ps x. Of these, ps, and ps T, which one provides
the most output per process (the most number of columns)? Redo that ps command
adding | more (or | less) to produce one screen’s worth of output so that you can see
the headers at the top of the output. What are the fields listed did not appear with ps T?

· ps: Lists active processes for the current shell session.


· ps T: Adds details for thread-level processes.
· Other Options:

 ps a: Includes processes from other users.


 ps u: Adds user details.
 ps x: Includes processes without a controlling terminal.

· Add | more to paginate the output.

Output for less:


Output for less:

Output for less:

Output for less:


Output for less:

Output Comparison:

 ps and ps T: The ps T command provides less output per process, typically


showing only the PID, TTY, TIME, and COMMAND fields.
 ps a, ps u, and ps x: These commands provide more detailed information per
process, including the USER, PID, %CPU, %MEM, VSZ, RSS, TTY, STAT,
START, and TIME fields.

4. To see what a command does (including its available options), view the
command’s man page.

a. What does man stand for?

-man provides manual pages (documentation) for commands.

b. Type man ps. Use the space bar or arrow keys to control moving up and down in
the man page and q to exit. What does this man page tell you about ps? Provide a
short (2-3 line) summary.
-The man ps command displays the manual page for the ps command. This manual
page provides detailed information about the ps command, including its syntax,
options, and usage.

A short summary of the ps command:

The `ps command displays information about a process or processes. It can be used to
view the current processes running on a system, their status, and resource usage.

c. Scroll down until you reach the options. Find the options for each of a, u, x and T.
What does each do? Note: do not look for the versions with hyphens like -a.

· a: List all active processes, including those not connected to a terminal.


· u: List user-oriented information about processes.
· x: List all processes, including those not connected to a terminal.
· T: List terminal processes.

Use man to explore each of these commands and answer the following questions:

d. ls: what does –a do? what does –r do? what does –R do?
· -a: List all files, including hidden files (those beginning with a dot).
· -r: Reverse the order of sort.
· -R: Recursively list subdirectories.

e. who: what does –b do? what does –H do?

-b: Show boot time.


-H: Show hostnames.

f. tar: what does this instruction do? there are a number of usages listed under
synopsis. What does the synopsis section tell us about using an instruction?
The tar command is used for archiving and extracting files. The synopsis section of
the man tar page provides examples of how to use the command, including the
various options and arguments that can be used.

The synopsis section typically shows the general syntax of the command, along with
examples of how to use it in different scenarios. It's a quick reference guide to the
command's usage.

5. There are other forms of help available besides man.

a. One form is the help command, but this only works on a few instructions. Type
man man. You will see that what the man page is for.a combination of all Bash-builtin
instructions (instructions defined in Bash rather than Linux). Scroll down until you
find the entry for alias. The Bash-builtins have help pages. Type help help. Which of
the two outputs, man or help, gave you more information!!?

Type help help:


 man man: Provides a comprehensive manual page for the man command,
explaining its syntax, options, and usage.
 help help: Provides a brief description of the help command, explaining its
basic function of displaying help information for shell builtins.

-In this case, man man provides significantly more detailed information.

b. Now type help man. What happens? Linux commands do not have help pages but
some have excerpts of man pages viewed using command -h (or --help). Try man -h |
less (or | more). Compare the output to that of man man (man’s man page).

Output man -h | less:

Output for man -h | more:


The help man command doesn't provide specific help for the man command itself,
as it's not a shell builtin. However, you can use the -h or --help option with many
Linux commands to get a brief usage summary : man -h | less

c. Use --help with each of cd, pwd, tar, who and whoami. Add | less (or | more) as
in cd --help | more so that you can view the entire output. If a command does not
respond to the --help option, you get a “usage” page instead. Which of these five
commands have a --help page?
· cd --help: Provides usage information for the cd command.
· pwd --help: Provides usage information for the pwd command.
· tar --help: Provides usage information for the tar command.
· who --help: Provides usage information for the who command.
· whoami --help: Does not provide a --help page.

-whoami does not have a --help page.

d. Another form of help is apropos. With this instruction, you list a string and it
compares this string to all of the man pages’ descriptions to see what instructions
might match the string. For instance, if we want to see what instructions deal with
virtual memory, we might use apropos “virtual memory”. Try each of apropos
virtual memory, apropos virtual, apropos memory and apropos “virtual
memory”. You will find the last one is the only one that returns just a few
instructions. What instructions does it return? With the quote marks, apropos attempts
to match the entire phrase and without it, it tries to match any of the keywords listed.
If there is only one word, we do not need quotes. Try apropos “memory virtual” and
you will find no matches. What response does apropos give you? Try apropos
manual. Which of the commands listed did you already know? In reading their brief
descriptions, which of the new commands might you feel useful?
· apropos virtual memory: Returns a list of commands related to virtual memory,
such as vmstat, free, and top.
· apropos virtual: Returns a larger list of commands, including those related to
virtual machines and virtualization.
· apropos memory: Returns a large list of commands related to memory
management, including free, top, and vmstat.
· apropos "virtual memory": Returns a more specific list of commands, focusing
on virtual memory, such as vmstat and free.
· apropos "memory virtual": No matches found.
· apropos manual: Returns a list of commands related to documentation and
manuals, including man, info, and apropos itself.

The history list allows you to recall previous commands.


e. Type history. How many commands are in your list?

-Total command in the list: 71 commands


f. Type !! which recalls the most recent command. It looks like nothing happened.
Why?

-If the most recent command didn't produce any visible output, it might appear that
nothing happened. However, the command was executed successfully.

g. To recall a previous instruction, type !# where # is the number in the list. Type !1.
What happened? If your history list has more items that can be stored, the first event
may no longer be on the history list. Type !n where n is the number of the last
instruction from 4d (should be immediately before history in the history list). What
happened?

-In this case, command number 70 was apropos manual. This command searches the
manual pages for any entries that match the keyword "manual". The output is a list of
commands related to manual pages, along with a brief description of each command's
purpose.
h. You can also type !str where str is a string of the beginning of the command.
Type !m. This should recall your last man command. Type !p and it should recall
your last ps command (from 2b). Type !h to recall the last help command from step
3h. What command is executed? What should you enter to re-execute the last help
command? (there are many answers, type the one that will be shortest).
· !m: This will recall the most recent command that starts with "m", which is likely
the last man command you executed.
· !p: This will recall the most recent command that starts with "p", which is likely the
last ps command.
· !h: This will recall the most recent command that starts with "h", which might be a
help command or another command starting with "h".

-To re-execute the last help command, the shortest way is to use !! .

e. Type history again. How much longer is your list now than it was in step 5a?

-Now the total command that have been executes is 76, since we type 5 more
commands from the previous one (71).
6. Variables store values. Environment variables are variables defined by a script but
the user can also define them.
a. To output all environment variables, enter the env command. What are the values
of each of these variables? PWD, HOME, USER, HOST, PS1. To see them all, you
might want to use env | less (or env | more).

· PWD: Present Working Directory - The current directory you are working in : home
· HOME: Home Directory - The default directory for your user account : /home/sham
· USER: Username - Your username on the system : sham
· HOST: Hostname - The name of the machine you are logged into
· PS1: Primary Prompt String - The default command prompt displayed by your shell.
b. To create a variable, type name=value where name is the variable name and value
is the value you want to store there. The value should either be a string, in which case
it can contain any characters desired, or an integer. If you use a string which contains
any spaces, enclose the string in either ‘’ or “”. We will use “”, at least for now.
Establish two variables, FIRST and LAST to store your first and last names. What
commands did you enter?

Enter command: FIRST="SITI"


LAST=”SHAMIRAH”

c. To output the value of a variable, use echo $VARNAME as in echo $HOME or


echo $FIRST. Write a single echo statement to output the values stored in FIRST and
LAST. What command did you use? What echo command would you use to output
your first name, last name, username, and home directory with one blank space
between each one.

Enter command: echo $FIRST $LAST


Enter command: echo $FIRST $LAST $USER $HOME

7. An alias is a substitution or shortcut for an instruction. We define an alias with an


assignment statement precede with the word alias, as in alias
shortcut=instruction. If the right hand side (after the =) has any spaces, it needs
to be placed in quote marks.

a. To see all defined aliases, type alias. How many aliases are there? The ones listed
primarily deal with changing the color of the output. What commands are aliased
other than those that establish the color as auto?

-There is 8 aliases
-The aliases that are not related to color customization:

alert: This alias seems to be a custom notification script that sends a notification with
a specified urgency level and message.
l: This alias is a shorthand for ls -CF, which lists files in a long format with color and
file type indicators.
la: This alias is a shorthand for ls -A, which lists all files, including hidden ones.
ll: This alias is a shorthand for ls -alF, which lists files in a long format with color,
file type indicators, and hidden files.
b. Define your own alias (the notation is alias name=command) called home which
performs a cd operation to return you to your home directory. After entering it, test it
out. What alias command did you enter? Enter an alias to perform a long listing of the
current directory, showing all files in reverse alphabetical order. Use man as needed
on ls. Test it out to see if it works correctly. What alias did you create?

Breakdown of the ll alias:

 ls: This is the basic command to list files and directories.


 -a: This option shows all files, including hidden ones.
 -l: This option shows a long listing format, including file permissions, owner,
group, size, modification date, and filename.
 -R: This option recursively lists subdirectories.

By combining these options, the ll alias provides a detailed and easily readable
directory listing.

c. The name given to the alias can include non-letters. For instance, define an alias, ..,
that will cd up one level (cd ..). Test it out. What command did you enter?

-Above command taken back to the parent directory.


Although we cannot define variables whose names start with characters like . or *, we
can define aliases. Define an alias * for the command cd /. Test it out to make sure it
works

-Above command taken to the root directory.

d. Enter an alias to output your current working directory, your username, the
architecture, and the date (remember that multiple instructions are separated with
semicolons). What alias did you enter?

· pwd: Prints the current working directory.


· whoami: Prints the current username.
· uname -m: Prints the system architecture.
· date: Prints the current date and time.

e. To remove a defined alias, type unalias name where name is the name of the alias.
Type unalias home. Type alias. Is the home alias listed? NOTE: you can recreate
your home alias if you want to keep it. Now try to unalias *. What happens? The
reason is that Bash thought * was a wildcard. Retry your unalias instruction using ‘*’
instead of *. Did it work? Type alias. What aliases are listed that you defined yourself?

-To remove the home alias, will use the following command:
To verify that the alias has been removed -> need to type alias again. The home alias
no longer be listed.
-This command attempts to remove all aliases, but it doesn't work as expected. The
reason is that * is a wildcard character in Bash, and it matches any string. So, unalias
* tries to remove all aliases, which might not be intended.

-This will specifically unalias the alias named "*".

-This will list all the currently defined aliases in the shell.

-Close your terminal window and shut down your VM-


Part 2 (G4)

Question 1 - The simplest Linux commands can be used without options.

a) The ~ symbol represents “home directory”. Type cd ~. Where are you? Type cd ~zappaf.
What does your prompt now say? Use pwd to obtain the full path to your location. Type cd
~foxr/HUMOR. What directory does your prompt show now?

● The ~username syntax attempts to navigate to the home directory of the specified user
(zappaf in this case).

b) Type cat bumperstickers to make sure it exists. Type cat b<tab> meaning type cat b
and then press the tab key. What happened? Type cat s<tab>. This emits a beep (if you
have speakers turned on) indicating that the interpreter could not uniquely identify a
file. Type cat s<tab><tab> (tab key twice). What happened? With cat s on the
command line, add m<tab>. What appears? Press <enter>.

1. Type cat bumperstickers to make sure it exists.

● The cat command outputs the content of a file to the terminal.


● If bumperstickers exists in the current directory, you’ll see its contents displayed.

2. Type cat b<tab> and observe what happens.

● The <tab> key is used for autocompletion. When you press it, the terminal completes
it to bumperstickers.
● If multiple files start with "b" (e.g., bumperstickers and blogposts), pressing <tab>
twice displays all matching files.

3. Type cat s<tab>.

● If no file starts with "s" in the directory, the terminal emits a beep (if sound is
enabled) or does nothing to indicate that it cannot uniquely identify a file.
● But in this cases, the terminal completes it to setup.sh
4. Type cat s<tab><tab> (press <tab> twice).

● Pressing <tab> twice after typing an ambiguous file name (s) shows all files starting
with "s" in the current directory.
● But since there is only one word with s, the terminal emits a beep from laptop’s
speaker.

5. With cat s on the command line, add m<tab>.

● This narrows down the search to files starting with "sm". If there is only one match
(e.g., smile.txt), it autocompletes to cat smile.txt

6. Press <enter>.

● The terminal runs the completed command (cat smile.txt) and displays the contents of
the specified file (Smiling through life.)

c) Type cd /usr/bin. Type cat c<tab><tab>. How did pressing <tab> twice differ from
in step 1b? You may answer y or n to this.

Yes, the output differs because /usr/bin has more files starting with "c" compared to
the earlier directory, which likely had fewer files or none.
d) Type cd ~/DUMMY-DIRECTORY. Type ls and then ls *.txt. Compare the results.

Comparison:

● ls: Lists all files and directories.


● ls *.txt: Filters files with the .txt extension only.

e) Type cd directory2. Type ls to view the two files. Type ls aa?.*. Why did aa1.abC
match but not aa10.bBC? Now, type cd ../directory3/c1. Type ls foo?. Why does foo
not appear but foo2 and foo3 do? Explain what the ? does.

The ? matches exactly one character. So:

● aa1.abC matches because aa? matches aa1.


● aa10.bBC does not match because aa? cannot match aa10 (two characters after aa).

The ? matches exactly one character.


foo does not appear because there are no characters after foo.
foo2 and foo3 appear because they have exactly one character after foo.
f) Return to DUMMY-DIRECTORY (..) and type ls *.[bc]*. Which files are listed?
Why did aaa.abc not appear? What does the [ ] indicate?

The [bc] matches one character that is either b or c. * matches zero or more
characters.

Why aaa.abc does not appear:

● The pattern matches files where the first character after the dot (.) is either b or c.
● aaa.abc does not match because it has a before b or c.

What [ ] indicates:

● [ ] is a character class.
● It matches any one character from the list inside the brackets.
Question 2 - Command Line Editing

a. In ~/DUMMY-DIRECTORY there are files named file1.txt – file4.txt. Let’s


rename them to end with .dat. The mv instruction (used to rename files) can
only rename one file at a time. Enter mv file1.txt file1.dat. The novice Linux
user would then retype this instruction 3 times changing the numbers each
time (from 1 to 2, etc). Command line editing will save you time and effort.
How? Type control+p. What does this do? Type control+b. What does this do?
Repeat this until you reach the “.” before the .dat. Hit the backspace key to
erase the “1”. Type 2. Now type escape+b (hit the escape key and then type b)
two times. What does escape+b do? Type control+b to move back to the
period and then backspace to erase the “1”. Type 2. Press <enter> to enter the
instruction. Repeat this operation to change the 2’s to 3’s. Repeat this again to
change the 3’s to 4’s. Did this get easier with each attempt?

Using control+p and control+b:

● control+p: This retrieves the previous command. It is equivalent to using the up


arrow key.
● control+b: Moves the cursor backward one character at a time. Use it to navigate the
command.

Renaming the rest:

● After renaming file1.txt to file1.dat, press control+p to bring back the previous
command.
● Use control+b repeatedly to move the cursor to the 1 in file1.
● Erase the 1 using the backspace key and replace it with 2, making it file2.dat.
● Press Enter to execute.

Using escape+b:

● escape+b moves the cursor backward by one word. This is faster than control+b for
moving multiple words.
● Use it to quickly jump to the filename part before editing.
b. To create an empty file, type touch filename. Use touch to create a_file.txt
using touch a_file.txt. Now, create a new file called b_file.dat using command
line editing. Explain the specific steps needed to perform the operation. Notes:
control+a moves you to the beginning of a line, control+e to the end, you do
not need to be at the end of the line to submit the changed instruction, just
press <enter> as soon as you are done.

● Use control+a to move to the beginning of the line.


● Use control+b or arrow keys to navigate to a_file.txt.
● Replace a_file.txt with b_file.dat:
○ Use backspace to delete "a_file".
○ Type "b_file.dat".
● Press Enter to execute.

Explanation of shortcuts:

● control+a: Moves the cursor to the beginning of the line.


● control+e: Moves the cursor to the end of the line.
● Editing directly within the line avoids typing the entire command again.

c. What do each of these do: control+k, control+y, escape+f, control+d,


escape+d. You might look these up in the textbook (table 2.13).

Shortcuts Actions

control+k Deletes (cuts) everything from the cursor to the end of the line.

control+y Pastes (yanks) the last thing that was cut using control+k.

escape+f Move the cursor forward by one word.

control+d Deletes the character under the cursor (like the delete key).

escape+d Deletes (cuts) the rest of the current word from the cursor onward.
Question 3 - A pipe ( | ) redirects the output of one program (command) to be the input to
another.

a. Type cd /etc and type ls. The files scroll past. We used less and more earlier to display
output one screen at a time. Type ls | more. What happens? More responds to the
space bar, <enter> and s and q. What do each of the space bars, enter, s and q do?
Type ls | less. What happens? The less command is controlled by the same keys plus
the arrow keys, <page_up>, <home>, etc. Experiment with less and summarise how it
differs from more. At the bottom of the screen to continue scrolling, what appears
when using more and what appears when using less?
1. Using more and less

○ Output: A long list of files and directories scrolls past your screen. This is
because ls outputs everything at once.
2. Type ls | more
■ The output stops after the first screen of text.
■ You can use the following keys to navigate:
■ Space bar: Scrolls forward one screen.
■ Enter: Scrolls forward one line.
■ s: Scrolls forward half a screen.
■ q: Exits more.
3. Type ls | less and compare to more
■ Similar to more, but can scroll backward using:
■ Arrow keys: Scroll up/down line by line.
■ Page Up/Page Down: Scroll by pages.
■ q: Exits less.
4. Comparison of more and less:
○ more: Can only scroll forward, displays --More-- at the bottom of the screen.
○ less: Can scroll forward and backward, displays current line number and
percentage viewed (e.g., line 1-20/200 (10%)).
b. Type ls | sort –r | less. What does this command do?

ls: Lists files and directories.

sort -r: Sorts the list in reverse alphabetical order.

less: Displays the sorted output for easier navigation.

c. Change directory to ~zappaf/ZAPPA. Type the following command cat c* | grep “f$” | sort.
Explain what each part of this instruction does. The $ in “f$” says “match an ‘f’ that ends the
line”.

cat c*: Combines and displays the contents of all files starting with c.

grep "f$": Filters lines where the last character is f (using $ to match the end of the line).

sort: Sorts the filtered lines alphabetically.


d. cd back to /etc. The command wc filename will output the number of characters (bytes),
words and lines in filename. We can combine ls and wc to count the number of files. For
instance, ls | wc –l will count the number of lines output by ls, or the number of files. Try it.
Now, use ls and a pipe to wc to find out how many files in /etc start with the letter ‘c’. How
many did you find? What command did you enter?

Found 71 files starting with the letter c with ls c* | wc -l command.


Question 4 - File redirection is used to alter the input/output of a command.

a. While in /etc, type ls > ~/etc-files.txt. This command performs an ls and sends the
output to the file etc-files.txt rather than to the window. Why is the ~/ necessary?
HINT: try it without the ~/.

~/ is shorthand for the user's home directory. Without ~/, the file will be created in the current
working directory (/etc in this case).

If omit ~/, the system tries to write etc-files.txt into /etc, but regular users typically don't have
permission to write in system directories like /etc. Using ~/ ensures the file is written in the
user's home directory, where the user has proper permissions.

b. Change directory to /usr/bin. Type ls >> ~/etc-files.txt. cd to your home directory and
type less etc-files.txt and examine ~/etc-files.txt. Explain the difference between >
and >>.

Difference between > and >>:

● > overwrites the file's content with new output.


● >> appends new output to the existing content of the file.
Explanation of Steps:

● The first command (ls > ~/etc-files.txt) overwrites the file with the ls output of /etc.
● The second command (ls >> ~/etc-files.txt) appends the ls output of /usr/bin to the
same file.
● After examining the file with less, you'll see that it contains both the /etc and /usr/bin
directory listings.

c. Most Linux commands obtain input from files rather than keyboard so there is little
need for the < redirection. Let’s see. Type less ~/etc-files.txt followed by type less <
~/etc-files.txt. Was there any difference between using < and not using it?

There is no difference. Both commands display the file content using less. The < is optional
because less naturally expects a file input. The < explicitly redirects the file to the less
command, but it doesn’t change the behaviour.
d. Now let’s try <<. This works as command << string where command then obtains
input from keyboard until you type string. Type wc << quit. Now type the following:
Hello there<enter>, how are you today?<enter>, quit<enter>. What was the output of
the command? Now try wc << <enter> and type in some sentences. How do we end
the input since <enter> does not? Type control+d.

wc counts the lines, words, and characters from the keyboard input.
The input stops when you type the word quit on a new line.
Output:

● 2 = number of lines.
● 6 = number of words.
● 31 = number of characters (including spaces and newlines).

Lastly, end input with Control+D, which signals the end-of-file (EOF).

e. Let’s combine << and > to create a text file using cat. From your home directory, type
cat << quit > list.txt. Now enter any list of items (e.g., grocery items, band names) but
unsorted, and type quit to end. What output are you given? Type less list.txt. You
should see your list. Type the command cat << quit | sort > list2.txt. Enter the same
list. Type less list2.txt. How does this version of the file differ? Explain each part of
the command you entered.

No immediate output on the terminal. Content saved in list.txt.


<< quit: Takes input from the keyboard until quit is typed.
| sort: Pipes the input to the sort command, sorting the items alphabetically.
> list2.txt: Redirects the sorted output into list2.txt.

f. We cannot use << and > without a word in between. Try cat << > list2.txt. What
response do you get?

The << operator expects a delimiter word (e.g., quit) to signify the end of input.
Omitting the word causes a syntax error, as the shell cannot parse the command.
Question 5 - If you enter a variable or alias, it only lasts during this session. You can instead
define these in one of several files so that they are available in any and every shell.

a. First, let’s do an experiment. Type X=0 <enter>. Type echo $X. Now type bash. This
starts a subshell (or inner session). Type echo $X. What response do you get? Why?
Type exit to resume your outer session and type echo $X. What did you get this time?

Output: 0
The variable X is set to 0, and echo $X confirms the value.

Output: Blank (nothing is displayed).


This is because X is a local variable. When start a new Bash subshell, local variables from the
parent shell are not inherited.

b. We can make a variable accessible in other shells by exporting it. Type export X. Type
bash and from the inner session, type echo $X. What response did you get now? Type
X=1 to change X’s value. Type exit and echo $X. What value is X in this outer shell?
Why? Type export to list all exported variables in this shell. All but one of them are
environment variables defined in various scripts; one is X. It should say X=“1”. Type
exit to return to your outer session and type export. You will find in this list X=“0”
instead.
Exporting the variable makes it accessible in the subshell. When start a new Bash session and
type echo $X, the value is now inherited.

Changing the value of X in the subshell (X=1) does not affect the value in the parent shell.
When exit the subshell, the parent shell's value (0) remains unchanged.

Output: A list of all exported variables, including X="1" in the subshell and X="0" in the
parent shell. This illustrates that exported variables are isolated to the shell session where
they are set.

c. Type cd ~ to return to your home directory if you are not already there. Type cat
.bashrc. This script runs every time you start a new Bash session. It performs only
three operations: if /etc/bashrc exists, it runs that script; it adds two directories to your
PATH variable and it exports PATH. If you explore /etc/bashrc, you will find that that
script creates other environment variables like PROMPT_COMMAND, PS1 and
PATH. If you want to alter the values of your PS1 or PATH variables, would you do
so from the command line, from /etc/bashrc or ~/.bashrc? Why?

.bashrc is a shell script executed every time a new Bash session is started. It is used to
configure environment variables and shell preferences for interactive sessions.

Editing .bashrc or /etc/bashrc:

● .bashrc is user-specific and located in the home directory. Use it to configure


variables like PS1 (prompt style) or PATH for your user.
● /etc/bashrc is global and affects all users. Only administrators should modify this file.

Why use .bashrc?


Since .bashrc is user-specific, changes here do not affect other users and are safer for personal
configurations.
d. In your home directory, look at the contents of the file ~/.bash_profile. What does this
script do that .bashrc did not?

.bash_profile is executed when log in (login shells). It is used for login-specific settings,
whereas .bashrc is for interactive shell settings.

e. In comparing the two files (.bashrc, .bash_profile), there is a comment at the bottom
of each that explains what you might use the file for. What should you use .bashrc for
and what should you use .bash_profile for?

.bashrc is used for interactive shell sessions and contains configurations like aliases, shell
functions, and custom prompts. This file is executed every time a new terminal is opened in a
non-login session.

.bash_profile is used for login shells and is executed only once per login session. It is
typically used to initialise environment variables and run commands or scripts that should
only be set once per session.

The comments in these files guide users to use .bashrc for interactive customizations and
.bash_profile for session initialization.
f. While in your home directory, type echo “alias me=‘ps aux | grep Student’” >>
.bashrc. This instruction is an echo statement to output the text “alias me=‘ps aux |
grep Student’” and redirect the output to the file .bashrc. View your .bashrc file and
you should find this entry at the bottom of the file. Thus, you have added an alias to
the file. You would more conveniently edit .bashrc (or .bash_profile) using vi, but you
haven’t learned vi yet (it’s the topic of the next part). Explain what the alias does.
Type me. Why did it not work? Even though we’ve changed the .bashrc file, we need
our Bash session to re-read this file. To do so, type source .bashrc. Type me What
happens this time? Type alias to see that me has been defined. What does the source
do?

- Adding an alias to .bashrc makes it permanent for future sessions. Use source to apply
.bashrc changes immediately in the current session.

- Adds the alias me to the .bashrc file. The alias runs the command ps aux | grep
Student, which lists processes related to the user "Student."

Output: Command not found.

- Even though the alias is added to .bashrc, it is not immediately available because
.bashrc is only executed when a new shell is started.
- source (source .bashrc) forces the current shell to re-read the .bashrc file, making the
alias active. After that, Output displays processes matching "Student."
- Lists all defined aliases in the current session, confirming that me is now active.
‭Part 3 (G4)‬

‭QUESTION 1‬

‭1)‬ ‭a)‬

‭The cd etc command navigates to the /etc directory‬

‭The cd sysconfig moves to the /etc/sysconfig directory‬

‭The cd networking takes to the /etc/sysconfig/networking directory.‬


‭The command cd ~ changes the current directory to the user's home directory.‬

‭The command cd ~Student changes the current directory to the home directory of the user‬
‭Student, which is /home/Student.‬

‭b)‬

‭-‬ ‭sudo mkdir /home/Student/FILES/DUMMY-DIRECTORY‬


‭-‬ ‭cd DUMMY-DIRECTORY‬
‭-‬ ‭/home/Student/FILES/DUMMY-DIRECTORY‬
‭c)‬

‭cd /home/Student/FILES/DUMMY-DIRECTORY‬

‭d)‬

‭The command wc is typically located in /usr/bin/, while the command ip is usually found in‬
‭/usr/sbin/.‬

‭e)‬

‭This indicates that the systemd executable is already located in the /usr/bin directory, which is‬
‭included in the PATH by default. This means that modifying the PATH variable in this case is‬
‭unnecessary, as the system can already find systemd.‬
‭Question 2‬

‭A.‬ ‭cd‬ ‭to‬ ‭your‬ ‭home‬ ‭directory.‬ ‭Type‬ ‭touch‬ ‭test1.txt‬ ‭test2.txt‬ ‭test3.txt‬ ‭test4.txt‬ ‭to‬ ‭create‬
‭these‬‭four‬‭files.‬‭Type‬‭ls‬‭–l.‬‭The‬‭fifth‬‭item‬‭in‬‭the‬‭long‬‭listing‬‭is‬‭the‬‭file’s‬‭size.‬‭What‬‭are‬
‭the sizes for these four files?‬

‭The‬‭fifth column‬‭indicates the file size:‬


‭●‬ ‭All four files (‬‭test1.txt‬‭,‬‭test2.txt‬‭,‬‭test3.txt‬‭,‬‭test4.txt‬‭) have a size of‬‭0 bytes‬‭.‬
‭●‬ ‭The‬‭touch‬‭command does not write any content into the files; it merely creates them‬
‭or updates their metadata. Hence, the files are empty, resulting in a size of‬‭0 bytes‬‭.‬

‭B.‬ ‭cd‬ ‭to‬ ‭~Student.‬‭To‬‭copy‬‭a‬‭file,‬‭use‬‭cp‬‭currentfile‬‭newfile.‬‭Type‬‭ls‬‭FILES.‬‭Let’s‬‭copy‬


‭sales.txt‬ ‭to‬ ‭your‬ ‭home‬ ‭directory.‬‭The‬‭command‬‭is‬‭cp‬‭FILES/sales.txt‬‭~‬‭Give‬‭it‬‭a‬‭try.‬
‭Did‬ ‭it‬ ‭work?‬ ‭If‬ ‭you‬ ‭did‬ ‭not‬ ‭get‬ ‭an‬ ‭error‬ ‭message,‬ ‭it‬ ‭worked,‬ ‭but‬ ‭you‬ ‭can‬ ‭test‬‭it‬‭by‬
‭Page‬ ‭|‬ ‭8‬ ‭typing‬ ‭ls‬ ‭~‬ ‭to‬ ‭view‬ ‭the‬ ‭files‬‭in‬‭your‬‭home‬‭directory.‬‭We‬‭can‬‭copy‬‭multiple‬
‭files‬‭at‬‭a‬‭time‬‭using‬‭wildcards.‬‭Type‬‭cp‬‭FILES/*.txt‬‭~‬‭Make‬‭sure‬‭this‬‭worked‬‭as‬‭well.‬
‭One‬ ‭of‬ ‭Student’s‬ ‭directories‬ ‭is‬ ‭DUMMY-DIRECTORY‬ ‭which‬ ‭has‬ ‭a‬ ‭subdirectory‬
‭called‬‭directory2‬‭which‬‭contains‬‭two‬‭files.‬‭We‬‭will‬‭copy‬‭these‬‭to‬‭your‬‭home‬‭directory,‬
‭but‬‭do‬‭so‬‭from‬‭your‬‭home‬‭directory.‬‭Type‬‭cd‬‭~.‬‭To‬‭indicate‬‭that‬‭files‬‭should‬‭be‬‭copied‬
‭here‬ ‭(your‬ ‭current‬ ‭directory),‬ ‭the‬ ‭destination‬ ‭directory‬ ‭is‬ ‭denoted‬ ‭using‬ ‭.‬ ‭(period).‬
‭Enter‬‭a‬‭cp‬‭command‬‭to‬‭copy‬‭the‬‭files‬‭in‬‭Student’s‬‭DUMMY-DIRECTORY/directory2‬
‭to your current directory. What command did you come up with?‬
‭C.‬ ‭From‬ ‭your‬ ‭home‬ ‭directory,‬ ‭type‬ ‭echo‬ ‭hi‬‭>>‬‭aa1.abC.‬‭This‬‭will‬‭add‬‭the‬‭word‬‭“hi”‬‭to‬
‭this‬‭empty‬‭file.‬‭Type‬‭ls‬‭–l‬‭and‬‭you‬‭will‬‭see‬‭that‬‭aa10.bBC‬‭is‬‭still‬‭empty‬‭while‬‭aa1.abC‬
‭has‬‭a‬‭size‬‭of‬‭3.‬‭Repeat‬‭the‬‭last‬‭cp‬‭command‬‭from‬‭step‬‭2b‬‭which‬‭copied‬‭those‬‭two‬‭files‬
‭to‬ ‭this‬ ‭directory‬ ‭but‬ ‭add‬ ‭the‬ ‭option‬ ‭–i‬ ‭(interactive‬ ‭mode).‬ ‭This‬ ‭option‬ ‭forces‬ ‭cp‬ ‭to‬
‭pause‬ ‭and‬ ‭ask‬ ‭permission‬ ‭to‬ ‭copy‬ ‭a‬ ‭file‬ ‭if‬ ‭that‬ ‭file‬ ‭already‬ ‭exists‬ ‭in‬ ‭the‬ ‭current‬
‭directory.‬‭Answer‬‭n‬‭when‬‭asked.‬‭Repeat‬‭the‬‭ls‬‭–l‬‭command‬‭and‬‭you‬‭will‬‭see‬‭nothing‬
‭changed.‬‭Repeat‬‭the‬‭cp‬‭–i‬‭command‬‭but‬‭this‬‭to‬‭answer‬‭y.‬‭Repeat‬‭ls‬‭–l‬‭and‬‭you‬‭will‬‭see‬
‭that‬ ‭aa1.abC‬ ‭is‬ ‭back‬ ‭to‬ ‭a‬ ‭size‬ ‭of‬ ‭0.‬ ‭Repeat‬ ‭the‬ ‭cp‬ ‭command‬ ‭without‬ ‭the‬ ‭–i‬ ‭option.‬
‭What happens? Explain why and when you should use –i with cp.‬
‭D.‬ ‭The‬ ‭option‬ ‭–r‬ ‭performs‬ ‭a‬ ‭recursive‬ ‭copy.‬ ‭Type‬ ‭the‬ ‭command‬ ‭cp‬ ‭–r‬
‭~Student/DUMMY-DIRECTORY/directory3‬ ‭temp.‬ ‭This‬ ‭instruction‬ ‭will‬ ‭recursively‬
‭copy‬‭everything‬‭in‬‭directory3,‬‭including‬‭subdirectories‬‭and‬‭their‬‭contents,‬‭into‬‭a‬‭new‬
‭directory‬ ‭called‬ ‭temp,‬ ‭stored‬ ‭in‬ ‭your‬ ‭home‬ ‭directory.‬ ‭You‬ ‭will‬ ‭receive‬ ‭a‬ ‭few‬
‭permission‬ ‭denied‬ ‭errors‬ ‭of‬ ‭items‬‭that‬‭you‬‭do‬‭not‬‭have‬‭access‬‭to‬‭copy,‬‭ignore‬‭those.‬
‭Type‬ ‭ls‬ ‭~Student/DUMMY-DIRECTORY/directory3‬ ‭and‬ ‭then‬ ‭ls‬ ‭temp‬ ‭and‬ ‭compare‬
‭what you find. In your own words, what does recursive mean?‬
‭E.‬ ‭The‬ ‭mv‬ ‭command‬ ‭moves‬ ‭files‬ ‭from‬ ‭one‬ ‭location‬ ‭to‬ ‭another‬‭or‬‭renames‬‭a‬‭file.‬‭With‬
‭the‬ ‭temp‬ ‭directory‬ ‭created‬ ‭from‬ ‭2d,‬ ‭cd‬ ‭into‬ ‭temp‬ ‭and‬ ‭type‬ ‭mv‬
‭DUMMY-DIRECTORY‬‭temp2.‬‭What‬‭does‬‭this‬‭instruction‬‭do?‬‭Is‬‭temp2‬‭there?‬‭What‬
‭about‬‭DUMMY-DIRECTORY?‬‭Type‬‭mv‬‭temp2‬‭...‬‭Type‬‭ls‬‭and‬‭then‬‭cd‬‭~‬‭and‬‭ls.‬‭What‬
‭did‬ ‭this‬ ‭second‬ ‭mv‬ ‭command‬ ‭do?‬ ‭In‬ ‭the‬ ‭first‬ ‭case,‬ ‭we‬ ‭used‬ ‭mv‬ ‭to‬ ‭rename‬ ‭the‬
‭directory and the second, we moved the directory to a new location.‬
‭F.‬ ‭In‬ ‭one‬ ‭cd‬ ‭command,‬ ‭move‬ ‭to‬ ‭temp2‬ ‭(up‬ ‭1‬ ‭level‬ ‭and‬ ‭then‬ ‭down‬ ‭into‬ ‭temp2).‬ ‭What‬
‭command‬‭did‬‭you‬‭issue?‬‭Type‬‭ls‬‭and‬‭you‬‭should‬‭find‬‭4‬‭files‬‭ending‬‭in‬‭.dat‬‭(one‬‭file,‬
‭file3.dat‬ ‭is‬ ‭missing‬ ‭since‬ ‭you‬ ‭did‬ ‭not‬ ‭have‬ ‭permissions‬ ‭to‬ ‭copy‬ ‭it‬ ‭in‬ ‭step‬‭2c).‬‭Let’s‬
‭assume‬ ‭we‬‭want‬‭to‬‭change‬‭these‬‭file‬‭names‬‭back‬‭to‬‭end‬‭in‬‭.txt.‬‭Enter‬‭mv‬‭*.dat‬‭*.txt.‬
‭What‬‭happened?‬‭Now‬‭type‬‭mv‬‭file1.dat‬‭file1.txt.‬‭This‬‭version‬‭works‬‭by‬‭renaming‬‭one‬
‭file‬ ‭so‬ ‭why‬ ‭didn’t‬ ‭mv‬ ‭work‬ ‭using‬ ‭the‬ ‭*‬ ‭to‬ ‭rename‬ ‭all‬ ‭3‬ ‭files?‬‭Using‬‭command‬‭line‬
‭editing,‬ ‭repeat‬ ‭this‬ ‭last‬‭mv‬‭command‬‭3‬‭times‬‭to‬‭rename‬‭the‬‭other‬‭3‬‭files‬‭to‬‭have‬‭.txt‬
‭extensions‬
‭G.‬ ‭In‬ ‭step‬ ‭2b,‬ ‭you‬ ‭copied‬ ‭sales.txt‬ ‭to‬ ‭your‬ ‭home‬ ‭directory.‬ ‭Type‬ ‭cd‬ ‭..‬ ‭followed‬ ‭by‬ ‭rm‬
‭sales.txt.‬‭Type‬‭ls‬‭to‬‭see‬‭if‬‭the‬‭file‬‭was‬‭deleted.‬‭Move‬‭back‬‭into‬‭temp2‬‭where‬‭there‬‭are‬
‭several‬ ‭.dat‬ ‭files.‬ ‭Enter‬ ‭a‬ ‭command‬ ‭to‬ ‭delete‬ ‭these‬ ‭.dat‬ ‭files‬ ‭using‬ ‭a‬ ‭single‬ ‭rm‬
‭instruction‬ ‭and‬‭appropriate‬‭wildcards‬‭(only‬‭delete‬‭the‬‭.dat‬‭files).‬‭What‬‭command‬‭did‬
‭you enter?‬
‭/home/farahrosl/temp2‬

‭H.‬ ‭As‬‭with‬‭cp,‬‭rm‬‭has‬‭a‬‭–i‬‭(interactive)‬‭option‬‭for‬‭safety.‬‭While‬‭still‬‭in‬‭temp2,‬‭type‬‭rm‬‭–i‬
‭*.txt.‬ ‭Answer‬ ‭n‬ ‭when‬ ‭prompted.‬ ‭How‬ ‭could‬‭you‬‭ensure‬‭that‬‭whenever‬‭you‬‭type‬‭rm,‬
‭the –i option is used without having to necessarily type rm –i?‬

‭I.‬ ‭One‬‭subdirectory‬‭of‬‭DUMMY-DIRECTORY‬‭(which‬‭is‬‭now‬‭temp2‬‭in‬‭your‬‭directory)‬
‭is‬‭dir5.‬‭This‬‭is‬‭an‬‭empty‬‭directory.‬‭Type‬‭rm‬‭dir5.‬‭What‬‭happened?‬‭You‬‭cannot‬‭delete‬‭a‬
‭directory‬ ‭using‬‭rm.‬‭Instead,‬‭you‬‭can‬‭delete‬‭it‬‭using‬‭rmdir.‬‭Retry‬‭the‬‭delete‬‭command‬
‭using‬‭rmdir.‬‭What‬‭command‬‭did‬‭you‬‭use?‬‭Did‬‭it‬‭work?‬‭Now‬‭try‬‭to‬‭delete‬‭the‬‭directory‬
‭directory2.‬ ‭What‬ ‭response‬ ‭did‬ ‭you‬ ‭get?‬ ‭rmdir‬ ‭will‬ ‭only‬‭delete‬‭empty‬‭directories.‬‭cd‬
‭into‬‭directory2‬‭and‬‭type‬‭rm‬‭*.‬‭This‬‭will‬‭delete‬‭all‬‭files.‬‭Now‬‭cd‬‭up‬‭1‬‭level‬‭and‬‭repeat‬
‭the‬ ‭rmdir‬ ‭command.‬ ‭Did‬ ‭it‬ ‭work?‬ ‭Another‬ ‭way‬‭to‬‭delete‬‭the‬‭contents‬‭of‬‭a‬‭directory‬
‭and‬‭then‬‭the‬‭directory‬‭itself‬‭is‬‭to‬‭use‬‭rm‬‭–r‬‭directory.‬‭Let’s‬‭try‬‭this‬‭on‬‭temp2‬‭itself.‬‭cd‬
‭up 1 level to ~ and type rm –r temp2. Type ls and explain what the rm command did.‬

‭J.‬ ‭The‬‭opposite‬‭of‬‭rmdir‬‭is‬‭mkdir,‬‭to‬‭create‬‭a‬‭new‬‭directory.‬‭This‬‭is‬‭somewhat‬‭like‬‭touch,‬
‭it‬ ‭creates‬ ‭an‬ ‭initially‬ ‭empty‬ ‭directory.‬ ‭From‬ ‭your‬ ‭home‬ ‭directory,‬ ‭type‬ ‭mkdir‬ ‭new.‬
‭Type‬ ‭ls‬ ‭–l.‬ ‭What‬ ‭size‬ ‭is‬‭this‬‭directory‬‭(even‬‭though‬‭it‬‭is‬‭empty)?‬‭Do‬‭its‬‭permissions‬
‭(the‬ ‭first‬ ‭10‬ ‭characters‬ ‭of‬ ‭the‬ ‭line)‬ ‭match‬ ‭the‬ ‭other‬ ‭directories?‬ ‭If‬ ‭not,‬ ‭how‬ ‭does‬ ‭it‬
‭differ?‬

‭K.‬ ‭The‬ ‭files‬ ‭aa10.bBC,‬ ‭aa1.abC,‬ ‭and‬ ‭3‬ ‭.txt‬ ‭files‬ ‭were‬ ‭all‬ ‭copied‬ ‭from‬ ‭Student.‬ ‭Use‬ ‭a‬
‭single‬‭mv‬‭command‬‭with‬‭wildcards‬‭to‬‭move‬‭them‬‭all‬‭into‬‭new.‬‭Try‬‭your‬‭best‬‭to‬‭write‬
‭a‬ ‭command‬ ‭that‬ ‭only‬ ‭lists‬ ‭two‬ ‭items,‬ ‭one‬ ‭combining‬ ‭aa10.bBC‬ ‭and‬ ‭aa1.abC‬‭with‬‭a‬
‭wildcard, and one that references the 3 .txt files). What command did you use?‬
‭QUESTION 3‬

‭3. a)‬
‭The outputs of the commands differ as follows:‬

‭●‬ ‭cat passwd shows the entire file, providing a full list of all user and system accounts.‬
‭●‬ ‭head passwd limits the output to the first 10 lines, showing the beginning of the file,‬
‭primarily system accounts.‬
‭●‬ ‭tail passwd limits the output to the last 10 lines, displaying the most recently added‬
‭accounts or users.‬

‭b)‬

‭man head‬
‭man tail‬

‭What does this command do?‬

‭●‬ ‭tail -c 100 passwd: Outputs the last 100 bytes of the passwd file. This includes any‬
‭characters, spaces, and newline characters, starting from the end of the file.‬
‭●‬ ‭head -n 5 passwd: Outputs the first 5 lines of the passwd file, giving the details of the‬
‭first few accounts.‬
‭c)‬
‭Explain what –n -10 and –c +100 do in these cases.‬

‭●‬ ‭head -n -10: Displays all lines except the last 10 lines.‬
‭●‬ ‭tail -c +100: Displays the content of the file starting from the 100th byte and includes‬
‭everything after.‬
‭d)‬

‭e)‬

‭What types of entities do you find?‬

‭-Using file a* in the /etc directory, the following types of entities were identified:‬

‭1.‬ ‭Text Files‬


‭○‬ ‭Example: adduser.conf identified as "ASCII text".‬
‭2.‬ ‭Directories‬
‭○‬ ‭Examples: alternatives, apparmor, apparmor.d, apport, apt.‬
‭-Using file -i a*, the entities were described in terms of their MIME types:‬

‭1.‬ ‭Text Files‬


‭○‬ ‭Example: adduser.conf identified as text/plain; charset=us-ascii. This shows it‬
‭is a plain text file encoded in ASCII.‬
‭2.‬ ‭Directories‬
‭○‬ ‭Examples: alternatives, apparmor, apparmor.d, apport, apt identified as‬
‭inode/directory; charset=binary. This describes them as directories (inode‬
‭type) with binary content, indicating they are not text-based.‬

‭Differences Between file and file -i‬

‭1.‬ ‭Without -i:‬


‭○‬ ‭Provides a human-readable description of the entity (e.g., "ASCII text",‬
‭"directory").‬
‭○‬ ‭Focuses on presenting the information in simple terms.‬
‭2.‬ ‭With -i:‬
‭○‬ ‭Provides the MIME type and character encoding of the entity.‬
‭○‬ ‭Gives a technical breakdown, such as text/plain for text files and‬
‭inode/directory for directories.‬
‭QUESTION 4‬
‭4. a)‬

‭Read diff’s man page and then explain precisely what the output from the diff‬
‭command means.‬

‭1.‬ ‭2c2: This means that line 2 in temp1.txt (the file on the left side) is changed to line 2‬
‭in temp2.txt (the file on the right side).‬
‭○‬ ‭2 indicates the line number where the change is happening (in this case, line‬
‭2).‬
‭○‬ ‭c means that the line is changed in temp2.txt compared to temp1.txt.‬
‭○‬ ‭The part after the c indicates the same line number in temp2.txt.‬
‭○‬ ‭< Bob is the line from temp1.txt, which has "Bob".‬
‭○‬ ‭> Bod is the line from temp2.txt, which has "Bod" (the misspelled name).‬

‭2.‬ ‭4d3: This means that line 4 in temp1.txt is deleted in temp2.txt.‬


‭○‬ ‭4 indicates the line number in temp1.txt where the deletion occurs.‬
‭○‬ ‭d means delete — the line is present in temp1.txt but missing in temp2.txt.‬
‭○‬ ‭The part after the d indicates that line 3 in temp2.txt does not contain any‬
‭corresponding line from temp1.txt.‬
‭○‬ ‭< Dave is the line from temp1.txt that has been deleted in temp2.txt.‬

‭Key Symbols in diff:‬

‭●‬ ‭<: Shows the line from the first file (temp1.txt) in the comparison.‬
‭●‬ ‭>: Shows the line from the second file (temp2.txt) in the comparison.‬
‭●‬ ‭c: Indicates a change — this means the content of the line in the first file is different‬
‭from the second file.‬
‭●‬ ‭d: Indicates a deletion — the line exists in the first file but not in the second.‬
‭●‬ ‭a: Would indicate an addition — the line exists in the second file but not in the first.‬
‭b)‬

‭Explain what the join command does‬

‭The join command is used to combine lines from two files based on matching values in a‬
‭specific field (default is the first field). It only joins lines where the fields match in both files.‬
‭By default, it uses the first space or tab-separated field as the key to match the lines.‬

‭Difference Between join and paste:‬

‭●‬ ‭JOIN : Combines files based on matching fields (by default, the first field), and only‬
‭shows lines where the fields match in both files.‬
‭○‬ ‭In your case, because both files have matching numbers (1-5), join combines‬
‭them based on these matching fields.‬
‭●‬ ‭PASTE : Combines the lines from two files side by side without any matching‬
‭requirement. It simply appends the content of the second file to the content of the first‬
‭file.‬
‭○‬ ‭In your case, it simply displays the names from both files on the same line,‬
‭separated by a tab.‬

‭c)‬

‭Differences in Output‬

‭1.‬ ‭Output of paste:‬


‭●‬ ‭The output from the paste command using the original files (temp3.txt and‬
‭temp4.txt) includes both the names and their corresponding numbers, resulting‬
‭in a format that looks like this: 1. Frank 1. Zappa, which maintains the original‬
‭numbering. In contrast, the output from the paste command on the new files‬
‭(names_first.txt and names_last.txt) is cleaner and simpler, showing only the‬
‭names side by side without any numbers, like this: Frank Zappa.‬
‭2.‬ ‭Output of join:‬
‭●‬ ‭The join command applied to the original files produces output that includes‬
‭both the numbered first names and last names, such as 1. Frank Zappa, which‬
‭retains the formatting of the original data. However, when using the join‬
‭command on the new files, the output consists solely of the names without any‬
‭numbering, resulting in a more straightforward format like Frank Zappa. This‬
‭change emphasises the names themselves rather than their original indexing.‬
‭d)‬
‭Question 5‬

‭a)‬

‭-‬ ‭The new item is “addresses”.‬


‭-‬ ‭It displays the cat address.‬

‭b)‬
‭c)‬

‭-‬ ‭The output after removing the sales.txt.‬


‭-‬ ‭Sales.txt are removed.‬

‭Display for cat sales.‬

‭-‬ ‭addresses.txt are now removed.‬


‭-‬ ‭Exit from Student and return to personal account.‬
‭-‬ ‭The addresses symbolic link is broken.‬

‭-‬ ‭Returned back to su Student.‬


‭-‬ ‭FILES directory and move addresses2.txt to addresses.txt to retain a copy.‬
‭-‬ ‭Now, addresses2.txt become addresses.txt.‬
PART 4

1.
a.

● Directories: Look for entries starting with d.


● Readable and writable by owner and group: Entries with rw-rw in
the permissions.
● Readable and writable by all: Entries with rw-rw-rw.

b.

● Number that appears are numeric IDs (UID/GID).


c.
i. Change aa6.bbc to not be writable by group.
chmod g-w aa6.bbc - g-w: Removes write permission from the group.

ii. Change aaa.abc to only be readable and writable by owner.


chmod go-rwx aaa.abc - go-rwx: Removes all permissions for group and
others.

iii. Change file1.txt to be readable by all and writable by owner only.


chmod a+r,u+w,g-w,o-w file1.txt - a+r,u+w: Adds read for all, write for owner.

iv. Change dir5 to be readable and executable by group and world.


chmod g+rx,o+rx dir5 - g+rx,o+rx: Adds read and execute for group and
others.

d.
i. Change directory3 to be readable, writable and executable by everyone.
chmod ugo=rwx directory3 - ugo=rwx: Grants all permissions to everyone.

ii. Change file1.txt so that it is no longer executable by group or world.


chmod g-x,o-x file1.txt - g-x,o-x: Removes execute for group and others.

iii. Change file3.txt to be readable, writable and executable by owner, and no


access to anyone else.
chmod u=rwx,go= file3.txt - u=rwx,go=: Grants all permissions to owner,
none to others.
e.
i. Change directory4 to have permissions of rwxr-xr-x.
chmod 755 directory4 - 755: Owner gets full, others get read and execute.

ii. Change file4.txt to have permissions of rw-rw-rw-.


chmod 666 file4.txt - 666: Everyone gets read and write.

iii. Change aa8.bCC to have permissions rwxrwxr--.


chmod 774 aa8.bCC - 774: Owner and group get full, others get read.

2.
a.

● ls access to directories where world has r.


● cd access to directories where world has x.
b.

● Change permissions: chmod g+r GENESIS


● Change group ownership: chgrp cool GENESIS

c.

● Owner (foxr) has rw-, so the value is 6.


● Group (foxr) has r--, so the value is 4.
● World has r--, so the value is 4.
● Thus, the 3-digit permission value for each of the files you listed is 644.

d. Student has read access to all files in the HUMOR directory since files with 644
permissions (rw-r--r--), have read access for all files because the world permission includes
r--

e. Student has no write access to any files in the HUMOR directory since have write (w)
access if the world permission includes w. However, all the files have 644 permissions
(rw-r--r--), where world has r-- (read-only), meaning no write access.

f. zappaf has read access to all files in the HUMOR directory Since zappaf is a member of
the cool group and the permissions for the files are 644 (rw-r--r--). Group (cool) has r--
(read-only) access. This means zappaf can read all files in the directory.
g.

● TEMP is outlined in green due to execute permission (x) for all.


● Deleting a1.txt works if you have write access to the directory.

h.

● Files readable by root but not by others: All files with rw-r--r-- (like adduser.conf,
bash.bashrc, bash_completion, etc.) are readable by root and others in the group or
others.
● Files not even readable by root: drwx------ 2 root root 4096 Apr 19 2024 credstore:
This directory has rwx for root, but no read, write, or execute permission for others or
groups. Root can access the directory, but the directory is restricted in permissions.
i.

● Files like -rwxr-xr-x mean anyone can execute the file, but only root can modify or
write to it.

● arp: A non-root user can execute it because it has world execute access.
● autrace: A non-root user cannot execute it due to missing world execute permissions,
leading to a "command not found" error.

j.

● Even though the useradd command is executable by everyone (due to the rwxr-xr-x
permission), it requires root privileges to function properly and modify system files
like /etc/passwd. You need to either log in as root or use sudo to execute this
command.
3.
a.

● All of files in /etc is both owned and group by root, except some of the files owned by
root but have shadow and landscape as their group
● Same as in /etc, all of files in /dev is both owned and group by root, except some of
the files owned by root but have disk, tty, kvm as their group
b.

● Group owner of ads was changed to cool

● Only root can change ownership


● Changed both owner and group of smileys

● Group ownership remains as Student

4.
a.

● No file found in that criteria


● find /etc -name \*.conf, to find entries that end with .conf
b.
c.

d.

● Find files size is greater than 1000 bytes


● Find empty files
e.

● Files that are sizes between 350 bytes and 450 bytes

f.
g.

● Find items under ~Student that readable, writable but not executable
h.

● Find writable files in ~foxr/HUMOR

● Changed permissions from 664 to 660

You might also like