Table of Contents
Today, in this article, we show you how to to use cat Linux command. One of the most used commands in Linux/Unix-like operating systems is the cat command, which means “concatenate”. We can use the cat command to create one or more files, read the contents of a file, concatenate files and redirect the output to a file or terminal.
Since the 1990s, Linux has been a popular operating system. Most of the contemporary technology we use today, including Android phones, televisions, cameras and even smart appliances, is powered by it. This manual will explain how to learn Linux and the practical uses of this free operating system.
Most people associate Linux with a difficult operating system that is used exclusively by programmers. But it’s not as scary as it sounds. The family of Linux operating systems, all open source and based on the Linux kernel, is known as Unix. This covers all the most commonly used Linux-based platforms. Below are the steps to use Linux cat command.
Steps to use cat Linux command
Cat Command Syntax
Step 1: to use the cat commandfollow the format:
cat [options] file name(s)
Step 2: This allows you to issue additional instructions to the cat command. For example, for exhibition the contents of a file with each numbered line, use the -n option:
cat –n filename
Step 3: Specify the name of File (or files) you want to display. If you use more than one file nameeach file will be displayed.
Create a new file
You can create new files and add content to them using the cat command. Create test1.txt and test2.txt, which you can use as sample files to test the other commands.
Step 1: open one terminal window and create the first file:
cat > test1.txt
Step 2: The cursor moves to a new line where you can add the desired text. Type a simple sentence like:
This is test file #1.
Step 3: To exit the prompt and write changes to the file, hold down the key Ctrl key and press d.
Step 4: Repeat the process to create test2.txt. Run:
cat > test2.txt
Step 5: Model:
This is test file #2.
Step 6: Press Ctrl+d.
View contents of a single file
Step 1: To display the content of test1.txt using the cat command run:
cat test1.txt
View contents of multiple files
Step 1: To display the contents of both files, run the command:
cat test1.txt test2.txt
Redirect the contents of a single file
Step 1: Instead of displaying the contents of a file in screenthe cat can put them in a File.
cat test1.txt > test3.txt
Step 2: if the destination file name does not exist, will be created. If you run cat on test3.txt you should see the contents of test1.txt:
cat test3.txt
Step 3: If you export a file that is already existsthis will replace the contents of the file:
cat test2.txt > test3.txt
cat test3.txt
Redirect content from multiple files
Step 1: You may redirect the contents of multiple files in a single file:
cat test1.txt test2.txt > test3.txt
Step 2: View the content of test3.txt with:
cat test3.txt
Display content in reverse order
Step 1: O cat command can display the contents of a file in reverse order (by lines). To do this, use tack (cat in reverse):
tac test3.txt
Append file contents to another file
Step 1: The cat command can add the contents of a file to the final from another file. Instead of using a single > signaluse one Double >> signal:
cat test1.txt >> test3.txt
Step 2: open the test3 file Running:
cat test3.txt
Append text to existing file
Step 1: You can use a similar command to append text to an existing file:
cat >> test1.txt
Step 2: add one new line to file:
This is the second line in test file #1.
Step 3: Hold Ctrl and hit d.
Step 4: Check the content of test1.txt file:
cat test1.txt
Combine Operations
Step 1: The functions of cat command can be combined. For example, to combine the output of two files and store the result in a new file:
cat test1.txt test2.txt > test4.txt
cat test4.txt
Step 2: Alternatively, you can add several files to final from an existing file:
cat test2.txt test1.txt >> test4.txt
cat test4.txt
More and less options (manage large files)
Step 1: If you use cat on a very large file, you will end up up with a huge string of data that is hard to read. You can break it in pages using | most:
cat test4.txt | most
Step 2: This displays a single page of the File. When you press a key, it Scrolling for the next page.
Step 3: If you want the ability to roll front and back through the viewfinder, use | any less.
cat test4.txt | any less
show line numbering
Step 1: You may find it useful to have line numbers in the output, especially for large files. To enable line numbering, add the -n option for the cat command:
cat –n test1.txt
show end of line
Step 1: You may instruct cat to highlight the final of each line and the spaces between the lines with $.
Step 2: To do this, use the command:
cat -e test1.txt
Show lines separated by TAB
Step 1: The cat command has the option to displaying the contents of the file along with the tab space within the text.
Step 2: Show tab separate lines for a sample run:
cat -t test4.txt
Step 3: O tab space within the text is represented by ^ me.
Remove blank lines
Step 1: For omit blank lines from the output of cat with the -s option:
cat -t test4.txt
List all CAT commands
Step 1: If you have trouble remembering options, use the -help command:
cat ––help
Final Words
We hope you like our article about how to use the cat command from Linux. Cat is a very old tool that was used in the first Unix implementation. It may sound funny, but the name is short for “concatenate”. It’s a technical term for putting a bunch of things together. Most users use it to print documents on the screen or in another file. Your computer’s text user interface is called the Linux cat command. It may seem difficult and complicated to use because it is often called a shell, terminal, console, prompt, or other names. However, how is it possible to copy and paste commands from websites.