Retrieving file content with cat, head, tail and less
This short post provides a brief description of a few simple commands that deal with retrieving file contents.
cat (meaning concatenate)
Shows the full file contents of one or more files. For example:
cat one.txtwill output the single file contents to the terminalcat one.txt two.txtwill output the concatenated contents of both files to the terminal
head and tail
Show the first or last lines of a file. For a file:
head one.txtwill show the first ten lines of the filehead -1 one.txtwill show the first line of the filehead -1 one.txt two.txtwill show the first line of both file
Tail works the same way.
File perusal with less
While the previous commands simply output the file contents to the terminal window, the command line program less provides powerful file perusal (their word, not mine) capabilities. For example: less one.txt opens the file for perusal in less. There are a lot of commands available in less but here the key ones:
hshows all available commandsspacegoes forward one windowqquits
Practical applications
Here are a few practical applications of these programs used in conjunction with others.
head -1 lebowski.txt | pbcopyplaces the first line of the file onto the clipboardcat *.css > new.csscopies the contents from all CSS files within the current directory into new.css (this is just an illustration: there are better ways/means of doing this).