本文介绍了Bash,根据其名称(即日期)获取最新文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人都可以告诉我如何使用bash根据日期格式将其命名为最新文件夹。例如:

Can anyone tell me how to get the name of the latest folder based on its name which is formatted as a date, using bash. For example:

20161121/
20161128/
20161205/
20161212/

输出应为:20161212

The output should be: 20161212

推荐答案

只需将带有 -nr 标志的 GNU排序用于基于反向数值排序。

Just use GNU sort with -nr flags for based on reverse numerical sort.

find . ! -path . -type d | sort -nr | head -1

一个示例结构,我在当前路径中有以下文件夹的列表,

An example structure, I have a list of following folders in my current path,

find . ! -path . -type d
./20161121
./20161128
./20161205
./20161212

查看排序如何选择所需的文件夹,

See how the sort picks up the folder you need,

find . ! -path . -type d | sort -nr
./20161212
./20161205
./20161128
./20161121

head -1 仅用于首次进入

find . ! -path . -type d | sort -nr | head -1
./20161212

要将其存储在变量中,请使用命令替换 $()

to store it in a variable, use command-substitution $() as

myLatestFolder=$(find . ! -path . -type d | sort -nr | head -1)

这篇关于Bash,根据其名称(即日期)获取最新文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 23:08