Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Script to open latest text file from a directory

I need a shell script to open latest text file from a given directory. it will be then copied to another directory. How can i achieve it?

I need a logic which will search and give the latest file from a directory (name of the text file can be anything (not fixed), so i need to find out latest text file)

like image 810
Priyanka Avatar asked Nov 17 '25 04:11

Priyanka


1 Answers

Here you can do something like this

#!/bin/sh

SOURCE_DIR=/home/juned/Downloads
DEST_DIR=/tmp/

LAST_MODIFIED_FILE=`ls -t ${SOURCE_DIR}| head -1`
echo $LAST_MODIFIED_FILE

#Open file
vim $SOURCE_DIR/$LAST_MODIFIED_FILE

#Copy file
cp $SOURCE_DIR/$LAST_MODIFIED_FILE $DEST_DIR
echo "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

like image 140
Juned Avatar answered Nov 18 '25 19:11

Juned



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!