Книга: Fedora™ Unleashed, 2008 edition
Printing the Contents of a File with cat
Printing the Contents of a File with cat
Many of Fedora's shell commands manipulate text strings, so if you want to be able to feed them the contents of files, you need to be able to output those files as text. Enter the cat
command, which prints the contents of any files you pass to it. Its most basic use is like this:
$ cat myfile.txt
That prints the contents of myfile.txt
. For this use, there are two extra parameters that are often used: -n
numbers the lines in the output, and -s
("squeeze") prints a maximum of one blank line at a time. That is, if your file has 1 line of text, 10 blank lines, 1 line of text, 10 blank lines, and so on, -s
shows the first line of text, a single blank line, the next line of text, a single blank line, and so forth. When you combine -s
and -n
, cat
numbers only the lines that are printed — the 10 blank lines shown as one will count as 1 line for numbering.
This command prints information about your CPU, stripping out multiple blank lines and numbering the output:
$ cat -sn /proc/cpuinfo
You can also use cat
to print the contents of several files at once, like this:
$ cat -s myfile.txt myotherfile.txt
In that command, cat
merges myfile.txt
and myotherfile.txt
on the output, stripping out multiple blank lines. The important thing is that cat does not distinguish between the files in the output — no filenames are printed, and no extra breaks between the two files. This allows you to treat the two as one or, by adding more files to the command line, to treat 20 files as 1.
- Printing the Contents of a File with cat
- Changing Directories with cd
- Changing File Access Permissions with chmod
- Copying Files with cp
- Printing Disk Use with du
- Finding Files by Searching with find
- Searches for a String in Input with grep
- Paging Through Output with less
- Creating Links Between Files with ln
- Finding Files from an Index with locate
- Listing Files in the Current Directory with ls
- Reading Manual Pages with man
- Making Directories with mkdir
- Moving Files with mv
- Listing Processes with ps
- Deleting Files and Directories with rm
- Printing the Last Lines of a File with tail
- Printing Resource Usage with top
- Printing the Location of a Command with which
- Managing Passwords
- Changing File Access Permissions with chmod
- Creating Links Between Files with ln
- Reading Manual Pages with man
- Shared Cache file
- Безопасность внешних таблиц. Параметр EXTERNAL FILE DIRECTORY
- 4.4.4 The Dispatcher
- Dedications
- About the author
- Chapter 7. The state machine
- Chapter 13. rc.firewall file
- Appendix E. Other resources and links