Using Bash tail to get a particular line

Posted in Bash on May 10, 2019 · 1 min read
Using Bash tail to retreive content by line number

Today we will go through Bash's tail feature to show how to get a particular line of content from a file. If you're wanting to know what you can do with tail check out the manual through the terminal with man tail. Like our previous post, How to use Bash’s cut to modify contents of a file we will be using Bacon Ipsum to generate a paragraph of Ipsum content. After retrieving a paragraph of content separate each sentence to it's own line so your resulting file will look like:

Bacon ipsum dolor amet fatback beef frankfurter salami pork ham hock ground round chuck kielbasa short ribs turkey.
Landjaeger chuck alcatra jowl prosciutto doner.
Shoulder pork tenderloin sirloin salami.
Chuck prosciutto ham brisket filet mignon ball tip pancetta, t-bone corned beef short loin ground round beef.
Chuck beef ribs ham corned beef short loin, filet mignon jerky flank.
Picanha strip steak drumstick beef, tail turkey capicola pancetta shankle short loin.
Drumstick landjaeger chicken swine tongue turkey tri-tip sirloin shank boudin andouille tenderloin.

Save this file as bacon.txt on your desktop and open a terminal and point to the location of the file:

cd; cd Desktop

With tail let's get a line of the file:

tail -n1 bacon.txt

which will output:

Drumstick landjaeger chicken swine tongue turkey tri-tip sirloin shank boudin andouille tenderloin.

the last line, and though it is confusing you could be wanting just the first line of the file but instead of using tail, it would be better to use head:

head -n1 bacon.txt

if you were curious to know how to get the first line. With tail you can get more than one line result:

tail -n2 bacon.txt

and it will return:

Picanha strip steak drumstick beef, tail turkey capicola pancetta shankle short loin.
Drumstick landjaeger chicken swine tongue turkey tri-tip sirloin shank boudin andouille tenderloin.
Help the site go ad free:
Storm Trooper