How VPS - How to use/setup VPS
  • Home
  • Management guides
    • Web servers software
      • Directadmin
      • Hocvps Script
      • Centmin Mod
      • CWP
      • Kloxo-MR
      • Plesk
    • Control Panels
    • Securing VPS/Servers
      • SSL Certificates
      • Upgrading
      • Authentication
  • Operating System
    • CentOS
    • Fedora
    • Debian
    • Linux
    • Arch
    • BSD
    • CoreOS
  • Reviews
  • Coupon
    • Domain Coupon
    • Hosting Coupon
No Result
View All Result
  • Home
  • Management guides
    • Web servers software
      • Directadmin
      • Hocvps Script
      • Centmin Mod
      • CWP
      • Kloxo-MR
      • Plesk
    • Control Panels
    • Securing VPS/Servers
      • SSL Certificates
      • Upgrading
      • Authentication
  • Operating System
    • CentOS
    • Fedora
    • Debian
    • Linux
    • Arch
    • BSD
    • CoreOS
  • Reviews
  • Coupon
    • Domain Coupon
    • Hosting Coupon
No Result
View All Result
How VPS - How to use/setup VPS
No Result
View All Result
Home Operating System Linux

How to Use ‘cat’ and ‘tac’ Commands with Examples in Linux

How VPS by How VPS
November 2, 2018
in Linux, Operating System
0
0
SHARES
103
VIEWS
Share on FacebookShare on Twitter

Contents

  1. Basic Usage of Cat Command in Linux
  2. Learn How to Use Tac Command in Linux
    1. Sample Output

This article is a part of our Linux Tricks and Tips series, in this article we will cover some basic usage of cat command (most frequently used command in Linux) and tac (reverse of cat command – print files in reverse order) with some practical examples.

Read Also: 13 Useful ‘cat’ Command Examples in Linux

Basic Usage of Cat Command in Linux

Cat command, acronym for Concatenate, is one of the most used commands in *nix systems. The most basic usage of the command is to read files and display them to stdout, meaning to display the content of files on your terminal.

# cat file.txt
How to Use ‘cat’ and ‘tac’ Commands with Examples in Linux

View Content of File in Linux

Another usage of the cat command is to read or combine multiple files together and send the output to a monitor as illustrated in the below examples.

# cat file1.txt file2.txt file3.txt
How to Use ‘cat’ and ‘tac’ Commands with Examples in Linux

View Content of Multiple Files

The command can also be used to concatenate (join) multiple files into one single file using the “>” Linux redirection operator.

# cat file1.txt file2.txt file3.txt > file-all.txt
How to Use ‘cat’ and ‘tac’ Commands with Examples in Linux

Join Multiple Files in Linux

By using the append redirector you can add the content of a new file to the bottom of the file-all.txt with the following syntax.

# cat file4.txt >> file-all.txt
How to Use ‘cat’ and ‘tac’ Commands with Examples in Linux

Append Content File to New File

The cat command can be used to copy the content of file to a new file. The new file can be renamed arbitrary. For example, copy the file from the current location to /tmp/ directory.

# cat file1.txt > /tmp/file1.txt 
How to Use ‘cat’ and ‘tac’ Commands with Examples in Linux

Copy Content of File to New File

Copy the file from the current location to /tmp/ directory and change its name.

# cat file1.txt > /tmp/newfile.cfg
How to Use ‘cat’ and ‘tac’ Commands with Examples in Linux

Copy File to /tmp Location

A less usage of the cat command is to create a new file with the below syntax. When finished editing the file hit CTRL+D to save and exit the new file.

# cat > new_file.txt
How to Use ‘cat’ and ‘tac’ Commands with Examples in Linux

Create New File using Cat Command

In order to number all output lines of a file, including empty lines, use the -n switch.

# cat -n file-all.txt
How to Use ‘cat’ and ‘tac’ Commands with Examples in Linux

Add Numbers to Lines in File

To display only the number of each non-empty line use the -b switch.

# cat -b file-all.txt
How to Use ‘cat’ and ‘tac’ Commands with Examples in Linux

Print Line Numbers in File

Want to learn more about Linux cat command? then read our article about 13 Useful ‘cat’ Command Examples in Linux.

Learn How to Use Tac Command in Linux

On the other hand, a lesser known and less used command in *nix systems is tac command. Tac is practically the reverse version of cat command (also spelled backwards) which prints each line of a file starting from the bottom line and finishing on the top line to your machine standard output.

# tac file-all.txt
How to Use ‘cat’ and ‘tac’ Commands with Examples in Linux

Print Content File in Reverse Order

One of the most important option of the command is represented by the -s switch, which separates the contents of the file based on a string or a keyword from the file.

# tac file-all.txt --separator "two"
How to Use ‘cat’ and ‘tac’ Commands with Examples in Linux

Remove Matching String in File

Next, most important usage of tac command is, that it can provide a great help in order to debug log files, reversing the chronological order of log contents.

$ tac /var/log/auth.log

Or to display the last lines

$ tail /var/log/auth.log | tac
Sample Output
[email protected] ~ $ tac /var/log/auth.log
pr  6 16:09:01 tecmint CRON[17714]: pam_unix(cron:session): session closed for user root
Apr  6 16:09:01 tecmint CRON[17714]: pam_unix(cron:session): session opened for user root by (uid=0)
Apr  6 16:05:01 tecmint CRON[17582]: pam_unix(cron:session): session closed for user root
Apr  6 16:05:01 tecmint CRON[17583]: pam_unix(cron:session): session closed for user root
Apr  6 16:05:01 tecmint CRON[17583]: pam_unix(cron:session): session opened for user root by (uid=0)
Apr  6 16:05:01 tecmint CRON[17582]: pam_unix(cron:session): session opened for user root by (uid=0)
Apr  6 16:00:01 tecmint CRON[17434]: pam_unix(cron:session): session closed for user root
....
[email protected] ~ $ tail /var/log/auth.log | tac
Apr  6 16:09:01 tecmint CRON[17714]: pam_unix(cron:session): session closed for user root
Apr  6 16:09:01 tecmint CRON[17714]: pam_unix(cron:session): session opened for user root by (uid=0)
Apr  6 16:05:01 tecmint CRON[17582]: pam_unix(cron:session): session closed for user root
Apr  6 16:05:01 tecmint CRON[17583]: pam_unix(cron:session): session closed for user root
Apr  6 16:05:01 tecmint CRON[17583]: pam_unix(cron:session): session opened for user root by (uid=0)
Apr  6 16:05:01 tecmint CRON[17582]: pam_unix(cron:session): session opened for user root by (uid=0)
Apr  6 16:00:01 tecmint CRON[17434]: pam_unix(cron:session): session closed for user root
Apr  6 16:00:01 tecmint CRON[17434]: pam_unix(cron:session): session opened for user root by (uid=0)
Apr  6 15:55:02 tecmint CRON[17194]: pam_unix(cron:session): session closed for user root
Apr  6 15:55:01 tecmint CRON[17195]: pam_unix(cron:session): session closed for user root
...

Same as cat command, tac does an excellent job in manipulating text files, but it should be avoided in other type of files, especially binary files or on files where the first line denotes the program that will run it.

Source: tecmint.com

Tags: Linux Commandslinux guidelinux vps setup guide
How VPS

How VPS

Related Posts

Failed to download metadata for repo 'appstream' on Centos 8
CentOS

How to fix error: Failed to download metadata for repo ‘appstream’ on Centos 8

February 25, 2022
How to Install Apache Tomcat 8 on CentOS 7
Linux

How to Install Apache Tomcat 8 on CentOS 7?

February 11, 2020
Install Arch Linux With Btrfs Snapshotting
Uncategorized

Install Arch Linux With Btrfs Snapshotting

February 13, 2020
Next Post

WebHostingPad Review

zstd – A Fast Data Compression Algorithm Used By Facebook

How to Download and Extract Tar Files with One Command

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Follow Us

  • 121 Followers
  • 87.2k Followers

Recommended

Install Hiawatha Web Server with PHP-FPM and MySQL on Debian

3 years ago

How To Upgrade CentOS 6 to CentOS 7

3 years ago

How to Install Redaxscript 3.2 CMS on a FreeBSD 11 FAMP VPS

3 years ago

Installing HHVM and Nginx/Apache on Ubuntu/Debian/Mint

3 years ago

Instagram

    Please install/update and activate JNews Instagram plugin.

Categories

  • Arch
  • Authentication
  • Backups
  • BSD
  • Centmin Mod
  • CentOS
  • Control Panels
  • CoreOS
  • CWP
  • Debian
  • Directadmin
  • Encryption
  • Fedora
  • Firewalls
  • Hocvps Script
  • Hosting providers
  • Kloxo-MR
  • Linux
  • Mitigations
  • Operating System
  • Plesk
  • Reviews
  • Securing VPS/Servers
  • Security Patches
  • SSL Certificates
  • Uncategorized
  • Upgrading
  • VPS/Servers management guides
  • Vulnerability Detection
  • Web servers software
  • Webhosting Control Panel

Topics

Apache Web Server Bluehost Review 2019 Bluehost Review 2020 Bluehost Review 2021 Centmin Mod CentminMod centos install htop fsck htop install HTTP DoS attack Install Snort on an Ubuntu install Zabbix on CentOS install Zabbix on CentOS 7 Linux Commands linux guide linux install htop linux vps setup guide MariaDB MariaDB Error Mysql mysqld error optimize MariaDB optimize Mysql snort Ubuntu
No Result
View All Result

Highlights

Top Free Web Hosting Control Panels To Manage VPS/Dedicated Servers

Webmin Reviews

Virtualmin Reviews

CentOS Web Panel Reviews

Ajenti Reviews

ISPConfig Reviews

Trending

Failed to download metadata for repo 'appstream' on Centos 8
CentOS

How to fix error: Failed to download metadata for repo ‘appstream’ on Centos 8

by How VPS
February 25, 2022
0

I tried to update some extensions by use yum on centOs which I specified in Dockerfile. After...

How to Fix MySQL Error "Plugin 'InnoDB' registration as a STORAGE ENGINE failed"?

How to Fix MySQL Error “Plugin ‘InnoDB’ registration as a STORAGE ENGINE failed”?

November 17, 2020
How to optimize Mysql or MariaDB

How to optimize Mysql or MariaDB

November 3, 2020
Top Free Web Hosting Control Panels To Manage VPS/Dedicated Servers

Top Free Web Hosting Control Panels To Manage VPS/Dedicated Servers

February 17, 2020
Webmin Reviews

Webmin Reviews

February 17, 2020
How VPS – How to use/setup VPS

We bring you the best Premium WordPress Themes that perfect for news, magazine, personal blog, etc. Visit our landing page to see all features & demos.
LEARN MORE »

Recent News

  • How to fix error: Failed to download metadata for repo ‘appstream’ on Centos 8 February 25, 2022
  • How to Fix MySQL Error “Plugin ‘InnoDB’ registration as a STORAGE ENGINE failed”? November 17, 2020
  • How to optimize Mysql or MariaDB November 3, 2020

Categories

  • Arch
  • Authentication
  • Backups
  • BSD
  • Centmin Mod
  • CentOS
  • Control Panels
  • CoreOS
  • CWP
  • Debian
  • Directadmin
  • Encryption
  • Fedora
  • Firewalls
  • Hocvps Script
  • Hosting providers
  • Kloxo-MR
  • Linux
  • Mitigations
  • Operating System
  • Plesk
  • Reviews
  • Securing VPS/Servers
  • Security Patches
  • SSL Certificates
  • Uncategorized
  • Upgrading
  • VPS/Servers management guides
  • Vulnerability Detection
  • Web servers software
  • Webhosting Control Panel

[mc4wp_form]

© 2018 JNews - City News Magazine WordPress theme. All rights belong to their respective owners.
JNews is a top selling 2018 WordPress News, Blog, Newspaper & Magazine Theme.

No Result
View All Result
  • Home

© 2023 JNews - Premium WordPress news & magazine theme by Jegtheme.