Skip to content

1. Linux/UNIX shell basics#

This page is not an activity to follow, but rather a very small reference guide to some common UNIX/Linux commands that may come in useful during the activities in case using the bash Linux/UNIX shell is new to you.

In this page we have at your disposal some useful bash tips for managing files in the Linux/UNIX shell (CLI). You can review how to read a file, search for a string on the file, replace a string and some other handy hints.

1.1 cat#

The cat command in Linux displays a files contents. It reads one or more files and prints their content to the terminal. cat can be used to view file contents, combine files, and create new files.

1.1.1 Basic usage#

cat [option] [filename]

Now let’s go on to some practical examples of using cat. To better understand the results, a simple text file has been created. The file contains the following lines:

Hi 
this 
is test file 
to carry out a few 
operations with cat 
123 456 
Abcd 
ABCD 
cat testfile 
Hi 
this 
is test file 
to carry out a few 
operations with cat 
123 456 
Abcd
ABCD
cat -n testfile 
     1  Hi
     2  this
     3  is test file
     4  to carry out a few 
     5  operations with cat
     6  123 456
     7  Abcd
     8  ABCD

1.2 echo#

The echo command is a built-in command generally used to display the text or message on the screen. The echo command can also be used to write text to a file or to append a new line of text to a file.

1.2.1 Basic usage#

echo [option]

The following is a simple example for the echo command.

echo Hello World!
Hello World!

There are three common options:

  • -n: Does not print the trailing newline.
  • -E: Is the default option that disables the implementation of escape sequences.
  • -e: Is used to enable interpretation of backslash escape characters.

Note

Your shell may have its own version of echo, which will supersede the version described here. Please refer to your shell's documentation for details about the options it supports.

1.2.2 Escape sequences#

Some escape sequences perform different operations such as:

  • \\: Backslash
  • \n: New Line
  • \r: Carriage Return
  • \t: Horizontal Tab

Note

Depending on your Operating System (OS) (Windows, Linux or MacOS) the new line on a file is different:

  • Windows: \r\n
  • Linux: \n
  • MacOS: \n\r

1.2.2.1 Example 1: Backslash#

echo -e "Hackathon\\For\\SreXperts"
Hackathon\For\SreXperts

1.2.2.2 Example 2: New Line#

echo -e "Hackathon\nFor\nSreXperts"
Hackathon
For
SreXperts

1.2.2.3 Example 3: Carriage Return#

echo -e "Hackathon\rFor SreXperts"
For SreXperts

1.2.2.4 Example 4: Horizontal Tab#

echo -e "Hackathon\tFor\tSreXperts"
Hackathon       For     SreXperts

1.3 Redirect output to a file#

One of the lesser known functionalities of the bash terminal is the ability to redirect the output from the terminal into a file. You can use command > file to create a file with the content of the standard output (where command is the shell command that generates the output and file is the name of the destination file).

The > symbol is used to redirect the output of a command to a file instead of the terminal screen.

1.3.1 Example: Write echo output to a file#

echo "Hackathon For SreXperts" > test.txt

To print the contents of the file we just created, use the cat command.

cat test.txt
Hackathon For SreXperts

1.4 Appending output to a file#

In addition to the "redirect to a file" function, Linux shell's allow the user to append (or add) data to an existing file.

The >> symbol is used to append the output of a command to a file instead of the terminal screen.

1.4.1 Example: Append the output of the echo command to an existing file#

echo "SreXperts is the best" >> test.txt

To print the contents of the file we just appended to, use the cat command.

cat test.txt
Hackathon For SreXperts
SreXperts is the best

1.5 grep#

The grep command is among the system administrator’s “Swiss Army knife” set of tools. It is extremely useful to searching for strings and patterns in a file, a group of files or hierarchical directory structure.

This section introduces the basics of grep, provides examples of some more advanced uses and provides links for further reading.

grep (an acronym for “Global Regular Expression Print”) is installed by default on almost every distribution of Linux.

1.5.1 Basic usage#

grep finds a string in a given file or input, quickly and efficiently. While most everyday uses of the command are simple, there are a variety of more advanced uses that most people don’t know about, including regular expressions and more, which can become quite complicated.

grep [option] [regexp] [filename]

Now let’s see some practical examples of the grep command.

To better understand the results, a simple text file has been created. The file contains the following lines:

Hi 
this 
is test file 
to carry out few regular expressions 
practical with grep 
123 456 
Abcd
ABCD

The -i option allows searches to be case insensitive.

grep -i 'abcd' testfile 
Abcd 
ABCD

The -w option requires that the whole word is matched.

grep -w 'test' testfile 
is test file

The -v option inverts the search, for example, matching on practical would return lines containing practical but the inverted search would return lines not containing practical.

grep -v 'practical' testfile 
Hi 
this 
is test file 
to carry out few regular expressions 
123 456 
Abcd 
ABCD

The -A option, when provided with a number, allows the searches to provide some context by additionally printing the requested number of lines after each match is found.

grep -A 1 '123'  testfile
123 456 
Abcd

The -B option, when provided with a number, allows the searches to provide some context by additionally printing the requested number of lines before each match is found.

grep -B 2 'Abcd' testfile
practical with grep 
123 456 
Abcd

1.5.7 Using grep on command outputs with the pipe character#

grep can also be used to find a string in command output using a pipe (|) to feed the standard out (STDOUT) of the command to the standard in (STDIN) of grep.

command | grep [option] [regexp]

The following example shows how the cat and grep commands can be chained together using the pipe (|) symbol.

cat testfile | grep -i 'abcd' 
Abcd 
ABCD

1.6 tail#

The UNIX/Linux tail command displays the latest content from the end of a chosen file(s) directly to the screen. This function is useful for instantly viewing recent additions to files, as new information is often appended at the end.

To better understand the results, a simple text file has been created. The file contains the following lines:

Hi 
this 
is test file 
to carry out a few
operations with tail.
The Tail command  
prints the last 
10 lines by default 
so let's add
a few more
123 456 
Abcd
ABCD

1.6.1 Basic usage#

By default, tail displays the 10 last lines of a file. Here is the basic syntax:

tail [file_name]

The following is an example using the testfile.

tail testfile 
to carry out a few
operations with tail.
The Tail command
prints the last
10 lines by default
so let's add
a few more
123 456
Abcd
ABCD

You can use several options for customizing tail output. Here are some of the most popular ones with their long form and functions:

  • -c num or --bytes=num: outputs the last num bytes of data.
  • -n num or --lines=num: outputs the last num lines of data.
  • -f or --follow: continually outputs new data as it is written to the end of the file.

To print a specific number of files, the -n option, provided with a number, is used.

tail -n 3 testfile 
123 456
Abcd
ABCD

1.6.3 Monitor a file for changes#

The tail command can be used to monitor a file for changes. The --follow option is used to do this and is very popular for monitoring log files.

As an example two terminals windows will be opened. In the first terminal, the testfile will be monitored using the tail -f command. In the second terminal the echo command will be used (coupled with the >> append option) to write new data to the same testfile.

tail -f testfile
$ tail -f testfile
to carry out a few
operations with tail.
The Tail command
prints the last
10 lines by default
so let's add
a few more
123 456
Abcd
ABCD
echo 'new string' >> testfile
$ tail -f testfile
to carry out a few
operations with tail.
The Tail command
prints the last
10 lines by default
so let's add
a few more
123 456
Abcd
ABCD
new string

1.7 vi/vim#

The vi (or vim) editor is the most used command line text editor on UNIX/Linux systems. It is extremely powerful with many built in features. It is not easy to use for beginners though.

The following are some high level hints.

1.7.1 Modes of Operation in the vi editor#

There are three modes of operation in vi:

1.7.1.1 Command Mode#

This mode is where vi interprets any characters we type as commands and does not display them in the window. This mode allows us to move through a file, and delete, copy, or paste a piece of text. To enter into Command Mode from any other mode, requires pressing the Esc key. When vi starts up, this is the mode it is in.

1.7.1.2 Insert mode#

To enter text, you must be in insert mode. To come in insert mode, you simply type i. To get out of insert mode, press the Esc key, which will put you back into command mode.

1.7.1.3 Escape Mode#

Line Mode is invoked by typing a colon :, while vi is in Command Mode. The cursor will jump to the last line of the screen and vi will wait for a command. This mode enables you to perform tasks such as saving files and executing commands.

1.7.2 Commands#

1.7.2.1 Inserting text in Vi Editor#

To edit the file, we need to be in the insert mode. There are many ways to enter insert mode from the command mode.

  • i: Inserts text before current cursor location
  • a: Insert text after current cursor location
  • A: Insert text at the end of current line
  • o: Creates a new line for text entry below cursor location and switches to insert mode.
  • O: Creates a new line for text entry above cursor location and switches to insert mode.

1.7.2.2 Save and Exit#

Need to press Esc key then type : before typing the following commands:

  • :q: Quit.
  • :q!: Quit without saving changes i.e. discard changes.
  • :wq: Write and quit (save and exit).

1.7.3 Searching and replacing#

vi also has powerful search and replacement capabilities. to so that we need to be in the Command Mode then enter the Escape Mode

1.7.3.1 Searching#

The syntax for searching is:

:s/string

Here the string represents the text we want to search for

1.7.3.2 Replacing#

The syntax for replacing one string with another string in the current line is:

:s/pattern/replace/

Here pattern represents the old string and replace represents the new string. For example, to replace each occurrence

The syntax for replacing every occurrence of a string in the entire text is similar. The only difference is the addition of a % in front of the s:

:%s/pattern/replace/

1.7.4 Examples#

1.7.4.1 Write text into a file#

First we need to open a file, we can create a new file directly using vi.

$ vi SreXperts

Then we need to press the i key to enter the Insert mode. we can see that in the last line of the screen the information --INSERT-- is to inform us that we are in the Insert mode

Now we can write some text

SreXperts is the best

To save the file we need to go back to the Command Mode, press Esc key and then to the Escape mode type a colon :. Now, in the Escape mode, type wq and press the Enter key.

1.7.5 Search and replace#

First we need to open a file using vi.

$ vi SreXperts

Search for SreXperts in the Escape mode using the command bellow:

:s/SreXperts

Let's replace the string SreXperts in the current line

:s/SreXperts/Hackathon/

in case we had multiple times the string we want to replace, we can replace all of them at once

:%s/Hackathon/SreXperts/

To save the file we need to go back to the Command Mode, press Esc key and then to the Escape mode type a colon :. Now, in the Escape mode, type wq and press the Enter key.