0
0
Linux CLIscripting~5 mins

cat (display file contents) in Linux CLI - Commands & Configuration

Choose your learning style9 modes available
Introduction
Sometimes you want to quickly see what is inside a file without opening a text editor. The cat command shows the contents of a file right in the terminal window.
When you want to read a short text file quickly without opening an editor.
When you want to combine multiple files into one by displaying them together.
When you want to check the contents of a configuration file to verify settings.
When you want to pipe the contents of a file into another command for processing.
When you want to create a new file by typing text directly into the terminal.
Commands
Create a file named greetings.txt with the text 'Hello, world!' inside. This sets up a file to display with cat.
Terminal
echo "Hello, world!" > greetings.txt
Expected OutputExpected
No output (command runs silently)
Display the contents of greetings.txt in the terminal so you can read the text inside.
Terminal
cat greetings.txt
Expected OutputExpected
Hello, world!
Create another file named welcome.txt with some text to demonstrate showing multiple files.
Terminal
echo "Welcome to Linux." > welcome.txt
Expected OutputExpected
No output (command runs silently)
Display the contents of both greetings.txt and welcome.txt one after the other in the terminal.
Terminal
cat greetings.txt welcome.txt
Expected OutputExpected
Hello, world! Welcome to Linux.
Create a new file named newfile.txt by typing text directly into the terminal. Press Ctrl+D to save and exit.
Terminal
cat > newfile.txt
Expected OutputExpected
No output (command runs silently)
Display the contents of the newly created file to verify the text was saved correctly.
Terminal
cat newfile.txt
Expected OutputExpected
This is a new file created by cat.
Key Concept

If you remember nothing else from cat, remember: it shows file contents quickly and can combine or create files using simple commands.

Common Mistakes
Trying to use cat on a file that does not exist.
cat will show an error because it cannot find the file to display.
Check the file name and path carefully before using cat.
Pressing Enter instead of Ctrl+D when creating a file with 'cat > filename'.
The file will not save and exit until Ctrl+D is pressed, so it seems stuck.
Press Ctrl+D to signal the end of input and save the file.
Using cat on very large files without paging.
The terminal will flood with text making it hard to read or control.
Use commands like 'less' or 'more' to view large files page by page.
Summary
Use 'cat filename' to quickly display the contents of a file in the terminal.
You can show multiple files at once by listing them after cat.
You can create a new file by typing 'cat > filename' and entering text, then pressing Ctrl+D.