Day 2 of 5
⏱ ~60 minutes
Linux in 5 Days — Day 2

Files: Reading, Writing, Searching

Read files with cat/less, edit with nano, search with grep and find, and chain commands with pipes.

Reading Files

Reading commands
cat file.txt           # print entire file
less file.txt          # paginated view (q to quit)
head -n 20 file.txt    # first 20 lines
tail -n 20 file.txt    # last 20 lines
tail -f /var/log/syslog  # follow in real-time
Editing with nano
nano file.txt
# Ctrl+O = save
# Ctrl+X = exit
# Ctrl+W = search
# Ctrl+K = cut line
# Ctrl+U = paste
grep: Search File Content
grep 'error' app.log                    # lines containing 'error'
grep -i 'error' app.log                 # case-insensitive
grep -r 'TODO' ./src/                   # recursive search
grep -n 'error' app.log                 # show line numbers
grep -v 'DEBUG' app.log                 # lines NOT matching
grep -E 'error|warning' app.log         # regex: error OR warning
grep -c 'error' app.log                 # count matching lines
find: Search for Files
find . -name '*.txt'                     # by name
find . -name '*.log' -newer config.txt   # modified after
find /var/log -size +10M                 # files over 10MB
find . -type f -name '*.py' -exec grep -l 'import os' {} \;
Pipes: Chain Commands
# | passes output of one command as input to the next
cat app.log | grep 'error' | wc -l      # count error lines
ls -la | sort -k5 -n                     # sort by file size
ps aux | grep nginx                      # find nginx processes
cat /etc/passwd | cut -d: -f1 | sort    # list usernames, sorted
📝 Day 2 Exercise
Search Logs
  1. D
  2. o
  3. w
  4. n
  5. l
  6. o
  7. a
  8. d
  9. a
  10. s
  11. a
  12. m
  13. p
  14. l
  15. e
  16. l
  17. o
  18. g
  19. f
  20. i
  21. l
  22. e
  23. (
  24. o
  25. r
  26. u
  27. s
  28. e
  29. /
  30. v
  31. a
  32. r
  33. /
  34. l
  35. o
  36. g
  37. /
  38. s
  39. y
  40. s
  41. l
  42. o
  43. g
  44. )
  45. .
  46. F
  47. i
  48. n
  49. d
  50. a
  51. l
  52. l
  53. e
  54. r
  55. r
  56. o
  57. r
  58. l
  59. i
  60. n
  61. e
  62. s
  63. .
  64. C
  65. o
  66. u
  67. n
  68. t
  69. t
  70. h
  71. e
  72. m
  73. .
  74. F
  75. i
  76. n
  77. d
  78. u
  79. n
  80. i
  81. q
  82. u
  83. e
  84. I
  85. P
  86. a
  87. d
  88. d
  89. r
  90. e
  91. s
  92. s
  93. e
  94. s
  95. i
  96. n
  97. a
  98. n
  99. n
  100. g
  101. i
  102. n
  103. x
  104. a
  105. c
  106. c
  107. e
  108. s
  109. s
  110. l
  111. o
  112. g
  113. .
  114. U
  115. s
  116. e
  117. f
  118. i
  119. n
  120. d
  121. t
  122. o
  123. l
  124. o
  125. c
  126. a
  127. t
  128. e
  129. a
  130. l
  131. l
  132. .
  133. c
  134. o
  135. n
  136. f
  137. f
  138. i
  139. l
  140. e
  141. s
  142. i
  143. n
  144. /
  145. e
  146. t
  147. c
  148. .

Day 2 Summary

  • cat for quick reads, less for long files, tail -f for live logs.
  • grep -r 'pattern' dir/ searches recursively through all files in a directory.
  • Pipes | are the Unix superpower: chain simple tools into complex operations.
  • find . -name '*.txt' finds files. Add -exec command {} \; to act on each result.
Finished this lesson?