This article explains using the
cat command in Ubuntu Linux to view file content. It also provides some examples for you to try.
cat command in Linux is a command line utility that allows you to view the contents of a file, create new files, concatenate files, and even append text to existing files. Its name is derived from “concatenate,” which reflects its main functionality of combining multiple files.
The primary use of the cat command is to display the contents of one or more files in the terminal. It’s a simple and efficient way to quickly view the contents of a file without having to open it in an editor.
Additionally, the command provides a versatile set of features to manipulate and view file contents, making it a useful tool for developers, system administrators, and users.
The basic syntax of the
cat the command is as follows:
cat [OPTION]... [FILE]...
OPTION specifies various options that can modify the behavior of the command and
FILE refers to the file or files you want to perform operations on.
Viewing file contents
The primary use of the
cat command is to display the contents of one or more files in the terminal. To view the contents of a file, simply specify the file name as an argument:
Creating a new file
cat, you can create a new file and enter text into it directly from the command line. To do so, type
cat followed by the redirection operator (
>), and the name of the file you wish to create.
Press Enter, and then start typing your text. Finally, press
Ctrl + D save and exit the file.
cat > newfile.txt This is some example text.
Press Ctrl + D to save and exit when you’re done typing.
cat command can also be used to concatenate multiple files into one. To do this, list the names of the files you want to combine as arguments, separating them with spaces:
cat file1.txt file2.txt > combined.txt
This command will concatenate the contents of
file2.txt into a new file named
Appending text to a file
If you want to add text to the end of an existing file without overwriting its contents, you can use the append operator (
>>). This is especially useful when you want to add content to a log or configuration file.
cat >> file.txt This text will be appended to the end of the file.
Press Ctrl + D to save and exit.
By using the
-n option, you can display the lines of a file preceded by line numbers. This is particularly helpful when analyzing log files or large code files.
cat -n file.txt
That should do it!
This post showed you how to use the cat command in Ubuntu Linux. Please use the comments form below if you find errors or have something to add.