Linux & the Command Line

Reading and Searching Files

30 דק׳

Viewing files

cat readme.txt          # print the whole file
less big.log            # scroll (Space, b, /search, q)
head -n 20 app.log      # first 20 lines
tail -n 50 app.log      # last 50 lines
tail -f app.log         # follow new lines live (Ctrl+C to stop)
wc -l app.log           # count lines

tail -f is how you watch a server log while you reproduce a bug in another window.

grep: find text inside files

grep "ERROR" app.log            # lines containing ERROR
grep -i "error" app.log         # ignore case
grep -n "timeout" app.log       # show line numbers
grep -c "ERROR" app.log         # count matching lines
grep -v "DEBUG" app.log         # lines NOT matching
grep -r "SECRET_KEY" .          # search every file below here
grep -rl "TODO" src/            # only list file names
grep -E "error|warn" app.log    # extended regex: either word

Combine flags: grep -rni "password" config/ searches recursively, case-insensitively, with line numbers. That exact command is worth running on your own projects before you publish them.

find: find files by name, size or age

find . -name "*.py"                    # by name
find /var/log -name "*.log" -mtime -1  # changed in the last day
find . -type d -name node_modules      # folders only
find . -size +100M                     # bigger than 100 MB
find /tmp -type f -mtime +7 -delete    # delete files older than 7 days

Always run a find without -delete first and read the output.

Worked example: why is the site slow?

cd /var/log/nginx
tail -n 1000 access.log | grep " 500 " | wc -l   # how many server errors recently?
grep -i "upstream timed out" error.log | tail -5 # the most recent timeouts

Two commands, and you know whether the app server is timing out.

Practice

Complete the Read a file and Search a log missions. Then try grep -n WARN ~/logs/server.log.

Check yourself

  • Which command follows a log file as it grows?
  • How do you list only the names of files that contain TODO?
  • How do you find files bigger than 1 GB?

ההתקדמות שלכם נשמרת בדפדפן הזה בלבד.

שליחה לבדיקה (10 נקודות)