Cat with lines linux

Cat command in Linux with examples

Cat(concatenate) command is very frequently used in Linux. It reads data from the file and gives their content as output. It helps us to create, view, concatenate files. So let us see some frequently used cat commands.

1) To view a single file
Command:

2) To view multiple files
Command:

3) To view contents of a file preceding with line numbers.
Command:

4) Create a file
Command:

5) Copy the contents of one file to another file.
Command:

6) Cat command can suppress repeated empty lines in output
Command:

7) Cat command can append the contents of one file to the end of another file.
Command:

8) Cat command can display content in reverse order using tac command.
Command:

9) Cat command can highlight the end of line.
Command:

10) If you want to use the -v, -E and -T option together, then instead of writing -vET in the command, you can just use the -A command line option.
Command

11) Cat command to open dashed files.
Command:

12) Cat command if the file has a lot of content and can’t fit in the terminal.
Command:

12) Cat command to merge the contents of multiple files.
Command:

13) Cat command to display the content of all text files in the folder.
Command:

14) Cat command to write in an already existing file.

Command :

This article is contributed by Pranav. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

Источник

Linux/Unix: Cat Command Display Line Numbers

H ow do I use the cat command to display the line numbers for a file called myapp.c under Linux or Unix like operating systems?

You can use the cat command concatenate files and show on the screen under Linux or Unix like operating systems.

Tutorial details
Difficulty level Easy
Root privileges No
Requirements cat command on Linux or Unix
Est. reading time 2 mintues

The cat command also number all output lines starting with number one with the following syntax:

Syntax For Cat Command To Display Line Numbers

The syntax is as follows:
cat -n fileNameHere
cat —number foo.c
We can use the more command/less command as filter when text can not be fitted on the screen:
cat —number foo.c | more
cat —number foo.c | less

Displaying line number using cat command

The -b / —number-nonblank option number all nonempty output lines, starting with one and the syntax is:
cat -b fileNameHere
OR
cat —number—nonblank filename

  • No ads and tracking
  • In-depth guides for developers and sysadmins at Opensourceflare✨
  • Join my Patreon to support independent content creators and start reading latest guides:
    • How to set up Redis sentinel cluster on Ubuntu or Debian Linux
    • How To Set Up SSH Keys With YubiKey as two-factor authentication (U2F/FIDO2)
    • How to set up Mariadb Galera cluster on Ubuntu or Debian Linux
    • A podman tutorial for beginners – part I (run Linux containers without Docker and in daemonless mode)
    • How to protect Linux against rogue USB devices using USBGuard

Join Patreon

Removing empty output while showing line numbers

Finally, you suppress or remove repeated empty output lines with the -s / —squeeze-blank option:
cat -s -n fileNameHere
cat -s -n /etc/resolv.conf
Sample outputs:

OR (GNU only syntax)
cat —squeeze-blank -n filename

Say hello to nl command

Use the nl command number lines of files under Linux or Unix oses. The syntax is:

Examples

Create a text file called hello.c as follows:

Use the cat or nl command to display line numbers:

Fig.01: Displaying line numbers for hello.c using the cat and nl command.

A note about sed

To just print 3rd line use the sed command:
sed -n 3p /etc/resolv.conf
To just print 3rd and 5th line use the sed command:
sed -n -e 3p -e 5p /etc/resolv.conf
To see specific range, say show lines between 3 to 5, run:
sed -n 3,5p /etc/resolv.conf

Summing up

You learned various commands that can display line numbers on Linux or Unix-like systems. See the following man page:
man cat
man nl
man sed

🐧 Get the latest tutorials on Linux, Open Source & DevOps via

Category List of Unix and Linux commands
Documentation help • mandb • man • pinfo
Disk space analyzers df • duf • ncdu • pydf
File Management cat • cp • less • mkdir • more • tree
Firewall Alpine Awall • CentOS 8 • OpenSUSE • RHEL 8 • Ubuntu 16.04 • Ubuntu 18.04 • Ubuntu 20.04
Linux Desktop Apps Skype • Spotify • VLC 3
Modern utilities bat • exa
Network Utilities NetHogs • dig • host • ip • nmap
OpenVPN CentOS 7 • CentOS 8 • Debian 10 • Debian 8/9 • Ubuntu 18.04 • Ubuntu 20.04
Package Manager apk • apt
Processes Management bg • chroot • cron • disown • fg • glances • gtop • jobs • killall • kill • pidof • pstree • pwdx • time • vtop
Searching ag • grep • whereis • which
Shell builtins compgen • echo • printf
Text processing cut • rev
User Information groups • id • lastcomm • last • lid/libuser-lid • logname • members • users • whoami • who • w
WireGuard VPN Alpine • CentOS 8 • Debian 10 • Firewall • Ubuntu 20.04

Comments on this entry are closed.

Hi Thanks for sharing … Its of great help …
And I m a great fan of your … because I found 90% of what i think on cyberciti.

Just a little correction here in this post ..
“cat –number–nonblank filename ”
is giving error
cat: unrecognized option ‘–number–nonblank’
Try ‘cat –help’ for more information.

Just make it
“cat –number-nonblank filename”
a single hyphen – after number

But we had a trouble. “cat” command does not stop display when the text is full screen.
So, we have any other option to resolve this in-convenience.

The standard output could instead be redirected using a pipe (represented by a vertical bar) to a filter (i.e., a program that transforms data in some meaningful way) for further processing. For example, if the file is too large for all of the text to fit on the monitor screen simultaneously, as is frequently the case, the text will scroll down the screen at high speed and be very difficult to read. This problem is easily solved by piping the output to the filter less, i.e.,

cat file1 | less

This allows the user to advance the contents of the file one screenful at a time by pressing the space bar and to move backwards by pressing the b key. The user can exit from less by pressing the q key.

Источник

13 Basic Cat Command Examples in Linux

The cat (short for “concatenate“) command is one of the most frequently used commands in Linux/Unix-like operating systems. cat command allows us to create single or multiple files, view content of a file, concatenate files and redirect output in terminal or files.

In this article, we are going to find out the handy use of cat commands with their examples in Linux.

General Syntax of Cat Command

1. Display Contents of File

The below example will show the contents of /etc/passwd file.

2. View Contents of Multiple Files in terminal

In below example, it will display the contents of the test and test1 file in the terminal.

3. Create a File with Cat Command

We will create a file called test2 file with the below command.

Awaits input from the user, type desired text, and press CTRL+D (hold down Ctrl key and type ‘d‘) to exit. The text will be written in the test2 file. You can see the content of the file with the following cat command.

4. Use Cat Command with More & Less Options

If a file having a large number of content that won’t fit in the output terminal and the screen scrolls up very fast, we can use parameters more and less with the cat command as shown below.

5. Display Line Numbers in File

With the -n option you could see the line numbers of a file song.txt in the output terminal.

6. Display $ at the End of File

In the below, you can see with the -e option that ‘$‘ is shows at the end of the line and also in space showing ‘$‘ if there is any gap between paragraphs. This option is useful to squeeze multiple lines into a single line.

7. Display Tab Separated Lines in File

In the below output, we could see TAB space is filled up with the ‘^I‘ characters.

8. Display Multiple Files at Once

In the below example we have three files test, test1, and test2, and able to view the contents of those files as shown above. We need to separate each file with ; (semicolon).

9. Use Standard Output with Redirection Operator

We can redirect the standard output of a file into a new file else existing file with a ‘>‘ (greater than) symbol. Careful, existing contents of the test1 will be overwritten by the contents of the test file.

10. Appending Standard Output with Redirection Operator

Appends in existing file with ‘>>‘ (double greater than) symbol. Here, the contents of the test file will be appended at the end of the test1 file.

11. Redirecting Standard Input with Redirection Operator

When you use the redirect with standard input ‘ Tags cat command Examples

If You Appreciate What We Do Here On TecMint, You Should Consider:

TecMint is the fastest growing and most trusted community site for any kind of Linux Articles, Guides and Books on the web. Millions of people visit TecMint! to search or browse the thousands of published articles available FREELY to all.

If you like what you are reading, please consider buying us a coffee ( or 2 ) as a token of appreciation.

We are thankful for your never ending support.

Источник

Linux cat Command

This article will focus on all useful aspects of “cat” commands. However, this command is also highly suitable for performing some really tricky tasks in scripting.

Cat usage

The binary is located in the “/usr/bin/cat” location.

This tool is a part of the GNU coreutils package. The source code of GNU coreutils is readily available on GitHub.

  • Display file content

I’ve created a text file with random data. The data was grabbed from random bytes generator by Random.org.

Let’s check out the content of the file using “cat”.

The “cat” tool can print the output of any file that the current user has the permission to read. For example, “/etc/passwd” file is accessible to any user to just “read”.

However, it can’t access something that only “root” has permission to. In this case, the file “sudo_random.txt” is the exact copy of the original “random.txt” but only “root” having access to it.

  • Contents of multiple files

The structure of this command is similar to the basic usage of “cat”. All you have to do is pass the files with their location one by one.

It can be performed in a different manner as well.

  • Create a file using “cat”

It’s not actually a core function of the “cat” command. However, it can serve the task quite easily.

After running this command, you can type whatever you want and then, press “Ctrl + D”. It will write the user input to the file.

If you want just a blank file, then press “Ctrl + D” without typing anything.

If you’re accessing a file that’s too large, then scrolling through the output of “cat” command becomes really, really annoying. In that case, we can redirect the output to “more” or “less” for more convenience.

For example, the demo text file I’m using is quite big. If you’re working with log files, this is a familiar scenario. In such situations, “more” or “less” can offer significant value. The “more” tool displays the output one page at a time. The “less” tool is similar to “more” but with additional features. However, we’re not going to dive deeper into these tools.

Let’s redirect the output of “cat” to “more”.

To quit the view, press Q.

For pipelining the output to “less”, use this command.

Same as “more”, quit by pressing Q.

When “cat” displays the content, it doesn’t show the numbering of the lines. Use the “-n” flag.

You can use this with “more” or “less” as well.

When using “-n” flag, “cat” shows line numbering for all the lines, including empty and non-empty ones. However, using the “-b” flag, “cat” will only number the non-empty ones.

Note: This flag will override “-n” by default.

How about replacing the “end of line” with $?

Here, “cat” prints the output with both the line number and replacing the “end of line” with $ symbol.

Using the following command, you can swap the tab spaces with “^I” character.

Within the chaos of characters, it’s hard to find out those tabs, right?

  • Suppress repeated empty lines

In some cases, there could be multiple empty lines in-between the content. In that case, use “-s” flag to eliminate the empty lines in the output.

  • Redirect output

We can use the standard output format to redirect the output of any “cat” command to a file. If the file already exists, it will be overwritten. Otherwise, it’ll be created.

This command can also be used to merge the contents of multiple files into one single file.

If you don’t want to overwrite the content of an existing file, you can append the “cat” output at the end.

Just like before, it’s possible to append the content of multiple files into the same file.

  • Showing non-printing characters

A text file isn’t just all the showing characters. There are a number of hidden characters that are non-printable. If you need to show them, use the “-v” flag.

“cat” alternative

While “cat” is a crucial part of every single UNIX/Linux system, there are reliable alternatives to print the content of a text file. Here, I’ll be showing off “bat” – a “cat” clone with wings!

The “bat” tool is readily available on all the major Linux distros. It comes up with its own style. You can customize the output with themes, pager, formats and a lot more.

Let’s see how “bat” shows the content of my demo file.

As you can see, “bat” shows line number and the file name by default. Moreover, it uses the “more”-like scrolling by default. For getting out of the window, press Q.

Let’s see if “bat” successfully makes a copy of the file.

Using “bat”, it’s possible to perform all the “cat” functions without any trouble. For complete documentation, check out the official bat GitHub page.

Final thoughts

There are plenty of scenarios where “cat” and “bat” can be useful. For all the available options, there’s nothing better than the man and info pages.

Источник

Читайте также:  Когда появилась ос линукс
Оцените статью