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 withsudo passwd labuser2. Do this only on a practice VM. - The
vimpackage (vim-enhanced). Check withrpm -q vim-enhanced. Thevicommand works without it.
Assumptions (not verified for your environment)
- Your shell is Bash.
- The manual page index exists. If
man -kreturns nothing, runsudo mandb.
Safety
- Work in
~/day4-labunless 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
~/.bashrcbefore 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/telanganawith one command. - Starting from your home directory, enter
telanganausing an absolute path and confirm your location. - From
telangana, return to~/day4-labusing only a relative path, and confirm. - Enter
telanganaagain using only a relative path from~/day4-lab. - Write the absolute path of
telanganato~/day4-lab/path.txt(use a command to produce it, do not type it).
Hints:
pwdprints 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 -loutput. - 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.txtin the formtype: example.
Hints:
ls -l /dev | headshows device entries. Block devices start withb, character devices withc.- 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.
Task 3: Hard links¶
Scenario: You need two names for the same report so that two teams can maintain it.
Requirements:
- Create
~/day4-lab/links/report.txtwith the textv1. - Create two hard links,
team-a.txtandteam-b.txt, to it in the same directory. - Show that all three names share one inode and report the link count.
- Append the line
v2throughteam-a.txtand show that all names see it. - Delete
report.txtand 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
lnis 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.
Task 4: Soft links¶
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.0and~/day4-lab/apps/app-1.1. - Create a soft link
currentin~/day4-lab/appsthat points toapp-1.1using a relative target. - Create a soft link
current-absthat points toapp-1.1using an absolute target. - Show where each link points with a command that prints a link's target.
- Switch
currenttoapp-1.0without deleting the directory it pointed to. - Create a file
~/day4-lab/apps/note.txt, link it asnote-link, deletenote.txt, and show what the dangling link looks like.
Hints:
- A relative soft-link target is read from the link's own directory.
- One
lnoption 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.txtcontaining 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.txtandb.txtin~/day4-labwith 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 thatcatshows unreadable text. - Reopen it with the password to prove you can read it, then remove the encryption and confirm
catshows plain text. - In
~/day4-lab/vim-notes.txtstate 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
coursewith the valueRHCSAand 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
PROJECTwith the valuealphaavailable in every new shell for your user. Back up your startup file first. Confirm in a fresh login-style shell. - Make a variable
LAB_ENVwith the valuepracticeavailable 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.
Challenge A: The link that does not work¶
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.
- Directories
releases/1.0andreleases/1.1, created with one command. - A file
releases/1.1/VERSIONcontaining1.1, written without opening an editor, and a filereleases/1.0/VERSIONcontaining1.0, written using Vim. - A soft link
currentinreleasesthat points to1.1using a relative target. Then switch it to1.0and confirm. - A hard link
releases/VERSION-maintoreleases/1.1/VERSION, and the inode and link count of both shown ininodes.txt. - A file
docs.txtthat lists, for thelncommand, one keyword-search command and one option you found in its manual page. - An environment variable
RELEASE_HOMEthat 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 tovar-check.txt. - A one-line
summary.txtexplaining why the soft linkcurrentcould break and the hard linkVERSION-maincould 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 -land fromfile. - [ ] 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 -oandvim -Oand 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
~/.bashrcand 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:
- You are in command mode. Press
o, typeinserted, pressEsc. Lines are now1,inserted,2,3,4,5. - Press
:set nuand Enter to show numbers. - Move down to the line
3withjand pressddto delete it. - Press
Gto go to the last line, thenyyandpto duplicate it. - 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, thenu(repeat as needed). - Save and quit:
:wq(orZZ). - 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¶
- Print your current directory.
- Enter
earth/asia/indiausing a relative path and print your location. - Go up one level with a relative path.
- Go up three levels with one command.
- Go to your home directory two different ways.
- Reach
~/day4-lab/drills/earthwith an absolute path. - Return to the previous directory.
- State when an absolute path is safer than a relative path.
- What do
.and..mean in a path? - Write the absolute path of
telanganatopaths.txtwithout typing it by hand.
B. File types¶
- Show a long listing of
/etcand find a line starting with-. - Find a directory entry (starts with
d) in/etc. - Find a symbolic link (starts with
l) in/etcor/. - Find a block device in
/dev. - Find a character device in
/dev. - Report the content type of
/etc/passwd. - Report the type of
/bin/ls. - What do the characters
sandpmean at the start of anls -lline? - Show the entry for the directory
/etcitself, not its contents. - Show detailed metadata of
sample.txt, including inode and link count.
C. Hard links¶
- Create hard link
hard1tosample.txt. - Create a second hard link
hard2. - Show the inode numbers of all three names.
- What link count do you expect after creating two hard links?
- Append
morethroughhard1and read throughhard2. - Delete
sample.txtand readhard1. - Show the new link count.
- Try to hard-link a directory. What happens?
- Explain why hard links cannot cross file systems.
- Do hard links use extra data space? Explain.
D. Soft links¶
- Create a file
target.txtand a soft linksoft1to it using a relative target. - Create
soft2using an absolute target. - Show where
soft1points. - Show the resolved absolute path of
soft1. - Show that
soft1has a different inode fromtarget.txt. - Create a soft link
earth-linkto the directoryearth. - Enter the directory through the link and print your location.
- Delete
target.txtand trycat soft1. - How can you recognize a dangling link in
ls -l? - Create
soft3to a file in another directory using a relative target, then movesoft3to a different directory. Does it still work? Why? - Replace an existing link
currentso it points to another target. - Remove a soft link without touching the target.
E. Manual pages¶
- Open the manual page for
lsand quit. - Inside
man ls, search for-a, go to the next match, then to the previous. - Jump to the end and then the start of a page.
- Find manual pages whose description mentions
password. - What should you run if
man -kreturns nothing? - Show the one-line description of
chmod. - Open the file-format page for
/etc/passwd. - What is the difference between
man passwdandman 5 passwd? - Which manual section contains user commands, file formats, and administration commands?
- Find the option of
useraddthat sets the home directory. - Find the option of
lsthat shows sizes in human-readable form. - Show a short usage summary of a command without opening the manual.
F. Vim¶
- Open a new file
v1.txtin Vim. - Enter insert mode, type two lines, return to command mode, save, and quit.
- Reopen and show line numbers.
- Hide line numbers.
- Make line numbers permanent for your user.
- Open a new line below the cursor and type
new. - Open a new line above the cursor.
- Insert at the beginning of a line, then at the end.
- Delete one character.
- Delete the current line.
- Copy the current line and paste it below.
- Undo, and redo.
- Select three lines in visual mode and copy them.
- Go to the last line and the first line.
- Search for the word
newand jump to the next match. - Quit without saving.
- Save and quit using two different methods.
- Open two files stacked, then side by side.
- Encrypt a file inside Vim.
- Remove the encryption.
- Replace every
oldwithnewin a file (optional). - You pasted text and it came in oddly indented. What can you try?
G. Variables¶
- Create a variable
namewith valueLaxmiand print it. - Why does
name = "Laxmi"fail? - Print the variable inside text, such as
Laxmi_lab. - Show that a child shell cannot see an unexported variable.
- Export the variable and test the child shell again.
- Create and export
cityin one command. - Show
cityin the environment. - Remove the variable
city. - Which variable holds your home directory? Your user name?
- Back up
~/.bashrc. - Make
PROJECT=alphapermanent for your user. - Load the change into the current shell.
- Prove it works in a fresh login-style shell.
- Change the value in
~/.bashrcbut do not source it. What doesecho $PROJECTshow? - Make
LAB_ENV=practiceavailable to all users at login. - Test it as another user.
- What rules apply to lines in
/etc/environment? - Remove the global variable file you created.
- Restore
~/.bashrcfrom your backup. - Name two places that make a variable survive a new login.
H. Integrated drills¶
- Create
proj/{v1,v2}, put aVERSIONfile in each, and a soft linklatesttov2. - Switch
latesttov1and verify. - Create a hard link
proj/VERSION-maintoproj/v2/VERSIONand show the inode and link count. - Delete
proj/v2/VERSIONand show thatVERSION-mainstill has the data. - Use
manto confirm thelnoption that makes a symbolic link, and write it toln-option.txt. - Set
PROJ_HOMEto the absolute path ofprojpermanently for your user and verify in a fresh shell. - Clean up: restore
~/.bashrc, unset the variable, and removeproj.
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¶
pwdcd earth/asia/india; pwdcd ..cd ../../..cdandcd ~cd /home/<your-user>/day4-lab/drills/earth(orcd ~/day4-lab/drills/earth)cd -- When you are unsure of your current directory, or in scripts that may run from anywhere.
.is the current directory...is the parent directory.cd ~/day4-lab/drills/earth/asia/india/telangana && pwd > ~/day4-lab/drills/paths.txt
B. File types¶
ls -l /etc | headls -l /etc | grep '^d' | head -3ls -l / | grep '^l'ls -l /dev | grep '^b' | head -3ls -l /dev | grep '^c' | head -3file /etc/passwdfile /bin/lssis a socket andpis a named pipe.ls -ld /etcstat sample.txt
C. Hard links¶
ln sample.txt hard1ln sample.txt hard2ls -li sample.txt hard1 hard2- 3 (the original name plus two links).
echo more >> hard1; cat hard2rm sample.txt; cat hard1(still works).ls -li hard1 hard2(count is 2).ln earth earth-hardis refused (hard link not allowed for directory).- An inode number is meaningful only inside one file system, so a name in another file system cannot refer to it.
- No. They add only a directory entry, because all names share one inode and the same data.
D. Soft links¶
echo hi > target.txt; ln -s target.txt soft1ln -s "$PWD/target.txt" soft2readlink soft1readlink -f soft1ls -li target.txt soft1ln -s earth earth-linkcd earth-link; pwd(usepwd -Pto see the real path), thencd ..rm target.txt; cat soft1fails: No such file or directory.- It still shows
soft1 -> target.txt, but the target does not exist (many terminals color it red). - Usually not. A relative target is read from the link's own directory, so moving the link changes where the path points.
ln -sfn <newtarget> currentrm soft1(removes only the link).
E. Manual pages¶
man ls, thenq./-aEnter, thenn, thenN.Gtheng.man -k password(orapropos password).sudo mandb, then retry.whatis chmod(orman -f chmod).man 5 passwdman passwdopens the command page.man 5 passwdopens the file-format page (section 5).- Section 1: user commands. Section 5: file formats. Section 8: administration commands.
man useradd, search/home, and read the option (-d, confirm in your page).-h, found by searching/humaninman ls.<command> --help, for examplels --help | head.
F. Vim¶
vim v1.txti, type,Esc,:wq:set nu:set nonu- Add
set numberto~/.vimrc. o, type,EscOIandAxddyythenpuandCtrl+rV,jj,yGandgg/newEnter, thenn:q!:wqandZZvim -o a.txt b.txtandvim -O a.txt b.txt:X, enter a password twice,:wq.:X, press Enter twice for an empty password,:wq.:%s/old/new/gthen review and save.- Run
:set pastebefore pasting in insert mode, then:set nopaste.
G. Variables¶
name="Laxmi"; echo $name- Spaces around
=make the shell treatnameas a command. echo "${name}_lab"bash -c 'echo [$name]'prints[].export name; bash -c 'echo [$name]'export city=Hyderabadenv | grep city(orprintenv city).unset city$HOMEand$USER.cp ~/.bashrc ~/.bashrc.bakecho 'export PROJECT=alpha' >> ~/.bashrcsource ~/.bashrcbash -l -c 'echo $PROJECT'- The old value, because the running shell has not reloaded the file.
echo 'export LAB_ENV=practice' | sudo tee /etc/profile.d/lab-env.shsu - <user> -c 'echo $LAB_ENV'- Plain
NAME=valuelines, noexport, no spaces around=, no expansion or commands. sudo rm /etc/profile.d/lab-env.shcp ~/.bashrc.bak ~/.bashrc~/.bashrc(per user) and/etc/profile.d/*.sh(all users).
H. Integrated drills¶
mkdir -p proj/{v1,v2}; echo 1 > proj/v1/VERSION; echo 2 > proj/v2/VERSION; ln -s v2 proj/latest; ls -l projln -sfn v1 proj/latest; readlink proj/latestln proj/v2/VERSION proj/VERSION-main; ls -li proj/v2/VERSION proj/VERSION-mainrm proj/v2/VERSION; cat proj/VERSION-mainman ln, search/symbolic, thenecho "-s" > ln-option.txt(use what you verified).echo "export PROJ_HOME=$PWD/proj" >> ~/.bashrc; source ~/.bashrc; bash -l -c 'echo $PROJ_HOME'cp ~/.bashrc.bak ~/.bashrc; unset PROJ_HOME; rm -r proj(check the path first).