-4
\$\begingroup\$

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
`)
\$\endgroup\$
  • \$\begingroup\$ Sandbox (deleted) \$\endgroup\$ – programmer5000 Aug 4 '17 at 23:16
  • \$\begingroup\$ Why the downvotes? \$\endgroup\$ – programmer5000 Aug 4 '17 at 23:30
  • \$\begingroup\$ I didn't downvote but it doesn't seem too complicated to me. \$\endgroup\$ – MD XF Aug 4 '17 at 23:34
  • \$\begingroup\$ I'm not sure your "Reference Implementation" would count as a valid answer. You need to require fs. \$\endgroup\$ – Thomas W Aug 5 '17 at 13:10
4
\$\begingroup\$

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

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

Try it online!

Thanks @DigitalTrauma for -7 bytes!

\$\endgroup\$
3
\$\begingroup\$

Pyth - 33 bytes

.w%"\ntail: %s: file truncated"QQ
\$\endgroup\$
  • \$\begingroup\$ Can't you remove "QQ? \$\endgroup\$ – Erik the Outgolfer Aug 5 '17 at 12:21
  • \$\begingroup\$ @EriktheOutgolfer no, because then .w would write to o.txt \$\endgroup\$ – Maltysen Aug 5 '17 at 16:11
2
\$\begingroup\$

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!

\$\endgroup\$
  • \$\begingroup\$ Edit Request: Change TIO link <-- This version makes the input work with the header/footer example code. \$\endgroup\$ – HyperNeutrino Aug 4 '17 at 23:49
  • \$\begingroup\$ Seems like you don't need the first \n, that would make your solution shorter and correct. \$\endgroup\$ – ბიმო Aug 5 '17 at 1:49
  • \$\begingroup\$ with python2: -2 bytes \$\endgroup\$ – Felipe Nardi Batista Aug 7 '17 at 13:43
2
\$\begingroup\$

Batch, 47 bytes

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

Assuming DOS newlines are acceptable on Windows...

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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