cat

>Usage

The cat command is primarily used to concatenate(link together) and display the contents of files. It is useful for viewing file contents, combining files, and creating new files.

>Synopsis

cat [option] file

>Options

Displaying File Contents: the cat command displays the contents of a file to the terminal. E.g (cat file1.txt) This command displays the contents of file1.txt.

Example:

Dots

-zsh

> cat file1.txt


Hello, this is file1.txt! It contains some text data.

-n

Number Lines: adds line numbers to each line of output. (cat -n file1.txt) This command displays file1.txt with each line numbered.

Example:

Dots

-zsh

> cat -n file1.txt



1 Hello, this is file1.txt!
2 This file has multiple lines.
3 Each line will be numbered.

-e

Show End of Line: shows $ at the end of each line. (cat -e file1.txt) This command displays file1.txt with $ marking each line end.

Example:

Dots

-zsh

> cat -e file1.txt



Hello, this is file1.txt$
This file has multiple lines$
Each line will end with a dollar sign$

>

Creating New Files: when you run the command in a terminal, it opens the file for writing. After running this command, you'll be able to type text into the terminal, and it will be saved.

cat > newfile.txt: this command creates a new file named newfile.txt and allows you to type content. Press Ctrl+D to save and exit.

Example:

Dots

-zsh

> cat > newfile.txt


Hello, this is a text file! I am writing some text into the file.

After pressing Ctrl D, the file newfile.txt contains:

Dots

-zsh

>


Hello, this is a text file! I am writing some text into the file.

Concatenating Files: you can use cat to concatenate multiple files into a single output or to create a new file (cat file1.txt file2.txt > combined.txt). This command concatenates file1.txt and file2.txt, writing the combined content into combined.txt.

Example:

Dots

-zsh

> cat file1.txt file2.txt > combined.txt



This is the first file.
It has some text.
This is the second file.
It also has some text.

>Quiz

Progress: Question ? out of ?

Question Title

Page written by Sweetmoreton Beryl