- How to Use Linux Cat Command
- Basic Usage of Linux Cat Command:
- Printing Line Numbers:
- Numbering Only Non Blank Lines:
- Removing Repeating Empty Lines:
- Printing Tab Characters:
- Printing End of Line Characters:
- Printing Non-Printing, Tabs, and EOL Characters:
- About the author
- Shahriar Shovon
- how to display line numbers when viewing a file in linux
- In Text Editors
- Vi Editor
- Nano Editor
- Kate Editor
- GEdit Editor
- Other Command Line Tools
- cat
- less
- nl and more
- With the Linux «cat» command, how do I show only certain lines by number
- 6 Answers 6
How to Use Linux Cat Command
The Linux cat command is used to print the contents of a text file. With the Linux cat command, you can print the contents of your c, java source file, Linux configuration files etc.
The cat command is available in every Linux distribution out there by default. So, you don’t have to install it separately.
In this article, I am going to show you how to use the Linux cat command. So, let’s get started.
Basic Usage of Linux Cat Command:
The basic and most common use of the Linux cat command is to use it without any command option.
For example, to view to contents of the /etc/hosts directory, run the cat command as follows:
As you can see, the contents of the /etc/hosts configuration file are printed on the screen.
Printing Line Numbers:
Let’s say, you want to print the contents of a Java source file on the terminal. You can use the cat command of course. But the cat command does not show line numbers by default. For a source file or a program, it is essential. Luckily, the cat command has -n option which you can use to display line numbers.
To display the contents along with the line number of the Java source file Welcome.java, run the Linux cat command as follows:
As you can see, the line numbers are displayed.
Numbering Only Non Blank Lines:
If you want to show line numbers for the lines that are not blank only, you can use the -b option of the Linux cat command.
In the previous Java source file Welcome.java, I’ve added some blank lines just to demonstrate how the -b option works.
As you can see, with the -n option, all the lines (including blank lines) are numbered.
With the -b option, only the lines that are not blank are numbered as you can see in the screenshot below.
Removing Repeating Empty Lines:
A file you’re trying to view may have lots of empty lines one after the other. This will make the output of cat command very long and annoying.
You can use the -s option of the Linux cat command to remove repeated empty lines as follows:
Printing Tab Characters:
In a source code file of a program, you may have used many tab characters. Fortunately, they are invisible by default. But, if you really need to see all the tab characters you have on your file, then you can use the -T option of the Linux cat command.
Where you might need this feature is when you want to replace all the tab characters with white spaces and you want to make sure that there are not any tab characters left.
To display all the tab characters in our Welcome.java source file, the Linux cat command can be used as follows:
As you can see, the tab characters are displayed as ^I.
Printing End of Line Characters:
If you want to print the EOL (End of Line) character which is represented by $, you can use the -E option of the Linux cat command.
For example, to print the EOL characters of Welcome.java, run the Linux cat command as follows:
As you can see, the EOL characters are printed.
Printing Non-Printing, Tabs, and EOL Characters:
Earlier, you had to use the -v option to print the non-printable characters, use the -T option to print the tab characters, and use the -E option to print the EOL characters. What if you need to print all of these? Well, you can combine all of these options together as follows:
But there is a better solution. The Linux cat command has a -A option that does just the same thing with less typing.
As you can see, the outputs are the same.
So, that’s basically how you use Linux cat command to display text files on Linux. Thanks for reading this article.
About the author
Shahriar Shovon
Freelancer & Linux System Administrator. Also loves Web API development with Node.js and JavaScript. I was born in Bangladesh. I am currently studying Electronics and Communication Engineering at Khulna University of Engineering & Technology (KUET), one of the demanding public engineering universities of Bangladesh.
This post and this website contains affiliate links. See my disclosure about affiliate links.
how to display line numbers when viewing a file in linux
While viewing files in Linux, especially text files it is often informative and useful to see the line numbers. Text based editors and utilities such as vi, nano, cat or less can all display line numbers with a command line argument or with an command with in the editor. Many text editors with a graphical interface will display line numbers by default, but you can always toggle it from the options to enable or disable it.
If you just want to find the total number of lines in a file, then you can do that without even opening and viewing the file in an editor. This post deals only the use case where you need to see the line content alongside the line number.
In Text Editors
Almost all text editors does have an option to display line numbers, most of them are turned on by default. We will just look at some of most the popular ones, couple of text based interfaces and a couple of graphical interfaces.
Vi Editor
Vi (or the Vim variation) is probably the most popular editor in Linux with a text user interface. To display the line numbers, first you need to enter the escape mode. You can do that by hitting the escape key at any time. In escape mode, enter following command
The se nu is the shortcut for the original command, set number. This will display the line numbers in the left hand side margin of the editor.
Once you have enabled the display of the line numbers, you might want to disable the them later. You can remove the line numbers from being displayed by using the following command.
The above :se nu! can be used to both enable and disable the line numbers. It toggles the line number display depending on the current state.
Nano Editor
Nano is another basic text editor in Linux, with a text based user interface. It is probably not as feature rich as Vi, but is widely supported over many distributions. Being a very simple editor, Nano does not have the ability to display line numbers as Vi or other editors do. The best you can do is to display the line number of the current cursor position.
The above command will start the nano editor with the option to constantly display the cursor position at the bottom of the editor. You can make this option permanent by modifying the nano config file, named .nanorc. Add the option set const to the file to persist.
If you already have the file open in an editor then you can use the keyboard shortcut Alt + C to toggle the option to display the cursor position.
Kate Editor
Kate is a text editor with a graphical user interface. It is the default text editor for KDE, and is quite feature rich.
You can toggle the line number display from the menu bar by going to View -> Show Line Numbers. Selecting that option will display the line numbers on the left hand side margin of the editor window. You can disable it by deselecting the same option.
You can also use the keyboard shortcut F11 to toggle this setting.
GEdit Editor
GEdit is another popular text editor, especially in Gnome desktop environment. Just as the Kate editor above, you can turn on line numbers from the menu bar. Click on the Edit option in the menu bar and select Preferences. This will open the Preferences dialog box.
In the dialog box, select the View tab and then select the checkbox next to Display line numbers. This will enable the line numbers in the editor window.
Other Command Line Tools
Aside from using a text editor to view your text files, there are also some command line utilities that can be used to print the file to the console. It might sometimes be helpful to display the line numbers along side the printed file.
cat
Even though it is mostly used for merging and concatenating files, it can also be used to display the file contents in the console. There are two command line options available in the cat command that allows you to display line numbers, –number (-n) and –number-nonblank (-b), according to your requirement.
The option -n or –number will print out the line numbers of all lines, including the blank or empty lines in the file. If you like to count and display line numbers only for the non-empty lines in the file, then use the -b or –number-nonblank option as shown below.
less
Another useful utility is less which allows files to be displayed by screenful and makes it easier to scroll the file contents as well, unlike the cat command. The option to enable line numbering in less is -N or –LINE-NUMBERS.
The option -N can be used while viewing the file as well. Typing -N will toggle the line numbering feature of the displayed file which can be quite useful.
nl and more
The more command does not have a line number feature. You can however use the nl command to display the line numbers in the file and then pipe the output through more to achieve a similar functionality.
With the Linux «cat» command, how do I show only certain lines by number
If I use cat -n text.txt to automatically number the lines, how do I then use the command to show only certain numbered lines.
6 Answers 6
$ cat file Line 1 Line 2 Line 3 Line 4 Line 5 Line 6 Line 7 Line 8 Line 9 Line 10
To print multiple lines (5 & 8)
$ sed -n -e 5p -e 8p file Line 5 Line 8
To print specific range (5 — 8)
$ sed -n 5,8p file Line 5 Line 6 Line 7 Line 8
To print range with other specific line (5 — 8 & 10)
$ sed -n -e 5,8p -e 10p file Line 5 Line 6 Line 7 Line 8 Line 10
Note that for a range including the first line (for example first 5 lines), you’ll use sed -n 1,5p . not sed -n 0,5p . . At least for the version of sed installed by default on Mac OS 10.15.
One way of doing it is by using sed :
where 11 is the number of the line you want removed.
And cat -n isn’t even needed:
You can use awk straight up.
replacing ‘1’ with the desired line number.
I concur that this is the very best answer. To add on top you can use equivalence operators to filter data out ie. awk ‘NR>=20’ file.txt
Depending on goals I like head or grep
cat /var/log/syslog -n | head -n 50 | tail -n 10
will return lines 41 thru 50.
cat /var/log/syslog -n | grep » 50″ -b10 -a10
will show lines 40 thru 60. The problem with the grep method is that you have to use account for padding of the line numbers (notice the space)
Both are quite handy for parsing log files.
well yeah. but. but. . There are better ways. The question asked about using cat though, so I used it.
As others have shown you, there is no need to use cat -n . Other programs will do it for you. If, however, you really need to parse the output of cat -n and show only specific lines (for example, 4-8, 12 and 42), you could do:
In awk , $1 is the first field, so this command prints all lines whose first fields are i) between 4 and 8 (inclusive) or ii) 12 or iii) 42.
If you also want to remove the field added by cat -n to get the original lines from the file, you can do:
$ cat -n file | awk '$1>=4 && $1' Line 4 Line 5 Line 6 Line 7 Line 8 Line 12 Line 42