When you work with linux a graphical user interface is not always available. In most cases, you’ll just have the access to a terminal of a remote machine, on which you have just logged in using ssh. In this article we list the basic linux commands for file/text manipulation that you’ll need. These commands are extremely useful for playing around with files in a system. It doesn’t have to be remote system at all, for experimentation purposes, you can just open up a terminal locally. Just try each command with any text file on a unix based operating system.

In this article I have aggregated the most powerful text manipulation commands in linux. After you master these commands, a GUI will not be desirable even if it’s available. Make sure to check out the manual pages for the command you’re exploring in order to get a deeper understanding of its options. As a result of which you’ll under the programs internal workings more clearly. Without any further delay, let’s start with the list of these beautiful commands and their uses on a linux system.

cat – One of the most basic linux commands

No, this command does not summon a Cat. It stands for ‘concatenate’ and simply outputs the contents of a file (or multiple files) to the terminal. cat command reads the file sequentially and writes it to the standard output, which is usually the terminal if you’re running it from inside a terminal. 

Useful options for cat:
-b                    output a corresponding line number starting from 1 and also, don’t display blank lines.
-n                    simply number the output lines, do not omit the blank lines.
-s                    squeeze the adjacent empty lines to just a single entry.

Examples for the cat command:
# Display the contents of ‘program.c’ file, and show line numbers 
cat -n program.c

# Display the contents of the files ‘program.c’ and ‘program.py’
cat program.c program.py

The less command

less is another very simple command which just buffers the output on the screen. It is usually used to display a very large file where using the cat command might take a long time to output on the screen. In other words, less can handle files ranging from 50MBs to 500GBs. The less command displays the first N number of lines that can fit into your screen and then using the enter or return key you can scroll through the whole file till you finally reach the end of file. Probably will take a while in case of a 500GB file.

Examples of less command:
# Start displaying the file from the beginning, hit enter to move forward. 

less filename

# Press q key to exit # Check out the `man less` page to get in depth information on its options.

The diff Command

This is one of the simplest commands in this list of basic linux commands. diff command simply compares two text sources and outputs their differences. It compares the files line by line to find the differences.

Options for `diff`:
-i                ignore case differences in lines.
-w              ignore all the white spaces.
-y               show differences side by side in two columns.

Examples for the diff command:

Let’s take a real life example to show its usefulness. Suppose you have an API, which responds in JSON format, based on the input parameters. You call it the first time with set A of parameters and call it the second time with a set B of parameters. Now you want to check for differences. All you need to do is write both the outputs in two different files, file A.txt and file B.txt. Now to check the differences you just have to do the following.

# Display the differences between files A.txt and B.txt
diff A.txt B.txt

# Ignore all character case differences, and show differences side by side in two columns.
diff -i -y A.txt B.txt

The sort Command

Sort utility sorts the lines of a text or binary files. This can be used to sort numbers, strings or binary data. This linux program can handle any amount of data you can possibly throw on it (but realistically). If the file contains just strings, then it will be sorted lexicographically. sort can also be used for uniquely identifying keys in a large dataset containing duplicate keys.

Examples of the sort command:

Suppose, you have a file of containing N rows of employeeIds and each employeeId is present multiple times. You want to identify all the unique employee ids in a sorted manner. Usually sort command is used for this kind of task, it can sometimes be coupled with other programs too.

# Sort and output only unique values (eliminate duplicate) 
sort -u Employee_Ids.txt

# Sort the unique records in a reverse manner.
sort -u -r Employee_Ids.txt

# Sort all the unique names containing ‘H' in the given file of employee names.
cat Employee_Names.txt | grep -i 'h' | sort -u

# In the above command, we’re just piping the output of the cat command to grep
# which filters the names containing ‘h’ and then eventually passing the output
# from grep to the sort utility.

The head and tail commands

The head command is used to display the head of the file, i.e. displaying the first N lines of the file. Similarly, the tail command displays the last N lines of a file. tail is extremely useful linux command for monitoring real time logs. It allows you to add a “follow” option by which it displays all the new content being added to the file in realtime.

Examples for head and tail commands:
# Display first 10 lines of the file. 
head -n 10 Employee_Names.txt

# Display last 10 lines of the file.
tail -n 10 Employee_Names.txt

# Display first starting 10 lines of the last 1000 lines of the log file.
tail -n 1000 server.log | head -n 10

# Follow the real time logs being added to the file.
tail -f server.log

# Follow the real time logs but filter out only the lines containing the string ‘ERROR’
tail -f server.log | grep 'ERROR'

The grep Command

This is by far one of the most useful commands in any unix based system because it’s so fast. It stands for “Global Regular Expression Print”, which basically means globally search the provided regular expression and print the result. grep is one of the basic linux commands that can be coupled with other programs to read input in multiple ways. The grep command can be used to find strings or matching regular expressions inside a file or directly from the standard output of another program. If you keep a list of basic linux commands add this to your top 10.

Frequently used options of `grep` command:

-A n                Print n number of lines after the matched line 
Usage:            grep -A 10 abc file.txt

-B n                Print n number of lines before the matched line.
Usage:            grep -B 10 abc file.txt

-C n                Print n lines before as well as after the matched line.
Usage:            grep -C 10 abc file.txt

-c                    Print the count of the matched occurrences of the search string.
Usage:            grep -c abc file.txt

-i                      Ignore case for the search string, match all the upper case, lower case or mixed occurrences.
Usage:            grep -i john names.txt

-r                     Search recursively inside the files of all the subdirectories.
Usage:            grep -r nginx /etc/ , grep -r nginx .

Examples of grep command:
# Searching for a string ’backend’ inside a file called haproxy.conf 
grep backend /path/to/haproxy.conf

# Searching for a string ’Mr Robot’ inside a file called series.txt and
# display 5 rows before and after it too.
grep -C 5 "Mr Robot" series.txt

# Searching all the files in a system (whichever the current user has permission to access) for
# the string ’terminal`, regardless of the case. (`/` denotes the root directory)
grep -r -i "terminal" /

# Print just the count of number of matches in a file.
grep -c "ironman" superheroes.txt

# Using the grep command on the output of some other process/program.
ps -ef | grep -i 'evernote'

# The pipe operator feeds the output of the ps command as an input to the grep command.
# The ps command lists all the running programs in the system, and the grep
# command then searches for the program named “Evernote”. And because the -i
# option is supplied, it searches all cases

The vim Program

vim is an extended version of the program vi which is just a basic text editor. vi stands for “visual” and vim stands for “VI-iMproved”. It was first released in 1991 and can be used in a command line mode as well as a GUI mode. vim is a really stable and highly configurable program (bug free, but most of the commands here are) and can be used to create or modify any kind of text. It supports multiple plugins like code-completion, command line themes etc. vi or vim is available on virtually all the unix based system. The only drawback of vim is that, that it has a very steep learning curve. But once you get used to it, you will never go back.

Features of vim:
  • Highly configurable, has support for hundreds of file types and encodings.
  • Extensive plugin system, you can get a vim plugin for almost everything.
  • Stable (with continuous development) and consistent across all unix platforms.
  • Very low memory fingerprint (runs on systems with 1MB of RAM).
Usage of vim:

To open up a file in vim, just type vim path/to/file. This opens up the file inside the terminal, in normal mode. A normal mode is basically read only mode, but you can enter vim commands in this mode. You can hit the i key in order to get into the interactive mode or you can type :help to view the help guide.

Normal Mode Commands (the mode vim starts in, also referred to as Command Mode)
i                      Enter interactive mode, where you can edit the file.
dd                  delete the current row.
o                    enter in interactive mode, on a new line.
:w                   write/save the changes you have made to the file.
:q                    quit vim, if you have not changed anything.
:q!                   quit vim, if you have made changes but want to discard them.
:wq                 save the change and quit.
/kubernetes  search for the string ‘kubernetes’ in the current file.
n                     go to the next occurrence of the searched string.

Entering back into the Command mode from interactive mode, just press Esc key.


There is a long running joke in the community, that people who open up vim aren’t able to exit it ever, because of how complex it is to exit. I myself faced this problem when I first started using vim. I’d just hit random keys and hope for the best, after giving up, I would usually find the PID of vim and kill it using the kill command.

Here are some of the famous vim memes, make sure to follow Programmer Humor on Facebook for programming related fun.

basic linux commands for text manipulation, vim exit meme

The sed Command

sed stands for stream editor. Using the sed command you can perform search, insertion, deletion, find and replace in a file without ever opening it in a text editor. It’s mainly used for finding and replacing things inside a file, because sed is the fastest way there is to replace something inside a file or inside multiple files. Also, sed comes pre-installed in all the linux distributions.

Examples of sed command:
# Find and replace string, find all occurrences of ‘abc’ and replace them with ‘xyz’. 
sed 's/abc/xyz/' filename.txt

# Replace the 5th occurrence of a pattern in every line.
sed 's/abc/xyz/5' filename.txt

# Starting from 3rd occurrence replace all occurrences in every line.
sed 's/abc/xyz/3g' filename.txt

# Print only the lines on which text was replaced.
sed -n 's/abc/xyz/p' filename.txt

# Display a text file and replace in the output, without actually replacing in the file.
cat filename.txt | sed 's/abc/xyz/'

sed concludes the list of the basic linux commands for text manipulation but the list is not is not over yet. In addition to this, I have made a similar list of top linux commands of all time, make sure to check that out too. Comment if you have any doubts about any of the linux commands discussed in the article. Keep following for other basic linux commands for other system wide applications. Also, if you are on Windows and just want a linux machine on the cloud to learn linux, drop a comment. I’ll be more than happy to help you!