Michael W
Michael W

Reputation: 3555

Is it possible to create a symlink to the latest file in a directory?

I have a home directory in my unix box. I would like to setup a number or shortcuts in it to point to the latest file in another directory and the link will update if a newer file is created.

Is this possible?

So far I able to get the latest file:

ls -lrt | tail -n1

Thanks

[EDIT]

Perhaps I could even create a shell instead of a softlink which finds the latest file and returns it so I can open/grep/delete etc?

Upvotes: 17

Views: 11143

Answers (2)

Richard Wilson
Richard Wilson

Reputation: 1

I found this.

ln -s "$(ls -t | head -n 1)" latest_file

Upvotes: 0

Neil Forrester
Neil Forrester

Reputation: 5241

In bash, this will make a link to the latest file or directory in "target-directory" called "latest":

ln -s target-directory/`ls -rt target-directory | tail -n1` latest

And this will wait for a change in "target-directory" before returning:

inotifywait -e attrib target-directory

Upvotes: 20

Related Questions