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.
-iThe option ignores the case for the specified string.
-r / -RThe option searches a specific string in the directory and all of the subdirectories.
Some of useful examples are:
$ grep "em\|am" EM.cpp
This searches the lines containing either "em" or "am" in the file EM.cpp. Note that you have to type backslash, \, before typing pipe, |.
$ 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:
$ find -size 500c
This finds a file that the size is exactly 500 bytes. The "c" specifies the file size.
$ find -size +500c
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.
$ find / -name *.pdf
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.
-mThe option counts file's character.
-lThe option counts the lines.
-wThe option counts the words.
-LThe 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 -iThis ignores the cases.
-qThe option only shows that they are different without the detailes.
-sThis tells even when they are identical.
-wThis ignores the spaces for comparison.
To compare two similar files, type as follows:
$ diff file1 file2
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