You really shouldn't be parsing the output of
ls either.
http://mywiki.wooledge.org/ParsingLs
http://mywiki.wooledge.org/BashFAQ/087
Probably the safest way to find the newest (or largest, etc.) file is to run through the directory in a simple loop.
Code:
for file in *; do
[[ -f $file && $file -nt $NEWEST ]] && NEWEST=$file
done
echo "$NEWEST"
Since it uses globbing, it's perfectly suitable for use with filenames containing whitespace, even newlines.
You also don't usually need
head just to grab the first line of an input text. The built-in
read can do it just as well. Use it in combination with
process substitution to grab the output of a command instead of a file.
Code:
read -r topline < <( command )
echo "$topline"