Script to open latest text file from a directory Script to open latest text file from a directory shell shell

Script to open latest text file from a directory


Here you can do something like this

#!/bin/shSOURCE_DIR=/home/juned/DownloadsDEST_DIR=/tmp/LAST_MODIFIED_FILE=`ls -t ${SOURCE_DIR}| head -1`echo $LAST_MODIFIED_FILE#Open filevim $SOURCE_DIR/$LAST_MODIFIED_FILE#Copy filecp $SOURCE_DIR/$LAST_MODIFIED_FILE $DEST_DIRecho "File copied successfully"

You can specify any application name in which you want to open that file like gedit, kate etc. Here I've used vim.

xdg-open - opens a file or URL in the user's preferred application


Not an expert in bash but you can try this logic:

First, grab the latest file using ls -t -t sorts by time head -1 gets the first file

F=`ls -t * | head -1`

Then open the file using and editor:

xdg-open $Fgedit $F...

As suggested by @ AJefferiss you can directly do :

xdg-open $(ls -t * | head -1)gedit $(ls -t * | head -1)


For editing the latest modified / created,

vim $(ls -t | head -1)

For editing the latest in alphanumerical order,

vim $(ls -1 | tail -1)