简体   繁体   中英

How to create a symbolic link that points to the latest directory in a target directory?

I've been reading various Stack Overflow similar questions and I still am having trouble creating a symbolic link that gets successfully created pointing to the latest directory in a given target-directory.

Back story of this is that we have a deployment script that needs to use the latest release version within a directory that is referenced with a symbolic link.

For experimenting with this structure, I've created a simple directory "testSymLinks/target/" and have the following sub-directories of target as "test1/", "test2/", etc. So overall I have this as my structure:

testSymLinks/
--target/
**latest (symbolic link)
-----test1/
-----test2/ (created second / as the latest, new directory)

First, I've been trying to create a symbolic link "latest" (sitting in "testSymLinks/") to point to the latest sub-directory of "target/", which is "test2/", with the following attempts:

ATTEMPT 1:

ln -sf target/`ls -rt target | tail -n1` latest

链接,因为显然我还不能发布图片,我需要一个反引号 #NotNewbieFriendly

ATTEMPT 2:

ln -sf target/`ls -td -- */ | head -n 1` latest

链接,因为显然我还不能发布图片,我需要一个反引号 #NotNewbieFriendly

Both of these attempts still have the symlink "latest" pointing to the directory "target/" instead of the latest sub-directory within "target/" so far. I've been checking by running a ls -lrt and a readlink -f latest to verify.

链接,因为显然我还不能发布图片#NotNewbieFriendly

I'm trying to see what I'm missing here and was hoping to find any advice, explanations, and suggestions so that I may not only be able to solve my problem, but to also understand why it's not working in the first place.

This works for me to create a symlink pointing to the last directory created in a folder:

$ mkdir target/f
$ mkdir target
$ mkdir target/a
$ mkdir target/b
$ mkdir target/g
$ mkdir target/c
$ mkdir target/h
$ mkdir target/d
$ ls target
a  b  c  d  f  g  h
$ ln -s $(ls -t --group-directories-first target | head -n 1) target/latest
$ ls target -la
total 0
drwxrwxrwx 1 paulos paulos 4096 Apr 12 18:18 .
drwxrwxrwx 1 paulos paulos 4096 Apr 12 18:17 ..
drwxrwxrwx 1 paulos paulos 4096 Apr 12 18:17 a
drwxrwxrwx 1 paulos paulos 4096 Apr 12 18:17 b
drwxrwxrwx 1 paulos paulos 4096 Apr 12 18:17 d
drwxrwxrwx 1 paulos paulos 4096 Apr 12 18:17 f
drwxrwxrwx 1 paulos paulos 4096 Apr 12 18:17 g
drwxrwxrwx 1 paulos paulos 4096 Apr 12 18:17 h
lrwxrwxrwx 1 paulos paulos    1 Apr 12 18:18 latest -> d

try this

$ ln -sf "$(ls -1dt target/*/ | sed 1q)" latest

note that there is no error handling...

So the task is composed of two subtasks:

  • Finding the latest directory
  • Creating the symlink

The second step is easy, the first one is hard.

This is what I have end up with:

ln -sf "target/$(find target -type d -mindepth 1 -maxdepth 1 -printf "%T@ %f\n" |
    sort -n -k1 | tail -n1 | cut -d' ' -f2-)" latest

find prints directories inside target directory with it's last modification time in unix timestamp %T@ and prints the directory name $f separated with newlines. Then I numeric sort the list using the first field, get the last line and remove the timestamp. After that the creation of the symlink stays the same.

I have found that your first solution work, however I would advise to at least use -1 option with ls . The second one creates the symlink to target/target/test2 , you should loose the first target/ . Note that ls is highly customized utility, mostly is aliased to some ls --color -F and is not advised to use in scripts. Also backticks ` ` are deprecated, use $( .. ) instead.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM