cat Linux command
cat Linux command
In this tutorial, we will learn about the cat Linux command with examples. The cat command reads one or more files and prints the file contents to the standard output device, i.e., the computer monitor screen. cat is a short form of “concatenate”. To learn more about the command and the command line switches, type the following command:
$ man cat
Syntax
The basic syntax of the cat command is as follows:
$ cat [options] [file…]
Example
# Display the contents of a file
The simple cat command displays the file contents to the screen. For example, to view the file hello.cpp file on the screen.
$ cat hello.cpp
#
The cat command can also be used to create and copy file contents. For example, the below command creates a new file called arguments.cpp, and the file contents of the hello.cpp are copied to the new file.
The > operator redirects the file contents to the file instead of displaying on the standard output screen.
$ cat hello.cpp > arguments.cpp
The command copies the file contents of the source file to the destination file. If the destination file already exists in the file system, then the command overwrites the contents of the destination file.
Create a new file (or overwrite an existing file)
$ cat > newfile.txt
After running this command, you can type content into the terminal. Press Ctrl + D to save and exit. This will create a newfile.txt and save your input into it.
Append content to an existing file
$ cat >> existingfile.txt
cat
command is best suited for viewing small files. For larger files, tools like less
or more
are often better because they allow you to scroll through the file content. It can be useful for creating quick files, combining files, or displaying file contents.Basic Linux Commands
Linux Basic Commands Tutorial page: