2

I'm not entirely sure how to categorize this question. It feels like I discovered a bug in tail and/or bash, but obviously it's much more likely this is a bug in my understanding!

I ran the following command:

tail -f -n`wc -l scrape.log` scrape.log

in order to cat the entire file and any new material that is added. However, this tailed the file twice. Running this command:

tail -f -n`wc -l scrape.log`

gave me the desired outcome, tailing the file once. I thought backticks are a Bash escape tool, to "preprocess" a command before running the next one. What is going on here?

3 Answers 3

9

When you use backticks you're essentially passing the output of one command to another. Now let's see what wc -l does:

[cnicutar@fresh ~]$ wc -l /etc/passwd
11 /etc/passwd

It outputs the number of lines and the filename. So your command becomes:

tail -f -n 11 scrape.log scrape.log
Sign up to request clarification or add additional context in comments.

1 Comment

D'oh! This should have been obvious. Thank you for correcting me.
4

If your tail supports it, you can use

tail -f -n +1 scrape.log

to begin the tail with line 1 of the file.

(I am purposefully not answering your question, which cnicutar has already done. I'm just presenting a possible alternative.)

1 Comment

Thank you for the tip. I missed this part of the man page, and it is a more straight-forward way of achieving my desired result.
1

Alternative solution

tail -f -n $(wc -l < scrape.log) scrape.log

Input redirection prevents the printing of the file name.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.