Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

unix

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?

like image 845
Michael W Avatar asked May 16 '12 20:05

Michael W


People also ask

Can you make a symlink to a directory?

Symlink, also known as a symbolic link in Linux, creates a link to a file or a directory for easier access. To put it in another way, symlinks are links that points to another file or folder in your system, quite similar to the shortcuts in Windows.

Can make relative symbolic links only in current directory?

The reason you get "xyz-file: can make relative symbolic links only in current directory" is because for the source directory, you specified a relative path. It'll work as you want it if you specify an absolute path for the source, like so: "cp -sR /root/absolute/path/name dest".

Can you symlink a file?

A symlink is a symbolic Linux/ UNIX link that points to another file or folder on your computer, or a connected file system. This is similar to a Windows shortcut. Symlinks can take two forms: Soft links are similar to shortcuts, and can point to another file or directory in any file system.

How do you create a symbolic link that points to a file?

To create a symbolic link, use the -s ( --symbolic ) option. If both the FILE and LINK are given, ln will create a link from the file specified as the first argument ( FILE ) to the file specified as the second argument ( LINK ).


1 Answers

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
like image 199
Neil Forrester Avatar answered Sep 30 '22 20:09

Neil Forrester