When you create some scripts, or when you need to find some specific source code files, search commands are a must have for such tasks.
Let's review a few of them to improve your usage of Haiku.
Command | Description |
grep | Searches for patterns using regular expressions in files or output |
egrep | Extended grep, supports extended regular expressions for more complex patterns |
fgrep | Fixed grep, searches for literal strings (no regex interpretation) |
find | Recursively searches for files and directories based on name, size, date, and other criteria |
lsindex (Haiku) | Lists all indexed file attributes available on the BFS volume |
query (Haiku) | Searches BFS file attributes using logical conditions and indexed metadata |
finddir (Haiku) | Returns the path to standard Haiku directories (settings, system, user folders,...) |
This command is very very useful :)
Let's suppose you have downloaded the Haiku source code and you would like to identify which part of the source is using the "BGLView" class.
In a Terminal, type :
cd Documents/C/haiku/src
grep -r "BGLView" .
Great !
You will see all the files and the corresponding lines containing the pattern searched.
What about having a more compact result ?
If you use the "-l" option, only the filename containing the pattern will be displayed :
grep -rl "BGLView " .
The grep command is powerful because it can use regular expressions.
If you need to search for "BGLView", followed by any character, and then an opened parenthesis, then type :
grep -r "BGLView.*(" .
The below results return some calls to functions from BGLView.
If you need to identify all the header files containing "BGLView", type :
grep -r --include="*.h" "BGLView" .
And if you want to identify the line number where the pattern was found in the file, use the "-n" option :
grep -rn "BGLView.*(" .
A great feature of Haiku's Terminal, is that if you hit "Alt+Left click" on one of the resulting lines, it will open automatically the file to the desired line number like below :
All of these features are out of the box !
If you need to use more complex search patterns, you can use "egrep" or grep with the "-E" option ("E" means "extended").
To search for both "LockGL" or "UnLockGL" words, type :
grep -Er '(LockGL|UnLockGL)' .
Thus you will identify quickly in the Haiku's source code, the examples which are using these OpenGL commands :)
Are you curious about all the "TODO" or "FIXME" remaining in the Haiku's source code ?
For that type :
grep -Er '(TODO|FIXME)' .
You can now try to improve things ;)
If you don't need to use regular expression, the "fgrep" or grep with the "-F" option can be used ("F" stands for "fixed").
For instance if you want to search for the exact wording ".Lock", in a Terminal type :
grep -Fr ".Lock" .
As you can see, the results will return the exact matching words.
If you were using the same with "grep" command, the "." character would mean any character, so you should have additional results compared to "fgrep".
Do you need to identify the header resource files ?
For that, type :
find . -name "Resource*.h"
The find command is useful to find files corresponding to specific filenames.
Now let's talk about Haiku's specific search commands !
In order to identify which attributes can be searched inside your BFS volume, use the "lsindex" command as below :
lsindex | grep name
In the above example, we have search all the searchable attributes which contain "name" in their name :)
Now let's search for attributes' values !
The corresponding command to use is "query".
In a Terminal type :
query '(META:name == DigitalBox)'
query '(MAIL:name == DigitalBox)'
The first command will return all the people's files with the exact contact name "DigitalBox"
The second one, will return all the email's files with the exact contact's name "DigitalBox".
As you can see, no result was return for the second query.
Now let's open the first file return regarding the people contact :
open /boot/home/people/DigitalBox
As you can see, the query command was working as expected :)
If you want to know more about the possible attributes for an application - like people - open FileTypes and in the application go to "Person" :
All the available attributes are displayed in the "Extra attributes" section.
Click on one of the attributes, to identify it's exact name (ie Internal name) :
The internal name will be the one which can be used with the query command.
The equivalent of the query command is the "Find" feature which is available in the Deskbar :
In the Find panel, select "application/Person" :
Then select "by attribute" :
Then select "Name" and fill the "contains" with the expected value :
Click for "Search".
As you can see the same below contact is identified :
Please note the equivalent of "contains" will be the below wit the query command :
query '(META:name == *DigitalBox*)'
Indeed the "*" characters will be needed.
Let's finalize this article with the Haiku's command named "finddir".
If you are not familiar with all the default folders to be used with Haiku, this command is quite useful.
In order to list all the kind of folders available type :
finddir -l
Do you want to identify in your scripts the common fonts directory or the documentation directory ?
For that type :
finddir B_COMMON_FONTS_DIRECTORY
finddir B_BEOS_DOCUMENTATION_DIRECTORY
Do you want to share other search commands available for Haiku ?
You can put a comment below.