Check out a free preview of the full Introduction to Bash, VIM & Regex course

The "head and tail commands" Lesson is part of the full, Introduction to Bash, VIM & Regex course featured in this preview video. Here's what you'd learn in this lesson:

James reviews the head and tail commands. Head command prints the first part of a file while the tail command reads from the end of the file.

Preview
Close

Transcript from the "head and tail commands" Lesson

[00:00:00]
>> James Halliday: Now I just wanna cover some more utilities that you're very likely to find on pretty much any Unix system. So the first one is called head, we've used it a few times already. It prints out starting from the beginning of a file, it prints out a certain number of characters or a certain number of lines starting from the beginning of a file.

[00:00:22]
So if I do head -n5 moby-dick.txt, I see the first five lines. There's also one called tail, tail is like head but it starts at the end, and it reads backwards. So if I want the last five lines, I can do that. Head and tail, if you don't specify a file name, we'll read from standard in by default.

[00:00:48]
So if you have other commands, that's like spammy, and you only care about the last bit or the first bit, see we have our git log, right. If I only want the first line of output, I can pipe into head-n1, and I get just the commit message, which is kind of handy.

[00:01:13]
>> James Halliday: If you read the man page for head, there's different options, -n gives you the number of lines, but if you want the number of characters or bytes you can use -c. So for example if I do had -c 10, I just get the first 10 characters. And you can see that it doesn't give a new line there so you gotta hit enter if you want.

[00:01:37]
Just type it in, or you could do that and that would add a new line. Get a little more into how to chain tick of a commands in a bit.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now