commandlinefu.com is the place to record those command-line gems that you return to again and again.
You can sign-in using OpenID credentials, or register a traditional username and password.
Subscribe to the feed for:
will check if the last byte of filename is a unix newline character. tail -c1 yields the file's last byte and xxd converts it to hex format.
There is 1 alternative - vote for the best!
tail -c 1 "$1" returns the last byte in the file.
Command substitution deletes any trailing newlines, so if the file ended in a newline $(tail -c 1 "$1") is now empty, and the -z test succeeds.
However, $a will also be empty for an empty file, so we add -s "$1" to check that the file has a size greater than zero.
Finally, -f "$1" checks that the file is a regular file -- not a directory or a socket, etc.
If you can do better, submit your command here.
You must be signed in to comment.