假设我当前的工作目录中有三个目录。每一个都指的是库/包的不同版本。我只想返回最高版本的目录。例如:
program program-1.0 program-2.0
Run Code Online (Sandbox Code Playgroud)
我能做的最好的事情就是使用命令find。
find . -maxdepth 1 -type d -name "program*" -print
Run Code Online (Sandbox Code Playgroud)
但这提供了所有三个目录。我想要目录的完整路径program2。
较新版本的 GNUsort实用程序有一个根据版本号排序的选项:来自man sort
-V, --version-sort
natural sort of (version) numbers within text
Run Code Online (Sandbox Code Playgroud)
所以你可以做
find . -maxdepth 1 -type d -name 'program*' | sort -V | tail -1
Run Code Online (Sandbox Code Playgroud)