This is a more correct and robust approach than ls -t, at the cost of some additional complexity.
Setup
Add a short shell script (code below) to your $PATH. ~/bin is a good place for it.
Remember to make sure
- the script is executable
chmod +x ~/bin/script_name
~/bin is in your $PATH
Usage
Pass the command you want to run on the newest file in ~/Download to last_download.
Examples
Assuming you named the script last_download
last_download (no arguments): runs evince, the default command, on the newest file in ~/Downloads
last_download mplayer: runs mplayer on the newest file in ~/Downloads
last_download cp -t ~/Desktop: copies the newest file in ~/Downloads to ~/Desktop
Code
#!/bin/sh
# Usage: last_download [cmd [options]...]
newest=
dir=~/Downloads
# default command
if [ $# -eq 0 ]; then
set -- evince;
fi
# find newest file
for f in "$dir"/*; do
if [ -z "$newest" ] || [ "$f" -nt "$newest" ]; then
newest="$f"
fi
done
if ! [ -e "$newest" ]; then
exit 1
fi
# run command on newest file
"$@" "$newest"
Note: The script only looks in ~/Download but it would not be hard to generalize it to support any directory, in which case a name change would also be warranted.
bashthat is both simple and foolproof. I believezshcan do this with the*(om[1])glob. – jw013 Mar 22 '12 at 15:11