Skip to content

Day 4 Lab Workbook: Paths, Links, Manual Pages, Vim, and Variables


1. Lab Overview

Item Detail
Lab title Links, documentation, editing, and variables on a RHEL system
Objective Navigate by absolute and relative paths, create and diagnose hard and soft links, find information in manual pages, edit files with Vim, and make variables persistent
Skills tested Paths, ln, ls -li, readlink, man, whatis, Vim modes and commands, shell and environment variables, startup files, verification in a fresh login
Estimated time 2 to 3 hours
Difficulty Beginner to intermediate
Required environment A RHEL 9 or 10 practice VM
Prerequisites Days 1 to 3 completed

2. Lab Environment and Safety

Requirements

  • A disposable RHEL VM with a regular user that has sudo.
  • A second user account for the global-variable tasks. If you do not have one, create it with sudo useradd labuser2 (this is a lab assumption). Set its password with sudo passwd labuser2. Do this only on a practice VM.
  • The vim package (vim-enhanced). Check with rpm -q vim-enhanced. The vi command works without it.

Assumptions (not verified for your environment)

  • Your shell is Bash.
  • The manual page index exists. If man -k returns nothing, run sudo mandb.

Safety

  • Work in ~/day4-lab unless a task says otherwise.
  • rm, rm -r, >, and editing startup files are destructive or can affect every new shell. Read each command twice.
  • Back up ~/.bashrc before editing it and restore it if a mistake makes your shell misbehave.
  • Never add commands that print output or ask questions to ~/.bashrc.
  • Do not store secrets in variables in shared files.
  • Do not disable SELinux or the firewall.
  • Remove the global-variable files you create when you finish.

3. Practice Tasks

Do each task from the requirements. Solutions are in section 7.

Task 1: Paths in both styles

Scenario: You are preparing a directory tree for regional data and must reach it reliably.

Requirements:

  • Create ~/day4-lab/earth/asia/india/telangana with one command.
  • Starting from your home directory, enter telangana using an absolute path and confirm your location.
  • From telangana, return to ~/day4-lab using only a relative path, and confirm.
  • Enter telangana again using only a relative path from ~/day4-lab.
  • Write the absolute path of telangana to ~/day4-lab/path.txt (use a command to produce it, do not type it).

Hints:

  • pwd prints your current directory.
  • .. means the parent directory.

Success criteria: cat ~/day4-lab/path.txt shows the full absolute path, and each pwd check matched what you expected.


Task 2: Identify file types

Scenario: A teammate cannot tell what kinds of objects live under /dev and /etc.

Requirements:

  • Find one example each of a regular file, a directory, and a symbolic link by looking at the first character of ls -l output.
  • Find one block device and one character device under /dev.
  • Use a command that reports the content type of /etc/passwd.
  • Write one line per type to ~/day4-lab/types.txt in the form type: example.

Hints:

  • ls -l /dev | head shows device entries. Block devices start with b, character devices with c.
  • Some devices exist only on certain systems. Use what your VM actually has.

Success criteria: Every example in your file really starts with the character you claimed.


Scenario: You need two names for the same report so that two teams can maintain it.

Requirements:

  • Create ~/day4-lab/links/report.txt with the text v1.
  • Create two hard links, team-a.txt and team-b.txt, to it in the same directory.
  • Show that all three names share one inode and report the link count.
  • Append the line v2 through team-a.txt and show that all names see it.
  • Delete report.txt and show that the other two names still hold the data.
  • Try to create a hard link to a directory and record what happens in ~/day4-lab/hardlink-dir.txt (write what you observed in your own words).

Hints:

  • The first argument of ln is what you link to.
  • A listing option shows inode numbers.

Success criteria: The listing shows identical inodes and a link count of 3 before the deletion and 2 after. Your note explains the directory result.


Scenario: You want a stable name, current, that points at the newest release directory, and a shortcut to a single file.

Requirements:

  • Create ~/day4-lab/apps/app-1.0 and ~/day4-lab/apps/app-1.1.
  • Create a soft link current in ~/day4-lab/apps that points to app-1.1 using a relative target.
  • Create a soft link current-abs that points to app-1.1 using an absolute target.
  • Show where each link points with a command that prints a link's target.
  • Switch current to app-1.0 without deleting the directory it pointed to.
  • Create a file ~/day4-lab/apps/note.txt, link it as note-link, delete note.txt, and show what the dangling link looks like.

Hints:

  • A relative soft-link target is read from the link's own directory.
  • One ln option replaces an existing link.

Success criteria: current points to app-1.0 at the end, both app-1.1 and app-1.0 still exist, and the dangling note-link is visible in ls -l with a target that no longer exists.


Task 5: Find information in the manual

Scenario: You must configure password aging but do not remember the command or its option names, and you have no internet access.

Requirements:

  • Use a keyword search to find the manual pages related to password aging or changing passwords.
  • Open the manual page of the command that shows or changes password aging information and find the option that sets the maximum number of days.
  • Find the manual page that describes the file format of /etc/passwd (not the command).
  • Write the page names you used and the option you found to ~/day4-lab/man-notes.txt.

Hints:

  • If the keyword search returns nothing, rebuild the manual index.
  • The same name can exist in more than one manual section.

Success criteria: Your notes name a keyword search command, a command page, the option, and the section number of the file-format page.


Task 6: Edit with Vim

Scenario: You must correct a configuration-like file using only Vim.

Requirements:

  • Create ~/day4-lab/edit.txt containing the numbers 1 to 5, one per line.
  • Using Vim, make the file contain exactly these lines in this order: 1, inserted, 2, 4, 5, 5.
  • Show line numbers while editing.
  • Save and quit.
  • Then reopen the file, make a deliberate wrong change, and quit without saving it. Confirm the file is unchanged.

Hints:

  • You start in command mode.
  • One key opens a new line below the cursor.
  • Copying and pasting a line takes two keystroke groups.

Success criteria: cat ~/day4-lab/edit.txt shows exactly the six lines, and the discarded change is not present.


Task 7: Vim windows and encryption

Scenario: You want to compare two files side by side and protect a private notes file.

Requirements:

  • Create a.txt and b.txt in ~/day4-lab with different content.
  • Open them stacked (horizontal split), then open them side by side (vertical split). Quit each time.
  • Create ~/day4-lab/secret.txt, encrypt it inside Vim with a password, and confirm that cat shows unreadable text.
  • Reopen it with the password to prove you can read it, then remove the encryption and confirm cat shows plain text.
  • In ~/day4-lab/vim-notes.txt state which option stacks the windows and which places them side by side.

Hints:

  • Two capital and lowercase letter options differ.
  • The encryption command starts with a colon and a capital letter.

Success criteria: Your notes are correct, encrypted content was unreadable with cat, and the final file is plain text again.


Task 8: Variables, persistence, and scope

Scenario: A project needs a variable for your user only and another for everyone.

Requirements:

  • In your current shell, create a variable course with the value RHCSA and display it.
  • Show that a child shell does not see course. Then export it and show that the child shell now sees it.
  • Make a variable PROJECT with the value alpha available in every new shell for your user. Back up your startup file first. Confirm in a fresh login-style shell.
  • Make a variable LAB_ENV with the value practice available to all users at login. Confirm as a second user.
  • Remove the global variable file at the end.

Hints:

  • Spaces around = break an assignment.
  • A drop-in directory under /etc/profile.d/ exists for global login variables.

Success criteria: The child-shell test shows empty then RHCSA, PROJECT prints alpha in a fresh shell, and LAB_ENV prints practice for the second user.


4. Troubleshooting Challenges

Perform these in ~/day4-lab/trouble only.

Setup:

mkdir -p ~/day4-lab/trouble/data ~/day4-lab/trouble/links
echo "hello" > ~/day4-lab/trouble/data/file.txt
cd ~/day4-lab/trouble/links
ln -s data/file.txt mylink
cat mylink

Your job: Explain why cat mylink fails, show the evidence, and fix it in two different ways (an absolute target and a corrected relative target).

Success criteria: After each fix, cat mylink prints hello.

Challenge B: The variable that disappears

Setup: In one terminal run export DEMO=one. Open a second terminal and run echo $DEMO.

Your job: Explain why the second terminal shows nothing. Make the variable available in new terminals and prove it.

Success criteria: A newly opened shell prints one.

Challenge C: The edit that was not applied

Setup: Add export CHALLENGE=yes to ~/.bashrc (after backing it up), then run echo $CHALLENGE in the same terminal.

Your job: Explain why it prints nothing, fix it without opening a new terminal, and then restore your original ~/.bashrc.

Success criteria: The value prints after your fix, and ~/.bashrc matches your backup at the end.

Challenge D: The directory that cannot be hard-linked

Setup: mkdir ~/day4-lab/trouble/dir1 then ln ~/day4-lab/trouble/dir1 ~/day4-lab/trouble/dir1-hard.

Your job: Read the error, explain it, and create a working link instead.

Success criteria: A valid link to the directory exists, and you can explain the restriction.

Challenge E: Stuck in Vim

Setup: Open any file in Vim, press i, type some text, press Esc, and make a change you do not want.

Your job: Undo the unwanted change, and quit in two ways: save, and discard.

Success criteria: You can describe the keystrokes for both exits and the file reflects the saved or discarded state correctly.


5. Final Practical Challenge

Scenario: You are preparing a small release workspace and must leave documentation, links, and environment settings that survive a new login.

Requirements: Inside ~/day4-lab/final, create the following. Choose the commands yourself.

  1. Directories releases/1.0 and releases/1.1, created with one command.
  2. A file releases/1.1/VERSION containing 1.1, written without opening an editor, and a file releases/1.0/VERSION containing 1.0, written using Vim.
  3. A soft link current in releases that points to 1.1 using a relative target. Then switch it to 1.0 and confirm.
  4. A hard link releases/VERSION-main to releases/1.1/VERSION, and the inode and link count of both shown in inodes.txt.
  5. A file docs.txt that lists, for the ln command, one keyword-search command and one option you found in its manual page.
  6. An environment variable RELEASE_HOME that points to the absolute path of ~/day4-lab/final/releases, available to your user in every new shell. Confirm it in a fresh login-style shell and save the value to var-check.txt.
  7. A one-line summary.txt explaining why the soft link current could break and the hard link VERSION-main could not.

Success criteria: ls -l and ls -li outputs match the requirements, var-check.txt has the correct absolute path, and the summary is accurate.


6. Verification Checklist

  • [ ] I created the directory tree and used both absolute and relative paths.
  • [ ] I identified file types from ls -l and from file.
  • [ ] I created hard links, read inodes and link counts, and saw the result of deleting the original.
  • [ ] I created soft links with relative and absolute targets and diagnosed a dangling link.
  • [ ] I used man -k, a manual page search, and a section number.
  • [ ] I edited with Vim (insert, delete, copy, paste, line numbers, save, quit without saving).
  • [ ] I used vim -o and vim -O and know the difference.
  • [ ] I encrypted and decrypted a file in Vim.
  • [ ] I set a shell variable, exported it, and tested a child shell.
  • [ ] I made a variable permanent for my user and global for all users, and tested both.
  • [ ] I completed Challenges A to E.
  • [ ] I restored ~/.bashrc and removed global files I created.
  • [ ] I deleted only items inside ~/day4-lab (and the global file I created).

7. Solutions and Answer Key

Try the tasks first. Output varies by system, so the expected results describe what to look for. Replace placeholders with your own values.

Task 1 solution

mkdir -p ~/day4-lab/earth/asia/india/telangana
cd ~
cd /home/<your-user>/day4-lab/earth/asia/india/telangana      # absolute
pwd
cd ../../../..                                                  # relative, back to day4-lab
pwd
cd earth/asia/india/telangana                                   # relative, down again
pwd > ~/day4-lab/path.txt
cat ~/day4-lab/path.txt

Explanation: Four .. steps climb from telangana to india, asia, earth, then day4-lab. pwd > file writes the path produced by the command.


Task 2 solution

ls -l /etc | head
ls -l /dev | head
file /etc/passwd

Look for lines starting with - (regular), d (directory), l (symbolic link), b (block), c (character). Write one line per type, for example:

{ echo "regular: /etc/passwd"; echo "directory: /etc/ssh"; echo "symlink: <name you found>"; echo "block: <device you found>"; echo "character: <device you found>"; } > ~/day4-lab/types.txt

Use real names from your own listings. file /etc/passwd reports its content type (for example ASCII text).


Task 3 solution

mkdir -p ~/day4-lab/links && cd ~/day4-lab/links
echo "v1" > report.txt
ln report.txt team-a.txt
ln report.txt team-b.txt
ls -li
echo "v2" >> team-a.txt
cat report.txt team-b.txt
rm report.txt
ls -li
cat team-a.txt team-b.txt
mkdir d1
ln d1 d1-hard

Expected: The first ls -li shows one inode with link count 3. After deletion, the count is 2 and cat still prints v1 and v2. The directory hard link is refused (a message saying a hard link is not allowed for a directory). Write your own sentence in hardlink-dir.txt, for example: Hard links to directories are not allowed; use a symbolic link instead.


Task 4 solution

mkdir -p ~/day4-lab/apps/app-1.0 ~/day4-lab/apps/app-1.1
cd ~/day4-lab/apps
ln -s app-1.1 current
ln -s "$PWD/app-1.1" current-abs
readlink current
readlink -f current-abs
ln -sfn app-1.0 current
readlink current
echo "note" > note.txt
ln -s note.txt note-link
rm note.txt
ls -l note-link
cat note-link

Explanation: -s makes a soft link. -f replaces the existing link, and -n stops ln from following an existing link that points to a directory. The relative target app-1.1 is read from ~/day4-lab/apps. After note.txt is deleted, ls -l still shows note-link -> note.txt, but cat fails with "No such file or directory".


Task 5 solution

man -k password        # or: apropos password
man chage              # chage changes password aging information

In the page, type /maximum, press Enter, then n for more matches. The option that sets the maximum number of days between password changes is -M (long form --maxdays). Confirm in the page.

man 5 passwd           # file format of /etc/passwd

Save your notes, for example:

{ echo "keyword search: man -k password"; echo "command page: man chage"; echo "option: -M (maxdays), confirmed in man chage"; echo "file format page: man 5 passwd"; } > ~/day4-lab/man-notes.txt

If man -k returns nothing: sudo mandb and retry.


Task 6 solution

seq 1 5 > ~/day4-lab/edit.txt
vim ~/day4-lab/edit.txt

In Vim:

  1. You are in command mode. Press o, type inserted, press Esc. Lines are now 1, inserted, 2, 3, 4, 5.
  2. Press :set nu and Enter to show numbers.
  3. Move down to the line 3 with j and press dd to delete it.
  4. Press G to go to the last line, then yy and p to duplicate it.
  5. Save and quit with :wq.
cat ~/day4-lab/edit.txt

Expected: 1, inserted, 2, 4, 5, 5.

To test discarding: open the file, press x a few times, then :q!. Run cat again and confirm nothing changed.


Task 7 solution

cd ~/day4-lab
echo "file a" > a.txt
echo "file b" > b.txt
vim -o a.txt b.txt        # stacked (horizontal). Quit each window with :q
vim -O a.txt b.txt        # side by side (vertical). Quit each window with :q
echo "my secret" > secret.txt
vim secret.txt

In Vim, type :X, enter a password twice, and then :wq.

cat secret.txt            # unreadable
vim secret.txt            # asks for the password

To remove: open it, type :X, enter an empty password (press Enter) twice, and :wq. Then cat secret.txt shows plain text.

echo "-o stacks windows; -O places them side by side" > ~/day4-lab/vim-notes.txt

Warning: A forgotten password cannot be recovered. This exercise uses a throwaway file.


Task 8 solution

course="RHCSA"
echo $course
bash -c 'echo "child sees: [$course]"'      # empty
export course
bash -c 'echo "child sees: [$course]"'      # RHCSA

cp ~/.bashrc ~/.bashrc.bak
echo 'export PROJECT=alpha' >> ~/.bashrc
source ~/.bashrc
bash -l -c 'echo $PROJECT'                  # alpha

echo 'export LAB_ENV=practice' | sudo tee /etc/profile.d/lab-env.sh
su - labuser2 -c 'echo $LAB_ENV'            # practice

sudo rm /etc/profile.d/lab-env.sh

Explanation: Single quotes stop your current shell from expanding $course, so the child shell evaluates it. bash -l -c starts a login-style shell. The /etc/profile.d/*.sh scripts are read at login, so a new login shell sees the variable.

Cleanup: cp ~/.bashrc.bak ~/.bashrc restores your startup file, and unset course PROJECT.


Challenge A solution

ln -s data/file.txt mylink was run inside links. The relative target is read from the link's directory (links), so the system looks for links/data/file.txt, which does not exist.

readlink mylink
ls -l mylink

Fix 1 (absolute target):

rm mylink
ln -s ~/day4-lab/trouble/data/file.txt mylink
cat mylink

Fix 2 (correct relative target):

rm mylink
ln -s ../data/file.txt mylink
cat mylink

Both print hello.


Challenge B solution

export DEMO=one affects only that shell and the programs it starts. A separate terminal is a separate shell. To make it permanent for your user:

cp ~/.bashrc ~/.bashrc.bak
echo 'export DEMO=one' >> ~/.bashrc

Open a new terminal and run echo $DEMO. Restore with cp ~/.bashrc.bak ~/.bashrc when finished.


Challenge C solution

Editing ~/.bashrc does not change the shell that is already running. Reload it:

source ~/.bashrc
echo $CHALLENGE
cp ~/.bashrc.bak ~/.bashrc       # restore the original

Challenge D solution

The error says a hard link is not allowed for a directory. Use a soft link:

ln -s ~/day4-lab/trouble/dir1 ~/day4-lab/trouble/dir1-soft
ls -l ~/day4-lab/trouble

Explanation: Hard links to directories are not allowed because they could create loops in the file system tree.


Challenge E solution

  • Undo the last change: press Esc, then u (repeat as needed).
  • Save and quit: :wq (or ZZ).
  • Discard and quit: :q!.

Verify with cat <file> afterward.


Final practical challenge solution

mkdir -p ~/day4-lab/final/releases/{1.0,1.1}
cd ~/day4-lab/final

echo "1.1" > releases/1.1/VERSION
vim releases/1.0/VERSION        # press i, type 1.0, Esc, :wq

cd releases
ln -s 1.1 current
ln -sfn 1.0 current
readlink current                # 1.0
ln 1.1/VERSION VERSION-main
cd ..

ls -li releases/1.1/VERSION releases/VERSION-main > inodes.txt
cat inodes.txt                   # same inode, link count 2

{ echo "keyword search: man -k link"; man ln | grep -m1 -- '--symbolic' ; } > docs.txt

For docs.txt, confirm the exact option you cite by reading man ln yourself (for example -s or --symbolic). Edit the file so it contains one search command and one option you verified.

cp ~/.bashrc ~/.bashrc.bak
echo "export RELEASE_HOME=$HOME/day4-lab/final/releases" >> ~/.bashrc
source ~/.bashrc
bash -l -c 'echo $RELEASE_HOME' > var-check.txt
cat var-check.txt

echo "current is a soft link that stores a path and breaks if the target is moved or deleted; VERSION-main is a hard link to the same inode so it keeps the data while any name exists." > summary.txt

Verification: ls -l releases shows current -> 1.0. ls -li shows the same inode for 1.1/VERSION and VERSION-main. var-check.txt contains the absolute path of releases.

Cleanup:

cp ~/.bashrc.bak ~/.bashrc
unset RELEASE_HOME
rm -r ~/day4-lab

Remove only the lab directory you created.


8. Extended Practice Drills

Short, independent drills for repeated practice. Do them in order or jump to the group you need. Work only inside your lab directory, read each drill carefully, and check your result with a command before looking at the answer list in section 9.

Setup (run once):

mkdir -p ~/day4-lab/drills && cd ~/day4-lab/drills
mkdir -p earth/asia/india/telangana
echo "sample" > sample.txt

A. Absolute and relative paths

  1. Print your current directory.
  2. Enter earth/asia/india using a relative path and print your location.
  3. Go up one level with a relative path.
  4. Go up three levels with one command.
  5. Go to your home directory two different ways.
  6. Reach ~/day4-lab/drills/earth with an absolute path.
  7. Return to the previous directory.
  8. State when an absolute path is safer than a relative path.
  9. What do . and .. mean in a path?
  10. Write the absolute path of telangana to paths.txt without typing it by hand.

B. File types

  1. Show a long listing of /etc and find a line starting with -.
  2. Find a directory entry (starts with d) in /etc.
  3. Find a symbolic link (starts with l) in /etc or /.
  4. Find a block device in /dev.
  5. Find a character device in /dev.
  6. Report the content type of /etc/passwd.
  7. Report the type of /bin/ls.
  8. What do the characters s and p mean at the start of an ls -l line?
  9. Show the entry for the directory /etc itself, not its contents.
  10. Show detailed metadata of sample.txt, including inode and link count.
  1. Create hard link hard1 to sample.txt.
  2. Create a second hard link hard2.
  3. Show the inode numbers of all three names.
  4. What link count do you expect after creating two hard links?
  5. Append more through hard1 and read through hard2.
  6. Delete sample.txt and read hard1.
  7. Show the new link count.
  8. Try to hard-link a directory. What happens?
  9. Explain why hard links cannot cross file systems.
  10. Do hard links use extra data space? Explain.
  1. Create a file target.txt and a soft link soft1 to it using a relative target.
  2. Create soft2 using an absolute target.
  3. Show where soft1 points.
  4. Show the resolved absolute path of soft1.
  5. Show that soft1 has a different inode from target.txt.
  6. Create a soft link earth-link to the directory earth.
  7. Enter the directory through the link and print your location.
  8. Delete target.txt and try cat soft1.
  9. How can you recognize a dangling link in ls -l?
  10. Create soft3 to a file in another directory using a relative target, then move soft3 to a different directory. Does it still work? Why?
  11. Replace an existing link current so it points to another target.
  12. Remove a soft link without touching the target.

E. Manual pages

  1. Open the manual page for ls and quit.
  2. Inside man ls, search for -a, go to the next match, then to the previous.
  3. Jump to the end and then the start of a page.
  4. Find manual pages whose description mentions password.
  5. What should you run if man -k returns nothing?
  6. Show the one-line description of chmod.
  7. Open the file-format page for /etc/passwd.
  8. What is the difference between man passwd and man 5 passwd?
  9. Which manual section contains user commands, file formats, and administration commands?
  10. Find the option of useradd that sets the home directory.
  11. Find the option of ls that shows sizes in human-readable form.
  12. Show a short usage summary of a command without opening the manual.

F. Vim

  1. Open a new file v1.txt in Vim.
  2. Enter insert mode, type two lines, return to command mode, save, and quit.
  3. Reopen and show line numbers.
  4. Hide line numbers.
  5. Make line numbers permanent for your user.
  6. Open a new line below the cursor and type new.
  7. Open a new line above the cursor.
  8. Insert at the beginning of a line, then at the end.
  9. Delete one character.
  10. Delete the current line.
  11. Copy the current line and paste it below.
  12. Undo, and redo.
  13. Select three lines in visual mode and copy them.
  14. Go to the last line and the first line.
  15. Search for the word new and jump to the next match.
  16. Quit without saving.
  17. Save and quit using two different methods.
  18. Open two files stacked, then side by side.
  19. Encrypt a file inside Vim.
  20. Remove the encryption.
  21. Replace every old with new in a file (optional).
  22. You pasted text and it came in oddly indented. What can you try?

G. Variables

  1. Create a variable name with value Laxmi and print it.
  2. Why does name = "Laxmi" fail?
  3. Print the variable inside text, such as Laxmi_lab.
  4. Show that a child shell cannot see an unexported variable.
  5. Export the variable and test the child shell again.
  6. Create and export city in one command.
  7. Show city in the environment.
  8. Remove the variable city.
  9. Which variable holds your home directory? Your user name?
  10. Back up ~/.bashrc.
  11. Make PROJECT=alpha permanent for your user.
  12. Load the change into the current shell.
  13. Prove it works in a fresh login-style shell.
  14. Change the value in ~/.bashrc but do not source it. What does echo $PROJECT show?
  15. Make LAB_ENV=practice available to all users at login.
  16. Test it as another user.
  17. What rules apply to lines in /etc/environment?
  18. Remove the global variable file you created.
  19. Restore ~/.bashrc from your backup.
  20. Name two places that make a variable survive a new login.

H. Integrated drills

  1. Create proj/{v1,v2}, put a VERSION file in each, and a soft link latest to v2.
  2. Switch latest to v1 and verify.
  3. Create a hard link proj/VERSION-main to proj/v2/VERSION and show the inode and link count.
  4. Delete proj/v2/VERSION and show that VERSION-main still has the data.
  5. Use man to confirm the ln option that makes a symbolic link, and write it to ln-option.txt.
  6. Set PROJ_HOME to the absolute path of proj permanently for your user and verify in a fresh shell.
  7. Clean up: restore ~/.bashrc, unset the variable, and remove proj.

9. Drill Answer Key

Answers show one valid approach. Output varies by system, so compare the meaning of your output, not exact text.

A. Absolute and relative paths

  1. pwd
  2. cd earth/asia/india; pwd
  3. cd ..
  4. cd ../../..
  5. cd and cd ~
  6. cd /home/<your-user>/day4-lab/drills/earth (or cd ~/day4-lab/drills/earth)
  7. cd -
  8. When you are unsure of your current directory, or in scripts that may run from anywhere.
  9. . is the current directory. .. is the parent directory.
  10. cd ~/day4-lab/drills/earth/asia/india/telangana && pwd > ~/day4-lab/drills/paths.txt

B. File types

  1. ls -l /etc | head
  2. ls -l /etc | grep '^d' | head -3
  3. ls -l / | grep '^l'
  4. ls -l /dev | grep '^b' | head -3
  5. ls -l /dev | grep '^c' | head -3
  6. file /etc/passwd
  7. file /bin/ls
  8. s is a socket and p is a named pipe.
  9. ls -ld /etc
  10. stat sample.txt
  1. ln sample.txt hard1
  2. ln sample.txt hard2
  3. ls -li sample.txt hard1 hard2
  4. 3 (the original name plus two links).
  5. echo more >> hard1; cat hard2
  6. rm sample.txt; cat hard1 (still works).
  7. ls -li hard1 hard2 (count is 2).
  8. ln earth earth-hard is refused (hard link not allowed for directory).
  9. An inode number is meaningful only inside one file system, so a name in another file system cannot refer to it.
  10. No. They add only a directory entry, because all names share one inode and the same data.
  1. echo hi > target.txt; ln -s target.txt soft1
  2. ln -s "$PWD/target.txt" soft2
  3. readlink soft1
  4. readlink -f soft1
  5. ls -li target.txt soft1
  6. ln -s earth earth-link
  7. cd earth-link; pwd (use pwd -P to see the real path), then cd ..
  8. rm target.txt; cat soft1 fails: No such file or directory.
  9. It still shows soft1 -> target.txt, but the target does not exist (many terminals color it red).
  10. Usually not. A relative target is read from the link's own directory, so moving the link changes where the path points.
  11. ln -sfn <newtarget> current
  12. rm soft1 (removes only the link).

E. Manual pages

  1. man ls, then q.
  2. /-a Enter, then n, then N.
  3. G then g.
  4. man -k password (or apropos password).
  5. sudo mandb, then retry.
  6. whatis chmod (or man -f chmod).
  7. man 5 passwd
  8. man passwd opens the command page. man 5 passwd opens the file-format page (section 5).
  9. Section 1: user commands. Section 5: file formats. Section 8: administration commands.
  10. man useradd, search /home, and read the option (-d, confirm in your page).
  11. -h, found by searching /human in man ls.
  12. <command> --help, for example ls --help | head.

F. Vim

  1. vim v1.txt
  2. i, type, Esc, :wq
  3. :set nu
  4. :set nonu
  5. Add set number to ~/.vimrc.
  6. o, type, Esc
  7. O
  8. I and A
  9. x
  10. dd
  11. yy then p
  12. u and Ctrl+r
  13. V, j j, y
  14. G and gg
  15. /new Enter, then n
  16. :q!
  17. :wq and ZZ
  18. vim -o a.txt b.txt and vim -O a.txt b.txt
  19. :X, enter a password twice, :wq.
  20. :X, press Enter twice for an empty password, :wq.
  21. :%s/old/new/g then review and save.
  22. Run :set paste before pasting in insert mode, then :set nopaste.

G. Variables

  1. name="Laxmi"; echo $name
  2. Spaces around = make the shell treat name as a command.
  3. echo "${name}_lab"
  4. bash -c 'echo [$name]' prints [].
  5. export name; bash -c 'echo [$name]'
  6. export city=Hyderabad
  7. env | grep city (or printenv city).
  8. unset city
  9. $HOME and $USER.
  10. cp ~/.bashrc ~/.bashrc.bak
  11. echo 'export PROJECT=alpha' >> ~/.bashrc
  12. source ~/.bashrc
  13. bash -l -c 'echo $PROJECT'
  14. The old value, because the running shell has not reloaded the file.
  15. echo 'export LAB_ENV=practice' | sudo tee /etc/profile.d/lab-env.sh
  16. su - <user> -c 'echo $LAB_ENV'
  17. Plain NAME=value lines, no export, no spaces around =, no expansion or commands.
  18. sudo rm /etc/profile.d/lab-env.sh
  19. cp ~/.bashrc.bak ~/.bashrc
  20. ~/.bashrc (per user) and /etc/profile.d/*.sh (all users).

H. Integrated drills

  1. mkdir -p proj/{v1,v2}; echo 1 > proj/v1/VERSION; echo 2 > proj/v2/VERSION; ln -s v2 proj/latest; ls -l proj
  2. ln -sfn v1 proj/latest; readlink proj/latest
  3. ln proj/v2/VERSION proj/VERSION-main; ls -li proj/v2/VERSION proj/VERSION-main
  4. rm proj/v2/VERSION; cat proj/VERSION-main
  5. man ln, search /symbolic, then echo "-s" > ln-option.txt (use what you verified).
  6. echo "export PROJ_HOME=$PWD/proj" >> ~/.bashrc; source ~/.bashrc; bash -l -c 'echo $PROJ_HOME'
  7. cp ~/.bashrc.bak ~/.bashrc; unset PROJ_HOME; rm -r proj (check the path first).