[Android General] tuxboy:
Using various command lines tools of Linux can make working very easy for developers. Here are a few tricks than can really help out.

1. grep

I cannot emphasize on this tool more, this utility is your one stop destination for searching anything. Want to look for all .so files in a folder? Type in

ls | grep .so

Or maybe a '.so' (library) file that starts with 'mtk' (e.g. mtkOmxVenc.so)

ls | grep mtk*.so

Or what if a '.so' (library) file that has 'Omx' somewhere in the name

ls | grep *Omx*.so

Awesome! Now that pole like character is simply called "the pipe" it pipes the output to another program, in this case grep. Now grep -s case-sensitive. To make it case insensitive, just use the -i flag with it. Now try looking for all .mp3 files in a folder!

What if you were in a folder with 1000s of files and were looking for a file with the text 'I love XDA Developers' in it? Pretty simple. Make sure your current directory is where you want to search, and type in

grep -R 'I love XDA Developers'

Great! You are becoming a command line genius! Now let's try some real world stuff. Say you want a rough idea on how many libraries or executables are somehow linked to 'libmediaservice.so'. It can be necessary when you get dlopen errors. Can you guess how to get an approximate result? Try to do it before looking any furter. Got it? 3... 2... 1... and the answer is

grep -R libmediaservice.so

Now was that too hard? Of course not!

2. head and tail

Quickly view the first few or last few lines of a text file with head or tail subsequently.

head logfile.log

tail logfile.log

3. sed

sed, short for stream editor can do tonnes of things, for now let's focus on searching and replacing. Say, we need to replace the text 'libxda.so' by 'libxdadev.so' in all the files within this current directory. We would simply command,

ls -1 | xargs sed -i 's/libxda.so/libxdadev.so/g'

(The -1 flag makes sure that ls ouputs in just ONE row, there is a better way to do this with find, we'll check that later)

4. find

This is the hypercharged version of grep; as the name implies, you can find stuff with it. For example, say you want to find all the folders under the current folder recursively.

find . -type d

[tip: . (dot) refers to the current folder]

you want to find all the files under the current folder recursively.

find . -type f

That was easy, but not too much useful. Let's try something more. Okay, here's a fun thing to do, we want to find empty files in the current folder whose name starts with 'xda', has executable permissions and is owned by 'tuxboy'.

find . -type f -name xda* -empty -executable -user tuxboy

Whoa! That was big! This baby is capable of doing so much more. Here are a few more options, self explanatory.

-empty
-fstype TYPE
-group NAME
-name PATTERN -newer FILE
-nouser
-nogroup
-path PATTERN
-perm [+-]MODE
-regex PATTERN
-readable -writable -executable
-wholename PATTERN
-size N[bcwkMG]
-type [bcdpflsD]
-user NAME

5. wc

This is the mother of all counters. You can count bytes, characters or lines in files with this tool. Want to know how many characters are there in a file?

wc -m filename.txt

Or, how many words are there in a file?

wc -w filename.txt

Or, how many lines are there in a file?

wc -l filename.txt

Or, the length of the longest line in a file?

wc -L filename.txt

Cool eh?

6. man and --help/-help/-h

Every command usually comes with some documentation. Say you want to get a short documentation on how to use the 'ls' command, at least one of the following commands should give you answers.

ls --help

ls -h

ls -help

If you want comprehensive documentation. Man is there for you!

man ls

Hope this will help you. If you have learned something new, hit Thanks!