do anything with the last created (touched, accessed) file in the current dir

bar() { foo=$(ls -rt|tail -1) && read -ep "cat $foo? <y/n> " a && [[ $a != "n" ]] && eval "cat $foo" ;}
This command will display the file, but you can change 'cat' to anything else (type 'n' when prompted to cancel the command or anything else to proceed). . Some hints for newbies: type unset bar to make 'bar' function annihilated. For permanent usage you can put this (bar) function in your .bashrc (for bash) or in .profile (for sh). With: . ~/.bashrc you can get all new inserted functions in .bashrc (so the function 'bar' or whatever name you choose) immediately available.
Sample Output
$ bar
cat <y/n> ^C

By: knoppix5
2015-10-21 20:09:33

What Others Think

Hmm, knoppix5... It's not up to your usual standard :-( . The function parses the output of ls, which entirely depends on any alias I might have. Better, would be: /bin/ls . You sort by time (ls -t), then reverse the list (-r) and extract the last entry. It'd be more efficient to use head on the non-reversed list. (Head would read one line and stop, rather than tail reading all the lines). /bin/ls -t | head -1 . You read the response from the user, but don't quote it in the test. If the user types "no thanks", bar() fails. [[ "$a" != "n" ]] . Also you don't need bash's extended test syntax [[ ... ]], unless you want to search for a regex, e.g. [[ ! "$a" ~ ^n ]] matches "n", "no", "non", "nein" ... . Finally, I don't understand why you've used eval. You don't need it and it prevents quoting the filename. Good: cat "$foo" Fu: cat -- "$foo" . My version: bar() { F=$(/bin/ls -t|head -1) && read -ep "cat $F? <y/n> " A && [ ! "$A" = "n" ] && cat -- "$F";}
flatcap · 327 weeks and 2 days ago
@flatcap: >> Hmm, knoppix5... It's not up to your usual standard :-( Your all objections are in place, maybe I'm growing old. OK. Somebody can utilize your function and give me credit for the idea.
knoppix5 · 327 weeks and 2 days ago
great that i found this forum. People here are great. Learned alot. Keep posting more
Killersmile · 43 weeks and 3 days ago

What do you think?

Any thoughts on this command? Does it work on your machine? Can you do the same thing with only 14 characters?

You must be signed in to comment.

What's this? is the place to record those command-line gems that you return to again and again. That way others can gain from your CLI wisdom and you from theirs too. All commands can be commented on, discussed and voted up or down.

Share Your Commands

Stay in the loop…

Follow the Tweets.

Every new command is wrapped in a tweet and posted to Twitter. Following the stream is a great way of staying abreast of the latest commands. For the more discerning, there are Twitter accounts for commands that get a minimum of 3 and 10 votes - that way only the great commands get tweeted.


Subscribe to the feeds.

Use your favourite RSS aggregator to stay in touch with the latest commands. There are feeds mirroring the 3 Twitter streams as well as for virtually every other subset (users, tags, functions,…):

Subscribe to the feed for: