Linux Commands - Search


  • Searching and quickly finding Information is an important skill a developer must have.

history

iTerm2

  • Show/Search Command History: Cmd+Shift+;

grep

-r–recursive
-epattern
-E–extended-regexp
-iIgnore uppercase vs. lowercase
-vInvert match
-cOutput count of matching lines only
-lOutput matching files only
-n(–line-number)Precede each matching line with a line number.
-s(–no-messages)Silent mode
-A NUM–after-context=NUM
-B NUM–before-context=NUM
-C NUM–context=NUM
  • grep -r –exclude=*.{html,htm,js} pattern rootdir
  • Grep file that contains binary data
    • cat -v tmp/test.log | grep regex
    • -v: Display non-printing characters so they are visible.
  • Find multiple patterns across multiple lines
    • find . | xargs grep ‘pattern1’ -sl | xargs grep ‘pattern2’ -sl

find

-maxdepth levelsDescend at most levels (a non-negative integer) levels of directories below the starting-points. -maxdepth 0 means only
-print0print the full file name on the standard output, followed by a null character (instead of the newline charactes that -print uses)
-exec command ;{} expands to the filename of each file or directory found with find, ; Says that for each found result, the command (in this case ‘grep’) is executed once with the found result
-exec command {} +run the specified command on the selected files, but the command line is built by appending each selected file name at the end; the total number of invocations of the command will be much less than the number of matched files.
  • find /usr -size +10M
  • find . -exec xxx {} ;
  • Find configuration data
    • find /etc -type f -exec grep -FHi “ENCRYPTION_PASSWORD” {} +
    • grep -r ENCRYPTION_PASSWORD /etc 2>/dev/null
    • find /etc -type f -print0 | xargs -0 grep ENCRYPTION_PASSWORD
    • Find biggest files
      • find ~/ -type f -exec du -Sh {} + | sort -rh | head -n 5
  • find -exec vs. find | xargs

sed

  • Search specific line ranges:
    • sed -n ‘startLine,endLinep’ a.txt | grep XX

xargs

–null, -0Input items are terminated by a null character instead of by whitespace, handle space or new line in file name. The find -print0 option produces input suitable for this mode
-I replace-strReplace occurrences of replace-str in the initial-arguments with names read from standard input.
-n max-args, –max-args=max-argsUse at most max-args arguments per command line, default 5000
-tprint each command prior execution
-pprint each command and ask to execute it
-xmake xargs quit if the nummber of arguments does not fit into the command line length

Notes

  • Use right tool for different tasks
    • For simple search, search directly in your tools like Atom
    • For complex search, much easier to use Linux commands

Labels

adsense (5) Algorithm (69) Algorithm Series (35) Android (7) ANT (6) bat (8) Big Data (7) Blogger (14) Bugs (6) Cache (5) Chrome (19) Code Example (29) Code Quality (7) Coding Skills (5) Database (7) Debug (16) Design (5) Dev Tips (63) Eclipse (32) Git (5) Google (33) Guava (7) How to (9) Http Client (8) IDE (7) Interview (88) J2EE (13) J2SE (49) Java (186) JavaScript (27) JSON (7) Learning code (9) Lesson Learned (6) Linux (26) Lucene-Solr (112) Mac (10) Maven (8) Network (9) Nutch2 (18) Performance (9) PowerShell (11) Problem Solving (11) Programmer Skills (6) regex (5) Scala (6) Security (9) Soft Skills (38) Spring (22) System Design (11) Testing (7) Text Mining (14) Tips (17) Tools (24) Troubleshooting (29) UIMA (9) Web Development (19) Windows (21) xml (5)