Despite this, most people do not take advantage of its full potential.
In this thread, I'll explain what the cat command is and show you some options you probably didn't know about (but you should):
The cat command is one of the most commonly used in Linux. The cat command gets its name from its ability to concatenate files. It has the ability to concatenate files, read and write file contents to standard output.
If you do not specify a filename to read from, or if the filename is replaced with a single hyphen (-), the data will be read from standard input (stdin)
Before we get into how to use the cat command, let's go over the basic syntax.
The syntax for the cat command is as follows:
$ cat [OPTION] [FILE]
- OPTION - to view the available options that are available use the --help (cat --help) option or its man page (man cat).
- FILE - 0 or more files to concatenate or read data from.
Now that you know what the cat command is and how its syntax, let's look at how it's used and some of the options it has.
1. Displaying contents of files
The most basic and common use of the cat command is to read the contents of a file and print it to standard output (stdout).
Here is an example of displaying contents of a specific file:
$ cat chkf [.] sh
2. Print line numbers
The cat command has excellent option (-n) you can use to check the count of lines on a file . If you ever had a large text document and needed to add lines to it, this option is your best friend.
Rather than going through the time-consuming process of manually numbering each line , you can simply use this option with cat from the command line and be done in a matter of seconds.
$ cat -n file
This command is equivalent to the 'nl file' command.
Rather than going through the time-consuming process of manually numbering each line , you can simply use this option with cat from the command line and be done in a matter of seconds.
$ cat -n file
This command is equivalent to the 'nl file' command.
You can then save the output to a file if you wish to, by just using output redirections operators (> and >>).
$ cat -s distros.txt > nolines.txt
The > operator will create a file (nolines.txt) if it doesn’t exist. Otherwise, it will overwrite the file.
If you wish not to overwrite the file use >> operator to append the contents to the end of nolines.txt file
$ cat -s distros.txt >> nolines.txt
4. Concatenating multiple files
With cat command you can also read (concatenate) contents of multiple files and display it to the standard output.
To do so you just separate the file names with spaces.
The following command, for example, will read the contents of file, file2, and file3 and display the results to your standard output (terminal screen)l:
$ cat file file2 file3
This becomes very handy if you want to combine the contents of different files into one file.
5. Distinguish between TABs and Spaces
If you want to visually distinguish between tabs and spaces, use the -T option in conjunction with the cat command.
Tabs will be replaced with the ^I characters.
6. Create files
To create files, use the cat command (without the file argument) followed by the redirection operator (>) and the name of the file you want to create. Press Enter, type your text, and then press CRTL+D to save the file.
$ cat > data.txt
Just like the touch command but cat allows you to have the ability to write text you want to be saved before actually creating a file. You can omit the text if you want to create an empty file.
If the file data.txt exists, it will be overwritten. To append the text to an existing file, use the '>>' operator.
$ cat >> data.txt
This is useful if you want to write to a file without having to open a full-fledged text editor.
7. Display end of lines characters
By default, line endings are not visible when displaying the contents of a file or files. If you want to display the invisible line ending character, use the -e option with the cat command:
$ cat -e data.txt
From the output, the $ character represents the line endings.
That's it!
Did you learn anything new from this thread? If so, please let us know by replying in the comments.
If you're new here, do toss us a follow us (@linuxopsys) for more threads, tips and resources on Linux.
• • •
Missing some Tweet in this thread? You can try to
force a refresh
If you want to learn Linux for FREE, open this (a thread of our Linux threads):
1. Permissions
The operating system controls file access in Linux by utilizing file permissions, attributes, and ownership. In Linux, file permissions, attributes, and ownership determine the level of access that system programs and users have to files.
If you want to learn Linux bash scripting for FREE, open this:
1. What is Shell in Linux
In this guide, let's learn about the shell which is an important part of the Linux operating system which interprets and passes the commands you provide on your keyboard to the operating system to perform some specific tasks
A variable is a named storage location in a program's memory where a value can be stored, retrieved, and manipulated. Like any programming language, Variables are an essential component in the Bash scripts.
10 Linux tr command practical examples you should know as a system administrator (bookmark this):
The tr command short for translate, is one of the most useful command for manipulating text on the command line.
It allows you to perform useful operations such as converting lowercase characters to uppercase characters, uppercase characters to lowercase characters, character replacing, and deleting characters.
It is usually used in conjunction with other commands via piping.
Linux command line substitution allows you to assign the output of a shell command to a variable. This one of the most useful feature of shell scripts.
Learn more in this guide:
After assigning the output to a variable, you can use the stored value anywhere in your shell scripts. This is useful when processing data in your scripts.
There are two ways to assign a command's output to a variable:
- The backtick character (`).
- The $() format.
To do so, encircle the entire command line command with two backtick characters:
In addition to UGO/RWX permissions and ACLs (Access Control Lists), Linux uses file and directory attributes to control the level of access that system programs and users have to files.
Let’s dive in!
File and directory attributes can be set and removed using different commands and utilities. Let's explore some commonly used attributes and how to manage them.
1. Immutable Attribute:
The immutable attribute prevents a file or directory from being modified, renamed, or deleted, even by the root user. It provides an extra layer of security to critical system files or sensitive data.
If you want to learn Linux for FREE, open this (a thread of our Linux threads):
1. Permissions
The operating system controls file access in Linux by utilizing file permissions, attributes, and ownership. In Linux, file permissions, attributes, and ownership determine the level of access that system programs and users have to files.