AndroidPub
Published in

AndroidPub

Search (grep && find) Commands

Search

For a lot of us, the fact that we can plug our Android phone into a computer and interact with it is a big plus. Besides the times when we’ve broken something and need to fix it, there are plenty of reasons why an advanced Android user would want to talk to their device. To do that, you need to have a few tools and know a few commands. That’s what we’re going to talk about today.

So here I will discuss some basic search commands:-

  • grep
  • find

grep

Grep command used for searching text. Grep is a command-line tool to search for regular expressions. Grep will print the matching line to the output and with the --color flag you can highlight the matching strings.

command format:

grep [options]… pattern [file]…

grep find rules … regular expression view file

Finding rules

finding rules

Regular Expression

Pattern regular expression main parameters:

  • \ : Ignore the original meaning of special characters in regular expressions.
  • ^ : Matches the beginning of the regular expression.
  • $ : Matches the end line of the regular expression.
  • < : Start with a line that matches the regular expression.
  • > : To the end of the line that matches the regular expression.
  • [] : A single character, such as [A] means A meets the requirements.
  • [-] : Range, such as [A-Z], that is, A, B, C, and Z all meet the requirements.
  • . : All single characters.
  • * : There are characters, the length can be 0.

Example

  • Ignore case search
grep -i "androiD" logcat.txt // From the logcat.txt file, search for text lines containing android, which is not case sensitive
  • Traverse search without showing no matching information
grep -rs "android". // From the current directory, iterate through all files and search for lines containing android
  • Whole word search
grep -w "android" logcat.txt // From the logcat.txt file, search for text lines containing the word android

grep -w "android | ios" logcat.txt // From the logcat.txt file, search for text lines containing the word android or ios
  • List only file names
grep -l "android".
  • Count character occurrences
grep -c "android".
  • Show line where characters appear
grep -n "android“ .
  • Show multiple condition matches
grep -E "android|linux“ .

Find

Find command in UNIX is a command-line utility for walking a file hierarchy. It can be used to find files and directories and perform subsequent operations on them. It supports searching by file, folder, name, creation date, modification date, owner and permissions. By using the ‘-exec’ other UNIX commands can be executed on files or folders found.

So in short find command is used for searching files.

Command Format

find pathname -options [actions]
find Find Directory-Find Rule [Execute Action]

Find Directory

  • If you do not write, the default is the current path.
  • Multiple paths are supported, and directories are directly separated by spaces
find .-name demo
  • View the number of files in a directory
find . -type f  |wc -l

Finding Rules

  • By fileName
-name // Find by file name, case sensitive -iname // Find by file name, not case sensitiveWildcard description:
* matches any number of characters.
? Matches any single character.
[] matches any character in parentheses
Example:
find /data -name dalvi*
find /data -name dalvik?cache
find /data -name dalvik-cach[abe]
  • According to file type
f : Ordinary files 
d : directory file
l : link file
b : block device file
c : character device file
p : pipeline file
s : socket file
Example:
find -type f // View file type
  • According to the directory depth
-maxdepth n: find maximum depth is n 
-mindepth m: find the minimum depth of m
  • According to the size
Units: c (lowercase), k (lowercase), M (uppercase), G (uppercase)  
-size + 10M: find files larger than 10M
-size -2k: find files smaller than 2k
-empty: find files or empty directories of size 0
  • Per file permission
find -perm 777 // Find files with permissions 777
  • By user and group which to which the file belongs
-user: find files by owner 
-group: find files by group
  • According to the uid and gid
-uid 500: find files with uid 500
-gid 1000: find files with gid 1000
  • According to the time
You can use the stat command to view the time of the file. The following is to find the file according to the time of the file:

-mtime -n + n: According to the modify time, -n means within n days, + n means n days ago
-atime -n + n: According to access time, -n means within n days, + n means n days ago -ctime -n + n: According to the creation time, -n means within n days, + n means n days ago -mmin -n + n: According to the modification time, -n means within n minutes, + n means n minutes ago -amin -n + n: According to access time, -n means less than n minutes, + n means n minutes ago -cmin -n + n: According to the creation time, -n means less than n minutes, + n means n minutes ago
  • Multi conditional join
-a: both conditions are met (and)
-o: two conditions satisfy one (or)
-not: Invert the condition (not)
For example, to find files or folders in the current path that start with a and exclude files that end with b:find -name a* -not -name *b

Take Action

  • -print Match file output to standard output, the default action.
  • -ls Find results, display in ls
find -name app -ls
  • -ok [command] After the search is completed, execute command execution and ask for execution
find -name app -ok cat {} \ // Note: there are spaces before and after {}
  • -exec [command] After the search is completed, execute the command and execute directly
find -name app -exec ls {} \

In this article, I have mentioned about search commands. I will write some more articles for other useful commands so to know about that please keep following and clapping.

I hope you enjoyed this session. If you have any comments or questions, please join the forum discussion below!

Thanks for the support :)

Android Developers AndroidPIT.com AOSP Extended Android Android Dev Perú Head First Android

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store