Introduction to Linux
Basic commands for your research
Useful Linux commands
Some of the general tips for Linux commands
How to use grep, find , wc, and diff commands
How to extract and archive files
How to use other's Linux...when you do not have your own Linux system.
How to use grep, find, wc, and diff commands
The grep command can search a specific string in text files. The following explains the possible options:
|grep ||-c ||This counts the number of lines of
specified string in a file or multiple files.|
| ||-i||The option ignores the case for the specified
| ||-r / -R||The option searches a specific
string in the directory and all of the subdirectories.|
Some of useful examples are:
This searches the lines containing either "em" or
"am" in the file EM.cpp. Note that you have to type backslash, \, before typing
$ grep "em" EM.cpp | grep "am"|
This means that it looks for lines containing "am" in which lines containing "em" in the file EM.cpp.
In other words, this shows lines containing both "am" and "em."
The "find" command has several options as others do; but instead of listing them, introducing the examples
should be appropriate for the explanation purpose. The following examples filter the files based on the size:
This finds a file that the size is exactly 500 bytes. The "c" specifies the file size.
This looks for more than 500 bytes and, as you can expect, "find -size -500c" will find
files that are less than 500 bytes. The find command can find directory and file names.
After typing "find", specify the place and the directory or file name you are looking for.
The option "-name" can be omitted. The slash, /, expresses the entire file system and
"*.pdf" indicates any pdf file.
The "wc" (word count) command has the following options:
|wc ||-c ||This displays the byte size.|
| ||-m||The option counts file's character.|
| ||-l||The option counts the lines.|
| ||-w||The option counts the words.|
| ||-L||The option displays the length of the longest line.|
Here is an example:
$ wc EM.cpp|
40 99 702 EM.cpp
Without options, the default shows, 40 lines, 99 words, and 702 bytes for the file, EM.cpp.
The diff command shows the differences of two files. The options are:
|diff ||-i||This ignores the cases.|
| ||-q||The option only shows that they are different without the detailes.|
| ||-s||This tells even when they are identical.|
| ||-w||This ignores the spaces for comparison.|
To compare two similar files, type as follows:
The result shows the line numbers and both content expressions. The options can be inserted right after diff command.
Back to Electronics Page
Back to Hiro's Physics Main