[英]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.我一直在阅读各种 Stack Overflow 类似的问题,但我仍然无法创建一个成功创建的符号链接,该链接指向给定目标目录中的最新目录。
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/”,并将目标的以下子目录作为“test1/”、“test2/”等。所以总的来说我把这个作为我的结构:
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:首先,我一直在尝试创建一个符号链接“最新”(位于“testSymLinks/”中)以指向“target/”的最新子目录“test2/”,并尝试进行以下尝试:
ATTEMPT 1:尝试 1:
ln -sf target/`ls -rt target | tail -n1` latest
ATTEMPT 2:尝试 2:
ln -sf target/`ls -td -- */ | head -n 1` latest
Both of these attempts still have the symlink "latest" pointing to the directory "target/" instead of the latest sub-directory within "target/" so far.到目前为止,这两种尝试仍然具有指向目录“target/”而不是“target/”中的最新子目录的符号链接“最新”。 I've been checking by running a ls -lrt and a readlink -f latest to verify.
我一直在通过运行ls -lrt和readlink -f latest来验证。
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:所以任务由两个子任务组成:
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. find
打印target
目录中的目录,其上次修改时间为 unix 时间戳%T@
并打印以换行符分隔的目录名称$f
。 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
.我发现您的第一个解决方案有效,但是我建议至少将
-1
选项与ls
。 The second one creates the symlink to target/target/test2
, you should loose the first target/
.第二个创建指向
target/target/test2
的符号链接,您应该松开第一个target/
。 Note that ls
is highly customized utility, mostly is aliased to some ls --color -F
and is not advised to use in scripts.请注意,
ls
是高度自定义的实用程序,主要是某些ls --color -F
别名,不建议在脚本中使用。 Also backticks ` ` are deprecated, use $( .. )
instead.不推荐使用反引号 ` `,请改用
$( .. )
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.