It seems that the purpose of cat
is to concatenate several files. However, many people still use cat
instead of less
(or a similar program like more
) to display a file. See, for example, the GNU m4 manual and the answer "How can I display the contents of a text file on the command line?".
Man page: less
-F or --quit-if-one-screen
Causes
less
to automatically exit if the entire file can be displayed on the first screen.-X or --no-init
Disables sending the termcap initialization and deinitialization strings to the terminal. This is sometimes desirable if the deinitialization string does something unnecessary, like clearing the screen.
Nowadays, is it a good practice to use cat
to display or view a file? Why use cat
to view a file?
This makes me think to Useless Use Of Cat.
Note: This question is not about the differences between less
and more
. Moreover, it concerns the visualization of a file created earlier.
According to the answers and comments, it seems that cat
is used beyond its use because it is easier to use than a pager (e.g. more
, less
...). Some people think this is an irrelevant fact (or useless) but experience shows that various subtleties pertaining to the shell may have practical consequences: use a shell loop to process a text file, use unquoted variables...
Negative consequences vary in intensity. For example, cat foo bar | less
is valid because the user concatenates two files but cat foo | less
is not valid. In the same spirit, cat
seems to be required in "a pipeline" although it seems that a pager like less
works in a pipeline too (note: less
is not suited in all cases concerning displaying, e.g. Reading a named pipe: tail or cat?).
See also: How to cat a file with "or" options
cat
is easier: how many keystrokes do you need to typecat
vs.less -F
? – wurtel Apr 08 '19 at 13:57cat
you can continue your work in the same terminal and the output is still visible to lookup something, copy & paste etc. – Freddy Apr 08 '19 at 14:02-X
, otherwise I don't see anything for small files... – pLumo Apr 08 '19 at 14:27cat | blah
is "correct" in general practice is just fanning the flames for another pointless FOSS Holy War. – 0xSheepdog Apr 08 '19 at 14:28-X disables the termcap de-/inititialization
, so not clearing screen at the end. – pLumo Apr 08 '19 at 14:48alias cat='less'
would be a good idea to I train my brain/fingers? After all aliases are not used in scripts and most other uses at the console would fall in the "Useless uses ofcat
" category anyway. – xenoid Apr 08 '19 at 16:11