tail: troll: file truncated

-4

1

Background

When you run tail -f file in bash, the file is outputted and then any subsequent appends.

However, when you remove something that has already been displayed, tail outputs:

tail: nameoffile: file truncated

Your Challenge

When given an input f, append tail: [value of f]: file truncated (with trailing & leading newline) to the file f. You can assume that file f exists, the device is not full, f is not empty, and that you have permissions to write to f.

Test Cases

Input: something

File before:

Some
thing

File after:

Some
thing
tail: something: file truncated

Input relative/path/to/file

File before:

tail: relative/path/to/file: file truncated

File after:

tail: relative/path/to/file: file truncated
tail: relative/path/to/file: file truncated

Reference Implementation (Node.js)

x=>require("fs").appendFileSync(x,`
tail: ${x}: file truncated
`)

programmer5000

Posted 2017-08-04T23:13:39.070

Reputation: 7 828

Sandbox (deleted) – programmer5000 – 2017-08-04T23:16:19.967

Why the downvotes? – programmer5000 – 2017-08-04T23:30:04.683

I didn't downvote but it doesn't seem too complicated to me. – MD XF – 2017-08-04T23:34:02.643

I'm not sure your "Reference Implementation" would count as a valid answer. You need to require fs. – Thomas W – 2017-08-05T13:10:19.267

Answers

4

Bash, 4̴0̴ 4̴6̴ 33 bytes

echo tail: $1: file truncated>>$1

Try it online!

Thanks @DigitalTrauma for -7 bytes!

ბიმო

Posted 2017-08-04T23:13:39.070

Reputation: 15 345

3

Pyth - 33 bytes

.w%"\ntail: %s: file truncated"QQ

Maltysen

Posted 2017-08-04T23:13:39.070

Reputation: 25 023

Can't you remove "QQ? – Erik the Outgolfer – 2017-08-05T12:21:16.730

@EriktheOutgolfer no, because then .w would write to o.txt – Maltysen – 2017-08-05T16:11:18.130

2

Python 3, 61 bytes

f=input();open(f,"a").write("\ntail: %s: file truncated\n"%f)

Can't really try it online... Try it online!

LyricLy

Posted 2017-08-04T23:13:39.070

Reputation: 3 313

Edit Request: Change TIO link <-- This version makes the input work with the header/footer example code.

– HyperNeutrino – 2017-08-04T23:49:54.447

Seems like you don't need the first \n, that would make your solution shorter and correct. – ბიმო – 2017-08-05T01:49:14.510

with python2: -2 bytes

– Felipe Nardi Batista – 2017-08-07T13:43:26.850

2

Batch, 47 bytes

@echo(>>%1
@echo tail: %~1: file truncated>>%1

Assuming DOS newlines are acceptable on Windows...

Neil

Posted 2017-08-04T23:13:39.070

Reputation: 95 035