5 mins readlinux

An intro to text manipulation in Linux

use "history" command to view a list of previously executed commands

blog image

In Linux, almost everything is a text file, manipulating text is crucial. Let's take look at a few commands that will help us do so :

  1. cat
  2. head
  3. tail
  4. nl
  5. grep
  6. sed
  7. more
  8. less

The cat command

The most basic command to display text.

  1. cat file_name
blog image

The head command

This command will display the first 10 lines (by default) of a file.

  1. head file_name

If you wanted to display a specific number of lines, specify it after the command with a -

  1. head -6 passwords.txt
blog image

The tail command

The opposite of head, will display the last 10 lines (by default) of a file.

  1. tail file_name

If you wanted to display a specify number of lines, specify it after the command with a -

  1. tail -20 passwords.txt

The nl command

Display a file with line numbers.

  1. nl file_name

If you pipe output to it, it makes referencing a whole lot easier.

  1. head -6 passwords.txt | nl
blog image

The grep command

Probably the most widely used text manipulation command. There are entire books on this single command.

  1. cat file_name | grep keyword

It lets you filter content of a file for display.

Combine it with previous commands, let's view all words from lines 5 to 25 of passwords.txt that have 14 in them.

  1. head -25 passwords.txt | tail -20 | grep 14 | nl
  • head -50 passwords.txt indicates we want to display the first 50 lines
  • tail -30 indicts we want the last 30 lines of those 50 lines from the head command, so lines 20 - 50.
  • grep 23 will display only lines that have 23 in them.
  • nl will display line numbers.
blog image

The sed command

This command lets you search for occurrences of a word or a test pattern and then perform some action on it. Similar to Find and Replace in Windows. Commands in sed begin with a single letter.

Search for the word Sunflower (remember, Linux is case sensitive) in the passwords.txt file using grep, like so:

  1. cat passwords.txt | grep Sunflower

Find and Replace with sed

Let’s say you want sed to replace every occurrence of Sunflower with Luck and then save the new file to passwords_v2.txt. You could do this by entering the command:

  1. sed s/Sunflower/Luck/g ~/Desktop/passwords.txt > passwords_v2.txt
  • The s command performs the search: you first give the term you are searching for (Sunflower) and then the term you want to replace it with (Luck), separated by a slash (/).
  • The g command tells Linux that you want the replacement performed globally (replace every occurrence, in this case there is just one).
  • Then the result is saved to a new file named passwords_v2.txt.

more and less command

Both the more and less command let's you view a page of file and let you page down through it using enter key.

Only with less you get a little bit more functionality. apparently less is more :)
With less command using "/" key you can search for terms in the file.

The AWK command

The awk command in Linux is a powerful programming language and command-line utility used for text processing and pattern matching. It is particularly useful for extracting and manipulating data in structured text files, such as log files, CSV files, and configuration files.

  • Here's a basic syntax of the awk command:
  1. awk 'pattern { action }' input_file
awk is a very useful command for shell scripting and has a variety of use cases . If you want to learn more about it, please read in here.


In the Linux ecosystem, where text files form the backbone of configuration, log files, and system communication, adept text manipulation skills are paramount. This blog has explored a glimpse into the powerful world of text manipulation commands, highlighting their significance in the Linux command line.

Published by :
Reshad Sadik