Terminal File Viewing Commands

Terminal is an application that runs commands given by user, actually there is a special program inside Terminal called Shell – which reads in commands and execute them on Mac OS operating system.Terminal’s only job is to open windows and manage shells, while shells actually do real work of Command Execution. You can enter commands in Terminal and then Shell Program running inside terminal will execute those commands.

Commands are a kind of way to interact with Operating System and File System. These can be used for creating files/directories or checking status of processes being run by the Operating System. That’s why these can be really useful, moreover terminal commands can be used for building programming projects as well. Like you can build a Django Framework based website just from Terminal. Moreover, Terminal can be used for compiling/executing multiple files in a large Software Engineering Project. That’s why every beginner in Software Engineering should be aware about What is Terminal, How does it works and what are commands which can be used in it? Also if in case you already don’t know what is terminal then check out this article – What is Terminal in MAC OS?.
Anyway let’s get into what File Viewing Operations can be used in Mac Terminal.

File Viewing Operations Commands

Below is a table containing commands which can be used for viewing files in the file system in Mac.

File Viewing CommandsDescription
catFor viewing entire content in a file
lessFor viewing text files one page at a time
headFor viewing just few first lines of a text file
tailFor viewing just last lines of a text file
nlFor viewing text files with their lines numbered

cat – File Viewing Command

For viewing entire content in a file.
cat command in general can be written as cat [options] [files]
For example – In the below picture, cat index.html command shows all of content in the HTML File.

cat command

Options for cat command

Options for cat commandDescription
cat -v filenameFor printing out any nonpriniting character
cat -t filenameKind of similar to -v option, but can also print tab
cat -e filenameSimilar to -v option, but also print newlines as $
cat -n filenamePrepend line number to every line
cat -b filenamePrepend line numbers to nonblank lines
cat -s filenameSqueeze each sequence of blank lines into a single blank line

cat -v filename example

cat -v filename command

cat -t filename example

cat -t filename command

cat -e filename example

cat -e filename command

cat -n filename example

cat -n filename command

cat -b filename example

cat -b filename command

cat -s filename example

cat -s filename command

less – File Viewing Command

For viewing text files one page at a time and also can be used for editing the file.
less command in general can be written as less [option][filename]
For example – In the below picture, less index.html command opens up file in an editable mode.

less command

Options for less command

Options for less commandDescription
less -c filenameClears the screen before displaying the content in file
less -m filenameDisplays the percentage of the file displayed so far in Terminal window
less -N filenameDisplays line numbers

head – File Viewing Command

For viewing just few first lines of a text file.
head command in general can be written as head [option][filename]
For example – In the below picture, head index.html just shows some few first lines in index.html file.

Options for head command

Options for head commandDescription
head -N filenamePrint first N lines in file
head -c N filenamePrint first N bytes of file

head -N filename command example

head -N filename command

head -c N filename command example

head -c N filename command

tail – File Viewing Command

For viewing just last lines of a text file.
tail command in general can be written as tail [options][filename]
For example – In the below picture, tail index.html command shows some content of index.html file from end of file.

tail command

Options for tail command

Options for tail commandDescription
tail -N filenamePrints last N lines of a file
tail -c N filenamePrints out last N bytes of a file

tail -N filename command example

tail -N filename command

tail -c N filename command example

tail -c N command

nl – File Viewing Command

For viewing text files with their lines numbered.
nl command in general can be written as nl [options][filename]
For example – In the below picture, nl index.html command prints out content of index.html file with lines being numbered.

nl command

Options for nl command

Options for nl commandDescription
nl -v N filenamePrints out lines of a file being numbered, starting with integer N
nl -i N filenamePrints out lines of a file being numbered, incrementing number of line by N each time while moving from one line to next line
nl -s S filenamePrints out lines of a file being numbered, but inserter S string inbetween number and line

nl -v N filename

nl -v N command

nl -i N filename

nl -i N command

nl -s S filename

nl -s S command

Gagan

Hi, there I'm founder of ComputerScienceHub(Started this to bring useful Computer Science information just at one place). Personally I've been doing JavaScript, Python development since 2015(Been long) - Worked upon couple of Web Development Projects, Did some Data Science stuff using Python. Nowadays primarily I work as Freelance JavaScript Developer(Web Developer) and on side-by-side managing team of Computer Science specialists at ComputerScienceHub.io

Leave a Reply

Your email address will not be published. Required fields are marked *

Recent Posts