UNIX commands with examples and syntax

The article about UNIX commands with examples and syntax. Here I have resume this article about UNIX commands and their syntax.

UNIX commands with examples and syntax

 

Displaying the contents of a file on the screen

clear (clear screen)

Before start the next section, you may like to clear the terminal window of the previous commands so the output of the following commands can be clarifying more.

Type

% clear

Above command will clear all text and leave you with the % prompt at the top of the window.

cat (concatenate)

CAT command is used to display the contents of a file on the screen.

Type:

% cat science.txt

The file is longer than the size of the window, so it scrolls past making it unreadable.

less

less command is use to writes the contents of a file onto the screen a page at a time.

Type

% less science.txt

Press the [space-bar] if you want to see another page, and type [q] if you want to quit reading. As you observed, less is used in preference to cat for long files.

head

The head command use to writes the first ten lines of a file to the screen.

First clear the screen then type


% head science.txt

Now type

% head -5 science.txt

tail

The tail command use to writes a last ten lines of a file to the screen

Clear the screen and type

% tail science.txt

Here one question introduce as, How can you view the last 15 lines of the file?

2.5 Searching the contents of a file

Simple searching using less

Using less, search though a text file for a keyword (pattern).

For example, to search through science.txt for the word ‘science’, type

% less science.txt

then, still in less (i.e. don’t press [q] to quit), type a forward slash [/] followed by the word to search

/science

As you already observed, less finds and highlights the keyword. Type [n] to search for the next occurrence of the word.

grep

grep is one of many standard UNIX utilities. It uses to searches files for specified words or patterns. First clear the screen, then type

% grep science science.txt

grep has printed out each line contain the word science.

Or has it????

Try typing

% grep Science science.txt

The grep command is case sensitive; it distinguishes between Science and science.

To ignore upper/lower case distinctions, use the -i option, i.e. type

% grep -i science science.txt

To search for pattern, you must enclose it in single quotes for example to search for spinning top, type

% grep -i ‘spinning top’ science.txt

Some of the other options of grep are:

-v display those lines that do NOT match

-n precede each matching line with the line number

-c print only the total count of matched lines

You can use more than one option at a time, for example, the number of lines without the words science or Science is

% grep -ivc science science.txt

wc (word count)

This is little utility is the wc command, short for word count. To do a word count on science.txt, type

% wc -w science.txt

To searches the how many lines the file has, type

% wc -l science.txt

I am sure that above information will give you more clarity and also will increase your awareness about UNIX commands with examples and syntax.

Share This Post

One Comment - Write a Comment

Post Comment