EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 600+ Courses All in One Bundle
  • Login

Linux Filter Commands

By Priya PedamkarPriya Pedamkar

Home » Software Development » Software Development Tutorials » Linux Tutorial » Linux Filter Commands

Linux Filter Commands

Introduction to Linux Filter Commands

As per the user requirement the linux filter commands will read the standard inputs, performs the necessary actions or operations on top of it and writes the end result to the standard output format. A filter is a small and specialized program in Linux operating system to get the meaningful input from the user / client and with the help of few other filters and pipes to perform a series of operation to get the highly unique or specified end result. It will help to process the information in a very dominant way such as shell jobs, modify the live data, reporting, etc.

Syntax of filter command:

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

[filter method] [filter option] [data | path | location of data]

Filter Methods in Linux Operating System

Given below are different filter methods:

  • Fmt
  • more
  • Less
  • Head
  • Tail
  • Sed
  • Find
  • Grep, Egrep, Fgrep, Rgrep
  • Pr
  • Tr
  • Sort
  • Uniq
  • AWK

1. Fmt

The fmt is simple and optimal text formatter. It is helpful to reformat the input data and print in end result in the standard output format.

Code:

cat site.txt
fmt -w 1 site.txt

Explanation :

  • In site.txt file, we are having some names. But it is in one single row and separated by space.
  • The fmt command is useful to display in single line multiple words into individual records separated by space.

Output:

linux filter commands 1

2. More

The more command is useful for file analysis. It will read the big size file. It will display the large file data in page format. The page down and page up key will not work. To display the new record, we need to press “enter” key.

Code:

cat /var/log/messages | more

Explanation:

  • We are reading the large file “/var/log/messages” of Linux via more command.

Output:

more

3. Less

The less command is like more command but it is faster with large files. It will display the large file data in page format. The page down and page up key will work. To display the new record, we need to press “enter” key.

Code:

cat /var/log/messages | less

Explanation:

  • We are reading the large file “/var/log/messages” of Linux via less command.

Output:

linux filter commands 3

4. Head

As the name suggested, we are able to filter / read the initial or top lines or row of data. By default, it will read the first 10 lines or records of the give data. If we need to read the more lines, then we need to specify the number of lines that we need to read with the help of “-n” keyword.

Popular Course in this category
Kali Linux Training (3 Courses, 3+ Projects)3 Online Courses | 3 Hands-on Projects | 21+ Hours | Verifiable Certificate of Completion | Lifetime Access
4.5 (5,494 ratings)
Course Price

View Course

Related Courses
Linux Training Program (16 Courses, 3+ Projects)Red Hat Linux Training Program (4 Courses, 1+ Projects)
Note: The record calculation will start from the top of the file or data.

Code:

head -n 7 file.txt

Explanation :

  • We have the sample file data having 10 records in it.
  • Now we are using the default “head” command to read the data file.
  • If we need to read or get the data except for default value then we will use the “-n” keyword to read the number of lines of records.

cat file.txt

Output:

linux filter commands 5

head file.txt

Output:

linux filter commands 6

head -n 7 file.txt

Output:

linux filter commands 7

5. Tail

If we need to get the data from the bottom of the file then we will use the tail command. By default, it will read the last 10 lines or records of the give data. If we need to read the more lines, then we need to specify the number of lines that we need to read with the help of “-n” keyword.

Code:

tail -n 3 file.txt

Explanation:

  • We are using the default “tail” command to read the data file.
  • If we need to read or get the data except for default value then we will use the “-n” keyword to read the number of lines of records.

tail file.txt

Output:

linux filter commands 8

tail -n 4 file.txt

Output:

linux filter commands 9

6. Sed

For filtering and transforming text data, sed is a very powerful stream editor utility. It is most useful in shell or development jobs to filter out the complex data.

Code:

sed -n '5,10p' file.txt

Explanation:

  • In head and tail command, we are able to filter the number of records from top or bottom.
  • But if there is need to display the record form starting point to ending point.
  • Then we need to use the sed command. We have 10 records in file.txt. But we need to record from line/record from 5 to 7 only.

cat file.txt

Output :

linux filter commands 10

7. Find

The find filter command is useful to find the files from the Linux operating system.

Code:

find / -name "file.txt"

Explanation:

  • With the help of find command, we will filter out necessary files or directory in the Linux operating system.
  • We need to add two main parameters in the find command i.e.
  • Search path: “/” (I have provided the root directory path.)
  • filename: “file.txt” (filename or directory name to filter or search)

Output:

find

8. Grep, Egrep, Fgrep, Rgrep

The grep, egrep, fgrep, rgrep are similar commands. It will be useful to filter or extract the matching pattern string form the input data or file.

Code:

grep -i "Davide" file.txt

Explanation:

  • In grep command, we need to specify the matching string in the command.
  • The utility will filter out the same matching string (Davide) form the input data or file (file.txt) and display the matching record in “red” colour format.
Note: We can use the “-i” keyword in the grep command. It will help to ignore the upper case or lower case letter while filtering the data.

Output:

linux filter commands 12

9. Pr

The pr command is useful to convert the input data into the printable format with proper column structure.

Code:

yum list installed | pr --columns 2 -l 40

Explanation:

  • In the above pr command, we are printing the output data in two columns (using ‘–columns’ keyword) and 40 lines in individual page (using ‘-l’ keyword).

Output:

pr

10. Tr

The tr command will translate or delete characters from the input string or data. We can transform the input data into upper or lower case.

Code:

echo "www.educba.com" | tr [:lower:] [:upper:]

Explanation:

  • In the above tr command, we need to define the input text or data in set 1 “[:lower:]” and output data in set 2 “[:upper:]”.

Output:

Tr

11. Sort

As the name suggested, we can sort or filter the records in ascending order.

Code:

sort char.txt

Explanation :

  • We are having the text file (char.txt).
  • It contains the number of character in a scattered way.
  • Now we are using the sort command on the same file.
  • After using the sorting filter on the char.txt file.
  • The character will sort and display in acceding pattern.

Output:

cat char.txt

linux filter commands 15JPG

sort char.txt

linux filter commands 16JPG

12. Uniq

The uniq command is useful to omit repeated records or lines from the standard input.

If you want to display the number of occurrences of a line or record in the input file or data. We can use the “-c” keyword in the uniq command.

Code:

uniq -c char.txt

Explanation:

  •  In char.txt file, we have the number of duplicate characters in it.
  • After using the uniq command, it will remove the duplicate character and count the number of duplicate character.

Output:

cat char.txt

cat char.txt

uniq –c char.txt

linux filter commands 18 JPG

13. AWK

In awk, we are having the functionality to read the file. But here, we are using integer variables to read the file.

Below are the integer information and meaning if they are using in awk commands.

  • $0: read the complete file or input text.
  • $1: read the first field.
  • $2: read the second field.
  • $n: read the nth field.
Note: If we are not using any separator keyword “-F” in the awk command. Then the awk command considers the space in the text file or input data and separates the result as per the $variable provided in the same awk command.

Code:

cat file.txt | awk '{print $1}'

Explanation:

  • In file.txt, we are having the sample data.
  • If we will use $0 then entire data will read.
  • If we will use $1 then only first column data will filter out.

cat file.txt

Output: Sample file view.

linux filter commands 19 JPG

cat file.txt | awk ‘{print $0}’

Output: Output with “$0”.

linux filter commands 20 JPG

cat file.txt | awk ‘{print $1}’

Output: Output with “$1”

Output with “$1”

Conclusion

We have seen the uncut concept of “Linux Filter Commands” with the proper example, explanation and command with different outputs. Basically filter utility comes with Linux operating system but some third party filter utilities we can install in the operating system. The filter data is very important while developing any code or shell job on Linux or any other platform. With the help of filter command or utility, we can able to extract the valuable data from the input data.

Recommended Articles

This is a guide to Linux Filter Commands. Here we discuss the introduction to Linux Filter Commands with different types of filter methods. You may also have a look at the following articles to learn more –

  1. Linux Apps
  2. Head Command in Linux
  3. Linux Rename Command
  4. Linux tee Command

All in One Software Development Bundle (600+ Courses, 50+ projects)

600+ Online Courses

50+ projects

3000+ Hours

Verifiable Certificates

Lifetime Access

Learn More

0 Shares
Share
Tweet
Share
Primary Sidebar
Linux Tutorial
  • Linux Commands
    • CAT Command in Linux
    • PS command in Linux
    • Mkdir Command in Linux
    • rmdir command in Linux
    • mv command in Linux
    • Linux rm Command
    • CHMOD Command in Linux
    • Zip Command in Linux
    • Linux Route Command
    • SCP Command in Linux
    • du Command in Linux
    • Less Command in Linux
    • Copy Command in Linux
    • Head Command in Linux
    • Help Command in Linux
    • Linux History Command
    • hostname Command in Linux
    • GREP Command in Linux
    • Linux Curl Command
    • Linux List Users
    • Linux List Directories
    • Linux Directory Permissions
    • Find Directory Linux
    • Linux User Permission
    • Linux Add User
    • Linux Delete User
    • Linux uniq
    • Linux Filter Commands
    • Linux sort Command
    • Linux Sort by Size
    • Remove dir Linux
    • Linux tr Command
    • Linux Touch Command
    • Linux More Command
    • Linux Cal Command
    • Linux OD
    • Linux Zcat
    • Linux tracepath
    • Linux Sleep
    • Linux bg Command
    • Linux WC
    • Linux Read
    • Linux Write
    • Linux Rename Command
    • Linux Free Command
    • Linux tac
    • Linux Shred
    • Linux wget
    • Linux Date
    • Linux Untar
    • Linux JQ
    • Linux Xargs
    • Linux Mount Command
    • Linux mdadm
    • Linux comm
    • Linux tee Command
    • Tail Command in Linux
    • Linux gzip
    • Linux Directory Structure
    • Linux Alias Command
    • Linux Inode
    • Linux Ping
    • Linux Watch Command
    • Linux Split Command
    • Linux Size
    • Linux Network Stack
    • Linux Diff Command
    • Vim Command in Linux
    • Linux nslookup
  • Basic
    • Introduction to Linux
    • What is Linux
    • Careers in Linux
    • How to Install Linux
    • How to Install Ubuntu
    • How to Install Ubuntu Server
    • How To Install Debian
    • Advantage Of Linux
    • What is Raid in Linux
    • What is Raid 5?
    • Raid 5 vs Raid 6
    • Careers in Linux Administration
    • What is Shell in Linux?
    • Bash Shell in Linux
    • Linux Emulator
    • Linux Operators
    • Linux Apps
    • Linux Keyboard Shortcuts
    • Linux Crontab
    • What is Raid Storage
    • What is Vmware
    • What is RedHat
    • What is Debian
    • Uses of Ubuntu
    • Career In RedHat
    • Linux Distributions
    • Linux From Scratch
    • Linux Format
    • Cheat Sheet Linux
  • Advanced
    • Install VMware
    • Install GRUB
    • Chown Command in Linux
    • Linux Network Command
    • Configuring Linux Network Environment
    • DNS Configuration in Linux
    • Linux Users
    • Create User in Linux
    • DHCP Server in Linux
    • FTP Server in Linux
    • Linux Regular Expression
    • IP Command in Linux
    • Set IP Address in Linux
    • Tar Command in Linux
    • Dynamic Host Configuration Protocol
    • Linux Kill Signals
    • Linux File Permissions
    • Linux File System
    • Linux Create File
    • Linux Concatenate Files
    • Linux Join
    • Linux Process Management
    • Linux Proxy Server
    • Linux System Commands
    • Linux System Logging
    • NFS in Linux
    • Linux LVM
    • Linux Repository
    • Samba Server in Linux
    • Top Commands in Linux
    • Types of Shells in Linux
    • Shell Script Types For Linux
    • GCC Command in Linux
    • Monolithic Kernel
    • YARN Package Manager
    • Linux Sed Replace
    • Linux List Groups
    • Linux Container
    • Linux Find File by Name
    • Create Folder in Linux
    • Linux Move Folder
    • Linux Error Codes
    • Linux Automount
    • Linux HugePages
  • Kali Linux
    • Install Kali Linux
    • Kali Linux Uses
    • Kali Linux Commands
    • Kali Linux Repository
    • Kali Linux Packages
    • Kali Linux root password
    • Kali Linux Alternative
  • Interview Questions
    • Linux Interview Questions
    • Linux System Administration Interview
    • Red Hat Interview Questions

Related Courses

Kali Linux Training

Linux Training Course

Red Hat Training Course

Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Java Tutorials
  • Python Tutorials
  • All Tutorials
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • ASP.NET Course
  • VB.NET Course
  • PHP Course

© 2020 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA Login

Forgot Password?

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you
Book Your One Instructor : One Learner Free Class

Let’s Get Started

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

Special Offer - Kali Linux Training Learn More