70

I am writing a shell script in OSX(unix) environment. I have a file called test.properties with the following content:

cat test.properties gets the following output:

//This file is intended for 
//blah blah purposes
123

Using cat command, how can I get only the last line of the file ?

4
  • 4
    Use : tail -1 file
    – sat
    Commented Oct 18, 2016 at 12:41
  • 1
    Yup. thanks a many. Commented Oct 18, 2016 at 12:47
  • 1
    It's probable because this is one of the "How do I use X to do Y?" questions where X does not do Y. Such questions are not as useful as "How do I do Y?" or "What does X do?".
    – Oskar Skog
    Commented Oct 18, 2016 at 20:37
  • 1
    // , Those of us who think X will do Y, when, in reality, Z does Y (ahem) do benefit from such questions, in spite of their dubious pedagogy. Commented Sep 26, 2018 at 20:43

2 Answers 2

135

Don't use cat. tail was meant for this usecase exactly:

$ tail -1 ./test.properties
3
  • 1
    last small question: if I want to get the last but second line or last but third line ? Commented Oct 18, 2016 at 13:02
  • 17
    combine tail and head for example: tail -2 | head -1 Commented Oct 18, 2016 at 15:33
  • 1
    // , très élégant Commented Sep 26, 2018 at 20:47
12

Don't have enough reputation to comment Mureinik's post to answer your last question, but if you want to display a part of a file between known lines, you can try sed -n '<line1>,<line2>p <filename>.

If you don't know the lines, mix tail and sed : For your file :

tail -r test.properties | sed -n '2p'

Not the answer you're looking for? Browse other questions tagged or ask your own question.