The cat command in Linux is used to display, combine, and create text files. It's short for “concatenate,” and is one of the most frequently used commands for working with file contents directly from the terminal.
cat [options] [filename(s)]
cat file.txtcat file1.txt file2.txt > merged.txtcat > newfile.txt (press Ctrl+D to save)cat >> file.txtcat -n file.txt-n : Number all lines-b : Number non-empty lines only-s : Squeeze multiple blank lines-E : Show $ at the end of each line$ cat notes.txt
Linux is fun!
Learning commands.
$ cat -n notes.txt
1 Linux is fun!
2 Learning commands.
Use cat for quick reading and file concatenation. For long files, tools like less or more may be better for scrolling. Always redirect output to avoid overwriting unintentionally.
The cat command is one of the most frequently used commands in Linux and UNIX systems. Short for “concatenate,” it is primarily used to read and combine files, displaying their contents on the standard output. It's fast, efficient, and incredibly flexible, making it an essential part of any Linux user’s toolkit.
cat [options] [file...]
The command works with one or multiple files and outputs the results to the terminal screen unless redirected.
cat filename.txt
This will output the contents of filename.txt to the terminal.
cat file1.txt file2.txt
This command displays the contents of both files, one after the other.
cat file1.txt file2.txt > combined.txt
Combines the contents of two files into a new file combined.txt.
cat > newfile.txt
After executing this command, you can type the file’s contents directly into the terminal. Press Ctrl+D to save and exit.
cat additional.txt >> existing.txt
This appends the contents of additional.txt to existing.txt.
-n: Number all output lines-b: Number only non-blank lines-s: Squeeze multiple blank lines into one-T: Show tab characters as ^I-E: Show end of lines with $cat -n filename.txt
This will display the file with line numbers.
cat only for small to medium-sized files to avoid overwhelming your terminal.more or less for paging through large files.cat file.txt | grep "keyword"While cat is ideal for quickly viewing content, less and more allow for paginated viewing:
cat: Shows all content at once.more: Displays content one page at a time (forward only).less: Allows forward and backward navigation, search, and more.The cat command is frequently used in bash scripts for tasks like:
cat * in sensitive directorieshead or tail for previewing contents before viewing full filesNo such file or directory – File path is incorrectPermission denied – Use sudo if requiredToo many arguments – Double check input files and syntaxWhile cat is popular, you can also use:
more – Basic paginated viewerless – Advanced pager with search and navigationtac – Like cat but outputs file content in reverseThe cat command is a simple yet powerful Linux utility that every user and developer should understand. Whether you’re viewing logs, merging files, or writing scripts, it serves as a quick go-to solution. Mastering cat not only enhances your command line efficiency but also sets the stage for more advanced shell scripting techniques.
The cat command in Linux is one of the most widely used and powerful text-processing utilities. Short for “concatenate,” the cat command is typically used to display file contents, combine multiple files, create new files, and redirect file output. Whether you're a beginner just exploring the Linux terminal or a seasoned sysadmin writing shell scripts, mastering the cat command is essential for efficient file handling in Unix-based systems.
The cat command is a standard utility used to read, concatenate, and display the contents of text files. It works with standard input and output, making it highly scriptable and compatible with other commands through piping and redirection.
cat [options] [file1] [file2] ...
If no filename is given, cat reads from standard input.
cat filename.txt
This prints the content of filename.txt to the terminal window.
cat file1.txt file2.txt
This concatenates and displays the content of both files, one after another.
cat > newfile.txt
Type the content and press Ctrl+D to save and exit.
cat >> existingfile.txt
This opens the file for appending. Type your content and press Ctrl+D when done.
cat file1.txt > copy.txt
This copies the contents of file1.txt into copy.txt.
cat chapter1.txt chapter2.txt > fullbook.txt
Joins multiple files into a single new file.
cat -n filename.txt
Adds line numbers to each line of output.
cat -b filename.txt
Same as -n but ignores blank lines.
cat -E filename.txt
Displays $ at the end of each line, useful for identifying trailing whitespace.
cat -T filename.txt
Helps visualize tab characters.
cat -s filename.txt
Removes consecutive blank lines and displays only one.
You can use cat to combine binary files (e.g., media files), but use it cautiously:
cat part1.iso part2.iso > full.iso
Send output of cat to another command:
cat filename.txt | grep "error"
This searches for the word "error" in the file.
cat is often used in bash scripts to read files or write content to them:
#!/bin/bash
cat >> log.txt << EOF
This is a new entry logged on $(date)
EOF
cat
Type some text and press Ctrl+D to echo it back.
cat is great for short files, but for long files use less or more to scroll page-by-page:
less largefile.txt
cat won’t show file size directly, but you can count bytes using:
cat filename.txt | wc -c
Use bat (a better alternative to cat) for syntax-highlighted viewing:
sudo apt install bat
bat script.sh
You can use cat to interact with system devices and kernel entries:
cat /proc/cpuinfo
This shows your CPU details.
Here documents allow you to pass multi-line text to a file or command:
cat << EOF > newfile.txt
Line 1
Line 2
EOF
cat -v filename.txt
This helps detect special or problematic characters in files.
cat is useful for quickly inspecting logs:
cat /var/log/syslog | tail -n 20
cat config.txt | tee backup.txt
This displays content on screen and saves a copy simultaneously.
sudo cat filenamecat on compressed or binary files unless necessarycat on sensitive files unless necessaryless or vim -R to prevent accidental editingThe cat command is one of the most versatile and fundamental tools in the Linux command line toolbox. From viewing text files and joining multiple documents to writing scripts and piping output into other commands, cat is simple yet powerful. By understanding its full capabilities, you can streamline many routine Linux operations and increase productivity in file handling, scripting, and debugging.