Linux-Ubuntu/C2/Simple-Filters-in-Linux/English

From Script | Spoken-Tutorial
Jump to: navigation, search

TITLE: Simple Filters in Linux

Author: EduPyramids

Keywords: head, tail, sort, cut, paste, Linux, command line, text manipulation, edupyramids, video tutorial.


Visual Cue Narration
Slide 1

Title Slide

Welcome to this spoken tutorial on Simple Filters in Linux.
Slide 2 Learning Objectives In this tutorial, we will learn to,
  • Display specific lines with head and tail commands.
  • Sort files in ascending and descending order
  • Extract and manipulate columns using cut and paste commands.
Slide 3

System Requirements

To record this tutorial, I am using,
  • Ubuntu OS version 24 point 04
Slide 4Pre-requisites

https://EduPyramids.org


To follow this tutorial,

Learners should have Ubuntu version 24 point 04

For the prerequisite of Linux tutorials please visit this website.

Slide 5

Code files

The following code files are required to practice this tutorial.

  1. marks.txt
  2. numbers.txt
  3. sf-commands.txt

These files are provided in the Code Files link of this tutorial page.

The following code files are required to practice this tutorial.

These files are provided in the Code Files link of this tutorial page.

Let us begin with terminal prompt symbols and their meanings.
Slide 6

Terminal Prompt Symbols

  • $ prompt indicates a normal user.
  • Commands can be run without administrative privileges at the $ prompt.
  • # prompt indicates that root privileges are required.
  • Switching to root can be done using sudo su or su root.
  • The dollar prompt indicates a normal user.
  • Commands can be run without administrative privileges at the dollar prompt.
  • The hash prompt indicates that root privileges are required.`
  • Switching to root can be done using sudo su or su root.
Slide 7

head command

The head command in Linux is used to display the beginning of a file.

By default, it shows the first ten lines of a file on the terminal.

It helps us to quickly preview file contents without opening the entire file.

To use the head command, type head followed by the name of an ASCII file.

Point to the numbers.txt file in the home folder. Let’s use numbers.txt file given in the code files folder to explain head command.

Please download it for practising.

Show the numbers.txt file opened. I have opened the numbers.txt file.
Press Ctrl + Alt+ T Keys. Next, let us open the Terminal.
Type ls and press Enter.

Point to numbers.txt file

Type ls at the prompt and press Enter.

We see that the file numbers.txt is listed in the home folder.

Type cat num and press the Tab key.

point to the output.

Type cat space num and press the Tab key.

This will auto-complete the file name to numbers.txt.

Press Enter to display the full contents of the file in the terminal.

Let us check with the head command.
Type at the command prompt

head num

press the Tab key and press Enter.

Type head space num and press the Tab key.


Press Enter.

Show the output The head command displays the first ten lines of the file.
Type at the command prompt

Type: head -n5 numbers.txt

highlight -n5

press Enter.

Show the output

Note: head -n5 or head -5 work the same way.

Let’s use the option hyphen n to display a specific number of lines.

Type head space hyphen n 5 space numbers dot t x t

Here n is a number.

Let us use n as 5 and run the command.


This displays just the first 5 lines of the file instead of all its contents.

Now let us learn about the tail command.
Slide 8

tail command


  • The tail command works exactly opposite to the head command.
  • By default, it shows the last 10 lines of a file on the terminal.
  • It is commonly used to monitor log files or view the most recent updates.
Now let us learn about the tail command.

The tail command works exactly opposite to the head command.

  • By default, it shows the last 10 lines of a file on the terminal.
  • It is commonly used to monitor log files or view the most recent updates.
Type at the command prompt

Type: tail numbers.txt

Let us try the tail command on numbers dot t x t file.

Type tail space numbers dot t x t and press Enter.

The last 10 lines of the file are displayed on the terminal.

Type at the command prompt

tail -n5 numbers.txt

Now at the terminal type: tail hyphen n five numbers dot t x t

hyphen n shows a specific number of lines from the end of a file.

and press Enter.

Only the last 5 lines of numbers.txt file are displayed on the screen.

Slide 9

Log Files


log-file.png


Now let’s learn about a log file.

A log file records events that occur on a system.


It includes system messages, application activities and security-related actions.


Log files help monitor system behaviour and fix problems.


The auth.log file maintains logs of authentication events on the system.

It records user logins, logouts and failed login attempts.

This file helps track user access and detect unauthorized attempts.

Slide 10

Options in tail command


tail with hyphen f option follows a file in real time.

tail hyphen f updates the output automatically as new lines are added to the file.

It is useful for monitoring log files like auth dot log.

It helps to track system and authentication events live.

When new data arrives, it prints it immediately.

Type at the command prompt

tail -f /var/log/auth.log and press Enter.

Type this command and press Enter.

The command does not exit, it waits for new lines to be added to the file.

When new data arrives, it prints it immediately.

click the + icon to open another terminal. Now, let us open another Terminal window.
Show 2 terminal windows, side by side. This helps us see how the tail command follows the last line of a log file in real time.
Type su spoken and press Enter. Now, let us switch the user.

Type su followed by your username at the prompt .

I will type su space spoken and press Enter.

Show the prompt.

Enter a wrong password and press Enter.

You will be prompted for a password.

Enter a wrong password and press Enter.

The system denies login and shows an authentication failure message.

This means the password is incorrect.

show the log file entry on another window

Point to the date and time.

You will now see that the terminal running the tail command is automatically updated with a new log entry.

The log shows the date and time when the authentication failure occurred.

Type date and press Enter. Now, let us verify the system date and time.

Type date and press Enter.

The terminal displays the current system date and time.

This corresponds to the timestamp shown in the authentication log.

Type: exit and press Enter, to close the terminal Type exit to close the new terminal.
Press Ctrl and C keys

close the new terminal.

Press Ctrl and C keys to stop the running tail command.
Point to the auth.log command on the terminal. In the previous example, we looked only at the auth.log file.
Slide 10

Log Files


log files1.png

/var/log/messages : General system messages and information

/var/log/auth.log : Authentication logs

/var/log/kern.log : Kernel logs

/var/log/cron.log : Cron daemon logs (scheduled jobs)

/var/log/maillog : Mail server logs

/var/log/httpd/ : Apache access and error logs directory

/var/log/mysqld.log : MySQL database server log file

  • Here are some commonly used log files in Linux.
  • A Linux system administrator examines log files to gather information.
  • This helps to troubleshoot the system when problems arise.
Slide 11

sort command


Now we will learn about sort command.
  • The sort command arranges lines in a text file.
  • It sorts data alphabetically or numerically in ascending and descending order.
  • Input can be from a file or standard input.
  • The sorted output is shown on the terminal.
Type sort numbers.txt and press Enter. Type: sort space numbers dot t x t and press Enter.

This sorts the file in ascending order by default.

Point to the output. Notice something interesting here:

The sort command, by default, sorts lines lexicographically, looking at the first character.

So numbers like 10, 11, and 12 appear before 2.

Type at the command prompt

sort -n numbers.txt

Show the output

To avoid this, add the hyphen n option and press Enter.

Now, the sort command looks at the entire number while sorting.

Type at the command prompt

sort -rn numbers.txt

To sort numbers in reverse order, add the hyphen r n option to the sort command.

We see the output in the reverse order.

point to the numbers in the output.


Type at the command prompt

sort -run numbers.txt

highlight u in the command.

This file contains repeated numbers.


To extract only the unique numbers, let us add the hyphen u option.

Show the output.


Show the earlier output for comparison.

Scroll up to show the output.

Point to the output

Now we only see the unique numbers displayed as the output.

Earlier, two 2s were displayed.

Now, only one 2 is displayed on the terminal.

Now, let us see how to sort a file based on a specific column.
Show the marks.txt file. Let us open the marks.txt file given the code files link.
Let us sort the marks.txt file based on the second column.
Type at the command prompt

sort marks.txt -t " " -k2

Type this command, press Enter.


Here, hyphen t specifies the delimiter and the space inside the quotes represents a space character.

The hyphen k 2 option indicates that the sort should be performed on the second column.


Type

cat marks.txt and press Enter


Type cat space marks dot t x t and press Enter.
Point to Avir and Bala This is the original file.

Notice that Avir has moved up.

Bala has moved down after sorting by the second column.

Slide 12

cut command


The cut command in Linux is used to extract specific sections from each line of a file.

It can select data based on characters, fields, or byte positions.

The cut command reads input from a file or standard input.

The selected output is displayed on the terminal.

Now about the cut command.


The cut command is used to extract specific information from each line of a file.

It can select data based on characters, fields, or byte positions.

The cut command reads input from a file or standard input.

The selected output is displayed on the terminal.

Let us extract the names from marks.txt.
Type at the command prompt

cut marks.txt -d " " -f2

cut space marks dot txt d double-quotes space double-quotes minus f2

Type the following command and press Enter.

Here hyphen d specifies the delimiter.

The space inside quotes represents the delimiter.

hyphen f 2 selects the second column.

The output displays only the second column.

Slide 13

paste command


The paste command in Linux is used to merge lines from two or more files.

It joins corresponding lines side by side, using a delimiter.

By default, the delimiter is a tab character.

The output is displayed on the terminal.


  • The paste command in Linux is used to merge lines from two or more files.
  • It joins corresponding lines side by side, using a delimiter.
  • By default, the delimiter is a tab character.
  • The output is displayed on the terminal.
Point to the files in the home folder. Let us use both the numbers.txt and marks.txt file now.
Type at the command prompt

paste numbers.txt marks.txt

paste space numbers dot txt space marks dot txt

Type this command and press Enter.
Show the output marks dot t x t has been appended to numbers dot t x t file.
Type at the command prompt

paste numbers.txt marks.txt > concatfile.txt


type cat concatfile.txt

We can use the redirect operator to send this output to a concatfile.txt.


Press the Up Arrow key.

Type the redirect operator( >) followed by concatfile.txt Press Enter.


Now the output is redirected to a file named concatfile.txt.


Type, cat space concatfile.txt and Press Enter.


We can see the paste command results are saved in this file.

Type at the command prompt

paste -s numbers.txt and press Enter.


To print numbers serially with tab delimiters, use the hyphen s option with paste.


Type

paste space hyphen s space numbers dot t x t and press Enter.


point to the output. We can see the output on the terminal with a tab space in between the numbers.
Slide 14


Summary

In this tutorial, we have learnt to:* Display specific lines with head and tail commands

  • Sort files in ascending and descending order
  • Extract and manipulate columns using cut and paste commands


With this we come to the end of this tutorial.

Let us summarise.

Slide 15


As an assignment# Use head and tail commands to display specific lines of a file.

  1. Sort numbers.txt in reverse numeric order and extract unique values.
  2. Use cut and paste to manipulate columns in marks.txt.


As an assignment, please do the following.
Slide 16

Thank you

This Spoken Tutorial is brought to you by EduPyramids Educational Services Private Limited, SINE IIT Bombay.

Thank you.

Contributors and Content Editors

Ketkinaina, Madhurig