The `cat` command in Linux is used to concatenate and display the contents of files.
It's one of the most
commonly used commands for viewing file content, creating files, and combining files. Below is a
detailed explanation of the `cat` command along with its options.
Basic Usage:
cat filename
- *Description:* Displays the content of the file `filename` in the terminal.
- *Example:*
cat file.txt
This will display the contents of `file.txt` in the terminal.
Commonly Used Options:
1. *`-n`* (Number all lines)
cat -n filename
- *Description:* Numbers all output lines, starting from 1.
- *Example:*
cat -n file.txt
Output:
1 This is the first line.
2 This is the second line.
2. *`-b`* (Number non-empty lines)
cat -b filename
- *Description:* Numbers only the non-empty lines. Blank lines are not numbered.
- *Example:*
cat -b file.txt
Output:
شائع العنتي.م
1 This is the first line.
2 This is the second line.
3. *`-E`* (Display `` at the end of each line)
cat -E filename
- *Description:* Displays a `` symbol at the end of each line, indicating the end of the line.
- *Example:*
cat -E file.txt
Output:
This is the first lineThis is the second line
4. *`-T`* (Display TAB characters as `^I`)
cat -T filename
- *Description:* Displays TAB characters as `^I` instead of actual tab spaces.
- *Example:*
cat -T file.txt
Output:
This^Iis^Ithe^Ifirst^Iline.
5. *`-A`* (Show all non-printing characters)
cat -A filename
- *Description:* Equivalent to using both `-vET`. It shows all non-printing characters (except for spaces
and tabs) as well as the `` at the end of each line and the `^I` for tabs.
- *Example:*
cat -A file.txt
شائع العنتي.م
Output:
This^Iis^Ithe^Ifirst^Iline
This^Iis^Ithe^Isecond^Iline$
6. *`-v`* (Show non-printing characters)
cat -v filename
- *Description:* Displays non-printing characters (except for spaces, tabs, and newline characters). For
example, control characters are shown as `^` or `M-` notation.
- *Example:*
cat -v file.txt
Output:
This is a text with some^M control characters.
7. *`-s`* (Suppress consecutive empty lines)
cat -s filename
- *Description:* Suppresses (removes) consecutive empty lines, displaying only one empty line instead
of multiple blank lines.
- *Example:*
cat -s file.txt
If `file.txt` has consecutive blank lines, only one blank line will be displayed.
8. *`-e`* (Equivalent to `-vE` – Show non-printing characters and `` at the end of each line)
cat -e filename
- *Description:* Displays non-printing characters and appends `` at the end of each line. It's equivalent
to using `-v` (for non-printing characters) and `-E` (for displaying ``).
- *Example:*
cat -e file.txt
```
شائع العنتي.م
Output:
This is the first lineThis is the second line
9. *`-u`* (Unbuffered output)
cat -u filename
- *Description:* Outputs data without buffering. This is useful when you want real-time output
(especially useful in pipes).
- *Example:*
cat -u file.txt
10. *`--show-nonprinting`* (Show non-printing characters, similar to `-v`)
cat --show-nonprinting filename
- *Description:* Same as `-v`, it shows non-printing characters using `^` notation or `M-` notation.
- *Example:*
11. *`--help`* (Display help message)
cat --help
- *Description:* Displays help information about the `cat` command and its options.
12. *`--version`* (Show version information)
cat --version
- *Description:* Displays the version of the `cat` command installed on your system.
Examples of `cat` Usage:
1. *View the contents of a file:*
cat file.txt
This will display the contents of `file.txt` in the terminal.
2. *Create a new file or append content to a file:*
شائع العنتي.م
cat > newfile.txt
- *Description:* This will create `newfile.txt` and allow you to enter text. Press `Ctrl + D` to save and
exit.
To append to a file:
cat >> existingfile.txt
3. *Concatenate multiple files and display their contents:*
cat file1.txt file2.txt
This will display the contents of `file1.txt` followed by the contents of `file2.txt`.
4. *Concatenate multiple files into a new file:*
cat file1.txt file2.txt > combined.txt
This will combine the contents of `file1.txt` and `file2.txt` into a new file `combined.txt`.
5. *Number the lines of a file:*
cat -n file.txt
This will display the contents of `file.txt` with line numbers.
6. *Suppress consecutive empty lines:*
cat -s file.txt
This will remove extra blank lines, leaving only one blank line where multiple consecutive empty lines
exist.
7. *Show tabs as `^I` and the end of each line with ``:*
cat -T -E file.txt
This will display `^I` for tab characters and `` at the end of each line.
8. *View non-printing characters:*
cat -v file.txt
This will show non-printing characters (like control characters) in the file.
شائع العنتي.م
Summary of `cat` Options:
| Option | Description |
|---------------|--------------------------------------------------|
| `-n` | Number all lines. |
| `-b` | Number non-empty lines only. |
| `-E` | Display `$` at the end of each line. |
| `-T` | Display TAB characters as `^I`. |
| `-A` | Show all non-printing characters (equivalent to `-vET`). |
| `-v` | Show non-printing characters. |
| `-s` | Suppress consecutive empty lines. |
| `-e` | Equivalent to `-vE`, show non-printing characters and ``. |
| `-u` | Unbuffered output (real-time). |
| `--help` | Display help information. |
| `--version` | Show version information. |
شائع العنتي.م