CertNote/ Commute Study Notes
Roadmap
KOEN
CLF-C02 · FoundationalCloud Practitioner - Foundational
DVA-C02 · AssociateDeveloper - Associate
SAA-C03 · AssociateSolutions Architect - Associate
SOA-C02 · AssociateCloudOps Engineer - Associate
SAP-C02 · ProfessionalSolutions Architect - Professional
DOP-C02 · ProfessionalDevOps Engineer - Professional
Linux Master 2 · 2급KAIT Linux Master - Level 2
  • Week 1
    • 1.Operating Systems and the Kernel: What Linux Does
    • 2.The Birth of Linux: From Unix to Linus
    • 3.Free Software and Open Source: What a License Decides
    • 4.Distributions: The Unit That Bundles the Kernel and Tools for Sale
    • 5.Week 1 Review: The Identity Behind the Name Linux
  • Week 2
  • Week 3
    • 1.First Steps in the Shell: How to Read a Single Command Line
    • 2.Working with Directories and Files
    • 3.Viewing and Finding Contents
    • 4.User and Account Management
    • 5.Week 3 Review: The Commands on One Page
  • Week 4
  • Week 5
  • Week 6
  • Week 7
  • Week 8
  • Week 9
  • Week 10
  • Week 11
SCS-C03 · SpecialtySecurity - Specialty
Linux Master 1 · 1급KAIT Linux Master - Level 1
MLA-C01 · AssociateMachine Learning Engineer - Associate
AIF-C01 · FoundationalAI Practitioner - Foundational
DEA-C01 · AssociateData Engineer - Associate
MLS-C01 · SpecialtyMachine Learning - Specialty
SQLD · 개발자SQL Developer (SQLD)

© CertNote · 주간랩스

  • About
  • Changelog
  • Contact
  • Terms
  • Privacy
← Linux Master 2/Week 3/Day 3

Based on the Linux Master 2 exam syllabus·Report an error

Linux Master 2· AssociateWeek 3 · Day 3~18 min read

Day 3 - Viewing and Finding Contents

📌 Key Takeaways

  • Four ways to view contents: cat (all at once) · less (page through) · head (the beginning) · tail (the end).
  • tail -f follows new content appended to the file in real time. Use it for viewing logs.
  • find finds files, and grep finds strings inside files. This distinction is an exam point.
  • find is written path first — find /etc -name "*.conf".
  • wc counts lines, words, and characters. wc -l is the line count.

Viewing File Contents

CommandWhat it doesWhen
cat filePrints all the contentsShort files
less filePage through one screen at a timeLong files
more fileAn older version of less. Can't go backward
head fileFirst 10 linesChecking the start of a file
tail fileLast 10 linesChecking recent logs
wc fileLine, word, and character countsMeasuring size
head -n 5 a.txt      # first 5 lines
tail -n 20 a.txt     # last 20 lines
tail -f /var/log/messages   # follow in real time
wc -l a.txt          # line count only
  • The default for head and tail is 10 lines. Use -n to change the count.
  • The f in tail -f stands for follow. Use it to watch a log that keeps growing, and get out with Ctrl+C.

Specifying the Line Count for head · tail, in Four Shapes

UsageMeaning
head -n 5 / head -5First 5 lines
tail -n 5 / tail -5Last 5 lines
tail -n +5From line 5 to the end (with a + it is the starting position)
head -n -5Everything except the last 5 lines
head -c 100First 100 bytes, not lines
head -n 20 a.txt | tail -n 5     # only lines 16-20 (the last 5 of the first 20)
tail -n +2 list.csv              # everything except the first (header) line
tail -f -n 50 /var/log/messages  # show the last 50 lines, then keep following
  • To pull out a middle section of a file, chain head and tail with a pipe. The first line above is the classic example.
  • tail -f is not a command that modifies the file. It only reads, and prints new lines appended at the end to the screen.

The Difference Between more and less

Aspectmoreless
NavigationBasically you page forwardYou can move freely forward and backward
When you reach the endIt exits automaticallyIt stays until you press q
Search/word/word (downward) · ?word (upward)
  • Both are grouped as "commands that show one screen at a time," but the dividing line is that the one you can scroll back in is less. Depending on the implementation, more can also go back one screen with b when viewing a file, but the exam asks using this contrast.
  • Both are used after a pipe to break up long output — ls -l /etc | less.

Reading wc Output

$ wc /etc/passwd
  42   78 2104 /etc/passwd
   │    │    │
   │    │    └── byte count
   │    └─────── word count
   └──────────── line count
OptionWhat it counts
-lLines (line)
-wWords (word)
-cBytes
-mCharacters
  • Used with no options, it prints three numbers in the order lines → words → bytes. The order shows up in exam questions.
  • For a file with only English text, -c and -m are the same, but they differ once Korean (Hangul) text is mixed in. That is because in UTF-8 a single Hangul character is 3 bytes.
  • In /etc/passwd each line is one account, so wc -l /etc/passwd is exactly the number of accounts.

Inside less

KeyWhat it does
Space / bNext / previous screen
/wordSearch forward
n / NNext / previous search result
G / gTo the very end / to the very start
qQuit
  • man pages are actually displayed with less, so the controls are the same.

Other Uses of cat

cat a.txt b.txt          # print two files back to back (concatenate)
cat a.txt > b.txt        # save the contents to b.txt (overwrite)
cat -n a.txt             # print with line numbers
  • cat is short for concatenate. It is originally a command that prints several files joined together.
cat > memo.txt           # save what you type on the keyboard to a file. End with Ctrl+D
cat a.txt >> b.txt       # append to the end of b.txt (keeping existing contents)
cat -b a.txt             # number only the non-blank lines
  • > overwrites and >> appends. Be careful: using the same file for both input and output, like cat a.txt > a.txt, empties its contents.
  • Typing just cat with no arguments waits for keyboard input. It hasn't frozen, so get out with Ctrl+D (end of input).

Finding Files — find

   find  /etc  -name "*.conf"
    │      │        │
    │      │        └── condition
    │      └─────────── where to start looking (required)
    └────────────────── command
ConditionMeaning
-name "pattern"By name (case-sensitive)
-inameBy name (ignoring case)
-type f / -type dFiles only / directories only
-size +10MLarger than 10MB
-user nameBy owner
-mtime -7Modified within the last 7 days
find /home -name "*.txt"        # by name
find / -type d -name "log"      # directories only
find . -size +100M              # larger than 100MB
find /var -mtime -1             # changed within a day
  • Always write the path first. find -name "*.txt" /home does not work.
  • Wrap the pattern in quotes. If you don't, the shell first replaces *.txt with the file names in the current directory, and find receives unexpected arguments.

The + and - in Front of a Number

For conditions that take a number, like -size and -mtime, the sign changes the meaning.

NotationMeaningExample
+nGreater than n / older than n-size +10M over 10MB, -mtime +7 modified more than 7 days ago
-nLess than n / more recent than n-size -10k under 10KB, -mtime -7 modified within the last 7 days
nExactly n-mtime 7 exactly 7 days ago
Time conditionBasis
-mtimeContent modification time (in days)
-atimeLast access time
-ctimeAttribute change time
-mminModification time in minutes
  • The units for -size are c bytes · k KB · M MB · G GB. They are case-sensitive, so -size +10m is an error.

Combining Conditions and Running a Command on What Was Found

find /home -name "*.log" -size +1M       # two conditions side by side is AND
find /etc -name "*.conf" -o -name "*.cfg" # -o is OR
find /tmp ! -user root                    # ! is NOT — not owned by root
find /var/log -type f -empty              # files with size 0
find /usr -perm 644                       # exactly permission 644
find . -maxdepth 1 -type d                # descend only one level
find /tmp -name "*.tmp" -mtime +7 -exec rm {} \;
#                                  │      │  │
#                                  │      │  └── end marker for -exec (\ protects the ;)
#                                  │      └───── where the found file name goes
#                                  └──────────── run this command on every file found
  • {} receives the found file names one at a time, and \; marks the end of the command. These two are the fixed pair of -exec.
  • Before a delete command, it is safer to run it first without -exec to check what gets matched.
  • find actually searches the disk, so it is slow. For quickly finding just names, there is locate, which uses a prebuilt index (refresh the index with updatedb).
Aspectfindlocate
MethodSearches the disk directlySearches a prebuilt DB
SpeedSlowFast
FreshnessAlways accurateCan't find files from before the index refresh
ConditionsName, size, time, owner, permissions, etc.Mostly name (path)
$ touch /tmp/newfile.txt
$ locate newfile.txt
$ sudo updatedb
$ locate newfile.txt
/tmp/newfile.txt
  • A file you just created not showing up in locate is not a malfunction. The index is usually refreshed automatically once a day, and to reflect changes right away, run updatedb as root.

There are more "finding" commands, and they get mixed together in exam questions. Tell them apart by what they look for.

CommandWhat it looks forWhere it looks
findFiles and directories matching conditionsEverything under the specified path
locateFiles whose names contain a stringA prebuilt index
whichThe location of a command's executableThe directories listed in PATH
whereisA command's executable plus its manual and source locationsFixed standard directories
grepLines inside filesThe contents of the specified files
$ whereis ls
ls: /usr/bin/ls /usr/share/man/man1/ls.1.gz

Finding Contents — grep

grep "root" /etc/passwd          # print lines containing that word
grep -i "ERROR" app.log          # ignore case
grep -n "root" /etc/passwd       # with line numbers
grep -v "root" /etc/passwd       # only lines without that word (inverse)
grep -r "TODO" /home/user        # recursively through subdirectories
grep -c "root" /etc/passwd       # count only
OptionMeaning
-iIgnore case
-nShow line numbers
-vLines that don't match (invert)
-rThrough subdirectories
-cThe number of matching lines
-wMatch whole words
-lFile names only instead of matching lines
-EUse extended regular expressions (same as egrep)
$ grep -n "root" /etc/passwd
1:root:x:0:0:root:/root:/bin/bash
10:operator:x:11:0:operator:/root:/sbin/nologin
$ grep -c "root" /etc/passwd
2
$ grep -w "root" /etc/passwd | wc -l
2
$ grep "root" /etc/passwd /etc/group
/etc/passwd:root:x:0:0:root:/root:/bin/bash
/etc/passwd:operator:x:11:0:operator:/root:/sbin/nologin
/etc/group:root:x:0:
  • grep works line by line. It prints not just the matching text but the entire line that contains it.
  • -c is the number of matching "lines". Even if root appears three times on one line, it counts as 1.
  • If you give two or more files, file name: is prepended to each line.
  • Don't mix them up with the uppercase options — -i is ignore case, -l is file names only, and -v is the inverse.

Symbols Used in Patterns

A grep pattern is not a simple string but a regular expression. You can specify where in the line something sits.

SymbolMeaningExample
^Start of linegrep "^root" /etc/passwd — lines that start with root
$End of linegrep "bash$" /etc/passwd — lines that end with bash
.Any single charactergrep "r..t" — root, r00t
[ ]One of the characters in the bracketsgrep "[0-9]" — lines containing a digit
^$Blank linegrep -v "^$" a.conf — everything except blank lines
grep "bash$" /etc/passwd | wc -l     # number of accounts whose login shell is bash
grep -v "^#" /etc/fstab              # only lines without comments (starting with #)
grep -E "error|fail" app.log         # lines containing error or fail

⚠️ The * in wildcards and the * in regular expressions are different. In file names * means "any string," but in a grep pattern * means "zero or more repetitions of the character just before it." The pattern for find -name is a wildcard, and the pattern for grep is a regular expression.

find vs grep — finding files by name is find, and finding contents inside files is grep. When a question says "lines that contain a specific string," it is grep.

Chaining Together — Pipes

ls -l | grep ".txt"          # only .txt from the ls result
cat a.txt | wc -l            # line count of a.txt
ps -ef | grep httpd          # httpd among the running processes
grep "ERROR" app.log | head  # the first 10 of the error lines
  • | (pipe) passes the output of the previous command as the input of the next command.
  • Each command is simple on its own, but chained together they become powerful — this is the Unix design philosophy.
  • | sends to a command, > sends to a file. ls | wc -l passes to a command called wc, while ls > wc ends up creating a file named wc.
  • You can chain as many pipes as you like — cat /etc/passwd | grep bash | wc -l.
$ wc -l /etc/passwd
42 /etc/passwd
$ cat /etc/passwd | wc -l
42
  • When you give a file as an argument, the name is printed too, and when it receives input through a pipe, only the number is printed. That is because wc was handed just the contents without knowing the file name.

Tomorrow we will learn the commands for dealing with the people who use the system, that is, users and accounts.

📖 Glossary

  • cat : A command that prints file contents joined together. Short for concatenate.
  • less : A pager for viewing long files one screen at a time. man is also displayed with it.
  • tail -f : An option that follows and prints content newly added to a file in real time.
  • find : A command that finds the files themselves matching conditions on disk.
  • grep : A command that finds lines containing a string inside files.
  • locate : A command that quickly finds file names from a prebuilt index.
  • Pipe (|) : A symbol that connects the output of the previous command to the input of the next command.
  • wc : A command that counts lines, words, and characters. -l is the line count.

📝 Practice Questions

Click a choice to reveal the answer and explanation.

Question 1

Which of the following commands is used to keep checking, in real time, content newly written to a file?

Question 2

Which of the following commands finds files with the .conf extension under the /etc directory?

Question 3

Which of the following commands finds lines containing a specific string inside a file?

Question 4

Which of the following is the result of the command grep -v "root" /etc/passwd?

Question 5

What is the number of lines that the head and tail commands print by default, with no options?

PreviousWorking with Directories and FilesWeek 3 · Day 2Next User and Account ManagementWeek 3 · Day 4

Official references

  • Linux Master 2 official exam syllabus(opens in a new tab)

On this page

  • Key Takeaways
  • Viewing File Contents
  • Specifying the Line Count for head · tail, in Four Shapes
  • The Difference Between more and less
  • Reading wc Output
  • Inside `less`
  • Other Uses of `cat`
  • Finding Files — find
  • The `+` and `-` in Front of a Number
  • Combining Conditions and Running a Command on What Was Found
  • Finding Contents — grep
  • Symbols Used in Patterns
  • Chaining Together — Pipes
  • Glossary
  • Practice Questions