Module brief#
Connect names to files with hard and symbolic links, select names with wildcards, locate files with find/locate, and search inside files with grep.
Learning objectives#
- Create hard links and symbolic links; remove them safely
- Explain when hard vs soft links matter
- Use wildcards
*,?, and[] - Find files by name and attributes with
find - Optionally use
locatewhen an index exists - Search file contents with
grepand common flags
1. Hard links vs symbolic links#
A directory entry is a name pointing at file data. A hard link is another name for the same inode (same data). A symbolic link (symlink) is a small special file that stores a path to another name.
cd /tmp/linux-lab-pushpjeet
printf 'Line one\nLine two\nLine three\nLine four\nLine five\n' > notes.txt
ln notes.txt hard-notes # hard link
ln -s notes.txt soft-notes # symbolic link
ls -l notes.txt hard-notes soft-notes
ls -i notes.txt hard-notes soft-notes
-rw-r--r-- 2 box box 49 Sep 26 11:04 hard-notes
-rw-r--r-- 2 box box 49 Sep 26 11:04 notes.txt
lrwxrwxrwx 1 box box 9 Sep 26 11:04 soft-notes -> notes.txt
1084900 hard-notes
1084900 notes.txt
1084906 soft-notes
Observations:
notes.txtandhard-notesshare inode1084900and show link count2soft-noteshas a different inode and displays as typelwith->
stat -c '%n links=%h inode=%i' notes.txt hard-notes soft-notes
notes.txt links=2 inode=1084900
hard-notes links=2 inode=1084900
soft-notes links=1 inode=1084906
| Hard link | Symbolic link | |
|---|---|---|
| Command | ln target name |
ln -s target name |
| Same inode? | Yes | No |
| Can cross filesystems? | No | Yes |
| Can link directories? | Generally not (as a user) | Yes |
| If original name removed | Data remains while any hard link remains | Symlink may dangle (broken) |
Remove a link name with rm or unlink (same idea for one name):
rm soft-notes # removes the symlink, not the target content
Practical pattern: symlink a config into your home for convenience, for example ln -s /path/to/project/config ~/.myconfig — adjust paths to files you own.
2. Wildcards (globs)#
The shell expands globs before the command runs.
| Pattern | Matches |
|---|---|
* |
Any string (including empty) |
? |
Any single character |
[abc] |
One character from the set |
[a-z] |
One character from a range (locale-dependent) |
cd /tmp/linux-lab-pushpjeet
ls *.txt
a.txt b.txt notes-copy.txt notes.txt readme.txt
ls [ab].txt
a.txt b.txt
Quote patterns when you want find or grep to see the wildcard characters themselves (otherwise the shell expands them too early). Example: find . -name '*.txt'.
3. find — walk the tree#
find . -name '*.txt'
./b.txt
./notes.txt
./a.txt
./readme.txt
Useful expressions:
find . -type f -name '*.log'
find . -type d -name 'src'
find . -mtime -1 -name '*.log' # modified in the last 24 hours
find /tmp/linux-lab-pushpjeet -maxdepth 2 -type f
find is precise and always walks live directories. It can also run actions (-exec) — learn names and types first; add actions carefully later.
4. locate — fast index search (optional)#
locate queries a prebuilt database (often updated by updatedb on a schedule). It is fast but may be slightly stale, and some minimal systems omit it.
command -v locate
# locate notes.txt
If locate is missing, install a package such as plocate or mlocate on Ubuntu, or the RHEL-family equivalent — or simply rely on find for this course.
5. grep — search inside files#
grep -n Linux readme.txt
1:Hello Linux learners
grep -i linux readme.txt
Hello Linux learners
Common flags:
| Flag | Meaning |
|---|---|
-n |
Show line numbers |
-i |
Case-insensitive |
-r or -R |
Recurse into directories |
-l |
List matching filenames only |
-v |
Invert match (non-matching lines) |
-E |
Extended regular expressions |
grep -rn 'Line' . --include='*.txt'
./notes-copy.txt:1:Line one
./notes-copy.txt:2:Line two
…
./notes.txt:1:Line one
…
Regex intro (enough to start):
^Hello— line starts with Hellolearners$— line ends with learners.— any single character*— previous item zero or more times (basic regex rules differ slightly from shell globs — keep them mentally separate)
grep -n '^Hello' readme.txt
grep -E 'learners|CLI' readme.txt
6. Putting it together#
A common admin loop:
find(or a glob) to locate candidate filesgrepto inspect contents- Symlink or copy into place once you know the right file
Example lab sequence:
find . -name 'readme.txt'
grep -n CLI readme.txt
ln -sf "$(pwd)/readme.txt" /tmp/linux-lab-pushpjeet/readme-link
ls -l readme-link
Practice: MCQs#
Q1. After ln a.txt b.txt, both names share an inode. This is a:
- A) Symbolic link
- B) Hard link
- C) Broken mount
- D) Compressed archive
Answer
B. Hard links share the inode. Symlinks use ln -s and show ->.
Q2. You delete the original name of a file that still has a hard link. What happens to the data?
- A) It vanishes immediately forever in all cases
- B) It remains available through the remaining hard link
- C) The disk reformats
- D) The symlink count increases automatically
Answer
B. Data remains until the link count drops to zero (and the file is unused).
Q3. Which pattern matches a.txt and b.txt but not notes.txt?
- A)
*.txt - B)
?.txt - C)
notes* - D)
*
Answer
B. ? matches one character, so ?.txt fits a.txt / b.txt.
Q4. Why quote '*.log' in find . -name '*.log'?
- A) Quotes delete log files
- B) So the shell does not expand
*beforefindruns - C)
findcannot search without quotes ever - D) It enables colour output
Answer
B. Quoting passes the pattern to find intact.
Q5. grep -n pattern file adds:
- A) Octal permissions
- B) Line numbers to matches
- C) A hard link
- D) Gzip compression
Answer
B. -n prints line numbers.
Q6. A symlink whose target was removed is often called:
- A) A hard link
- B) A dangling (broken) symlink
- C) An inode
- D) A daemon
Answer
B. The symlink name still exists but points nowhere useful.
Practice: Flashcards#
| Front | Back |
|---|---|
ln file alias |
Create a hard link named alias |
ln -s target name |
Create a symbolic link |
| Inode | Kernel identity for a file’s data/metadata |
Glob * |
Matches any string in filenames |
Glob ? |
Matches exactly one character |
find . -name '*.py' |
Find matching names under current tree |
grep -i |
Case-insensitive content search |
grep -n |
Show line numbers with matches |
locate |
Fast search via an indexed database |
| Dangling symlink | Symlink whose target path no longer exists |
Practice: Match the columns#
| Item | Description (scrambled) |
|---|---|
| 1. Hard link | A. Stores a path; can dangle |
| 2. Symlink | B. Same inode; another name for data |
3. find |
C. Live recursive search of directories |
4. locate |
D. Search using a periodic file name index |
5. grep -r |
E. Search file contents through a tree |
6. * wildcard |
F. Any sequence of characters in a name |
Answer key
1 → B, 2 → A, 3 → C, 4 → D, 5 → E, 6 → F
Practice: Fill in the blank#
- Symbolic link to
notes.txtnamedshortcut:ln ___ notes.txt shortcut - Find all
.logfiles under.:find . -name '___' - Case-insensitive grep for
errorinapp.log:grep ___ error app.log - Grep with line numbers:
grep ___ pattern file - Match any single-character
.txtname:ls ___.txt
Answers
-s*.log-i-n?
Practice: Mini terminal challenge#
Challenge: Symlink and search#
Goal: Create a symlink and use find/grep together.
Setup: cd /tmp/linux-lab-pushpjeet (recreate readme.txt if needed).
Tasks:
ln -s readme.txt home-readmeandls -l home-readmefind . -maxdepth 1 -type land confirm your symlink appearsgrep -n -i 'cli\|linux' readme.txt- Create three files
report1.logreport2.lognotes.bak; use a glob to list onlyreport*.log rm home-readmeand confirmreadme.txtstill exists
Verify: Symlink removal left the target intact; grep showed matching lines with numbers; glob listed only the report logs.
Stretch: Intentionally create a dangling symlink (ln -s missing-target broken-link) and observe ls -l and file broken-link.
Hands-on lab#
Goal: Links + discovery tools in one workflow.
Steps:
- In your lab directory, create
configs/app.confwith two or three settings lines. - Symlink it to
~/app.conf.linkonly if your home is writable and you are fine cleaning up — otherwise symlink inside the lab:ln -s configs/app.conf app.conf.link. - Use
find . -name '*.conf'to locate it. - Use
grep -n '='(or a key name) to show setting lines. - Create a hard link to a data file and prove shared inodes with
ls -i.
Verify: ls -l shows the symlink arrow; ls -i matches for the hard link pair; grep highlights your settings.
Stretch: Run man find and learn one predicate you have not used (-size, -user, or -mtime).
Common mistakes#
- Confusing shell globs with regular expressions in
grep - Forgetting to quote patterns for
find -name - Expecting hard links to work across different mount points
- Deleting a file and not noticing a dangling symlink still present
- Running recursive
greporfindon huge trees (like/) withoutpermissionawareness — stick to lab paths
Summary#
You can add hard and soft links, select names with globs, locate files with find (and locate when available), and search contents with grep. Together with Modules 0–5, you now handle everyday filesystem literacy on Ubuntu and RHEL-family systems alike.