-5
\$\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\$
2
  • \$\begingroup\$ I didn't downvote but it doesn't seem too complicated to me. \$\endgroup\$
    – MD XF
    Aug 4, 2017 at 23:34
  • \$\begingroup\$ I'm not sure your "Reference Implementation" would count as a valid answer. You need to require fs. \$\endgroup\$ Aug 5, 2017 at 13:10

4 Answers 4

3
\$\begingroup\$

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

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

Try it online!

Thanks @DigitalTrauma for -7 bytes!

\$\endgroup\$
0
2
\$\begingroup\$

Pyth - 33 bytes

.w%"\ntail: %s: file truncated"QQ
\$\endgroup\$
2
  • \$\begingroup\$ Can't you remove "QQ? \$\endgroup\$ Aug 5, 2017 at 12:21
  • \$\begingroup\$ @EriktheOutgolfer no, because then .w would write to o.txt \$\endgroup\$
    – Maltysen
    Aug 5, 2017 at 16:11
1
\$\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\$
3
  • \$\begingroup\$ Edit Request: Change TIO link <-- This version makes the input work with the header/footer example code. \$\endgroup\$
    – hyper-neutrino
    Aug 4, 2017 at 23:49
  • \$\begingroup\$ Seems like you don't need the first \n, that would make your solution shorter and correct. \$\endgroup\$ Aug 5, 2017 at 1:49
  • \$\begingroup\$ with python2: -2 bytes \$\endgroup\$ Aug 7, 2017 at 13:43
1
\$\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 and acknowledge you have read our privacy policy.