That's a great question! While we used cat to read a single file, it's actually quite a "multi-purpose tool." Here are some other common ways developers use it:
1. Combine Multiple Files
You can list multiple files, and cat will display them one after the other. This is where the name "concatenate" really comes from:
cat file1 file2
2. View Line Numbers
If you are looking at code or a long list and want to see line numbers, use the -n flag:
cat -n /tmp/hello
Output:
1 Hi,
2 I am Labby!
3. Create a New File
You can quickly create a small file and type content into it directly from the terminal using the > redirection symbol:
cat > my_new_file.txt
(After typing this, you would type your text and press Ctrl+D to save and exit.)
4. Merge Files into a New One
You can take two existing files and merge them into a brand-new third file:
cat file1 file2 > combined_file.txt
5. Append to an Existing File
If you want to add the contents of one file to the end of another without overwriting it, use >>:
cat file1 >> existing_file.txt
For now, the most important use case to remember is viewing file contents so you can compare them, which is exactly what we're doing in this lab! Feel free to try cat -n /tmp/hello in your terminal right now to see the line numbers in action.