unix 命令查找最近创建的目录

发布于 2024-10-28 12:38:10 字数 138 浏览 1 评论 0原文

我想从最近创建的目录复制文件。在unix下我该怎么做?

例如,如果我将目录名称作为日期戳,如下所示:

/20110311
/20110318
/20110325

I want to copy the files from the most recent directory created. How would I do so in unix?

For example, if I have the directories names as date stamp as such:

/20110311
/20110318
/20110325

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

痕至 2024-11-04 12:38:10

这就是我认为您所问问题的答案。

当我处理名称中包含日期/时间戳的许多目录时,我总是采用您所采用的方法,即 YYYYMMDD - 这样做的好处是日期顺序也是按字母顺序排列的命令。在大多数 shell 中(当然在 bash 中,我对其他 shell 有 90% 的把握),'*' 扩展是按字母顺序完成的,默认情况下 'ls' 返回字母顺序。因此,

    ls | head -1
    ls | tail -1

请提供目录中最早和最晚的日期。

这可以扩展到仅保留最后 5 个条目等。

This is the answer to the question I think you are asking.

When I deal with many directories that have date/time stamps in the name, I always take the approach that you have which is YYYYMMDD - the great thing about that is that the date order is then also the alphabetical order. In most shells (certainly in bash and I am 90% sure of the others), the '*' expansion is done alphabetically, and by default 'ls' return alphabetical order. Hence

    ls | head -1
    ls | tail -1

Give you the earliest and the latest dates in the directory.

This can be extended to only keep the last 5 entries etc.

眼眸 2024-11-04 12:38:10
lastdir=`ls -tr <parentdir> | tail -1`

我不知道如何让反引号与这里的评论系统很好地配合。只需用反引号替换这些撇号即可。

lastdir=`ls -tr <parentdir> | tail -1`

I don't know how to make the backticks play nice with the commenting system here. Just replace those apostrophes with backticks.

您的好友蓝忘机已上羡 2024-11-04 12:38:10

经过一些实验,我得出以下结论:

unix stat 命令在这里很有用。 '-t' 选项使 stat 以简洁模式打印其输出(全部在一行中),该简洁输出的第 13 个元素是最后一个的 unix 时间戳(自纪元以来的秒数)修改时间。此命令将按从最新修改到最旧修改的顺序列出所有目录(和子目录):

find -type d -exec stat -t {} \; | sort -r -n -k 13,13

希望 stat 的“简洁”模式在 stat< 的未来版本中保持一致/em>!

以下是对所使用的命令行选项的一些解释:

find -type d                # only find directories
find -exec [command] {} \;  # execute given command against each *found* file.
sort -r                     # reverse the sort
sort -n                     # numeric sort (100 should not appear before 2!)
sort -k M,N                 # only sort the line using elements M through N.

返回到您的原始请求,要复制文件,可以尝试以下操作。要仅输出单个目录(最近),请将其附加到命令中(注意初始管道),并将其全部输入到带反引号的“cp”命令中。

| head --lines=1 | sed 's/\ .*$//'

After some experimenting, I came up with the following:

The unix stat command is useful here. The '-t' option causes stat to print its output in terse mode (all in one line), and the 13th element of that terse output is the unix timestamp (seconds since epoch) for the last-modified time. This command will list all directories (and sub-directories) in order from newest-modified to oldest-modified:

find -type d -exec stat -t {} \; | sort -r -n -k 13,13

Hopefully the "terse" mode of stat will remain consistent in future releases of stat !

Here's some explanation of the command-line options used:

find -type d                # only find directories
find -exec [command] {} \;  # execute given command against each *found* file.
sort -r                     # reverse the sort
sort -n                     # numeric sort (100 should not appear before 2!)
sort -k M,N                 # only sort the line using elements M through N.

Returning to your original request, to copy files, maybe try the following. To output just a single directory (the most recent), append this to the command (notice the initial pipe), and feed it all into your 'cp' command with backticks.

| head --lines=1 | sed 's/\ .*$//'
街道布景 2024-11-04 12:38:10

基于 ls 的解决方案的问题在于它们不仅仅针对目录进行过滤。我认为这:

cp `find . -mindepth 1 -maxdepth 1 -type d  -exec stat -c "%Y %n" {} \;  |sort -n -r |head -1 |awk '{print $2}'`/* /target-directory/.

可能会解决问题,但请注意,这只会复制直接目录中的文件。如果您想要一个更通用的答案,将最新目录下的任何内容复制到新目录,我认为您最好使用 rsync 之类的:

rsync -av `find . -mindepth 1 -maxdepth 1 -type d  -exec stat -c "%Y %n" {} \;  |sort -n -r |head -1 |awk '{print $2}'`/ /target-directory/ 

但这取决于您想要哪种行为。反引号中的内容的解释是:

  • . - 当前目录(您可能需要在此处指定绝对路径)
  • -mindepth/-maxdepth - 限制 find 命令仅对当前目录的直接子目录
  • -type d - 仅目录
  • 输出修改时间和目录名称
  • -exec stat .. - 从 find 排序-n -r |头-1 | awk '{print $2}' - 日期对目录进行排序并输出最近修改的名称

The trouble with the ls based solutions is that they are not filtering just for directories. I think this:

cp `find . -mindepth 1 -maxdepth 1 -type d  -exec stat -c "%Y %n" {} \;  |sort -n -r |head -1 |awk '{print $2}'`/* /target-directory/.

might do the trick, though note that that will only copy files in the immediate directory. If you want a more general answer for copying anything below your newest directory over to a new directory I think you would be better off using rsync like:

rsync -av `find . -mindepth 1 -maxdepth 1 -type d  -exec stat -c "%Y %n" {} \;  |sort -n -r |head -1 |awk '{print $2}'`/ /target-directory/ 

but it depends a bit which behaviour you want. The explanation of the stuff in the backticks is:

  • . - the current directory (you may want to specify an absolute path here)
  • -mindepth/-maxdepth - restrict the find command only to the immediate children of the current directory
  • -type d - only directories
  • -exec stat .. - outputs the modified time and the name of the directory from find
  • sort -n -r |head -1 | awk '{print $2}' - date orders the directory and outputs the name of the most recently modified
灼疼热情 2024-11-04 12:38:10

如果您的目录名为 YYYYMMDD 就像您的问题所建议的那样,请利用字母通配符。

将所有目录放入一个数组中,然后选择第一个:(

dirs=(*/); first_dir="$dirs";

这实际上是 first_dir="${dirs[0]}"; 的快捷方式。)

类似,对于最后一个

dirs=(*/); last_dir="${dirs[$((${#dirs[@]} - 1))]}";

丑陋的语法,但这就是它的分解:

# Create an array of all directories inside the working directory.
dirs=(*/);

# Get the number of entries in the array.
num_dirs=${#dirs[@]};

# Calculate the index of the last entry.
last_index=$(($num_dirs - 1));

# Get the value at the last index.
last_dir="${dirs[$last_index]}";

我知道这是一个老问题,有一个公认的答案,但我认为这种方法更可取,因为它可以在 Bash 中完成所有操作。没有理由产生额外的进程,更不用说解析ls的输出。 (诚​​然,对于 YYYYMMDD 名称的特殊情况来说,这应该没问题。)

If your directories are named YYYYMMDD like your question suggests, take advantage of the alphabetic globbing.

Put all directories in an array, and then pick the first one:

dirs=(*/); first_dir="$dirs";

(This is actually a shortcut for first_dir="${dirs[0]}";.)

Similarly, for the last one:

dirs=(*/); last_dir="${dirs[$((${#dirs[@]} - 1))]}";

Ugly syntax, but this is what it breaks down to:

# Create an array of all directories inside the working directory.
dirs=(*/);

# Get the number of entries in the array.
num_dirs=${#dirs[@]};

# Calculate the index of the last entry.
last_index=$(($num_dirs - 1));

# Get the value at the last index.
last_dir="${dirs[$last_index]}";

I know this is an old question with an accepted answer, but I think this method is preferable as it does everything in Bash. No reason to spawn extra processes, let alone parse the output of ls. (Which, admittedly, should be fine in this particular case of YYYYMMDD names.)

幻梦 2024-11-04 12:38:10

请尝试使用以下命令
ls -1tr |尾部-1

please try with following command
ls -1tr | tail -1

放赐 2024-11-04 12:38:10
find ~ -type d | ls -ltra

这是我最近学到的,简单又有用。
该命令将以相反的时间顺序显示结果。

find ~ -type d | ls -ltra

This one is simple and useful which I learned recently.
This command will show the results in reverse chronological order.

奶气 2024-11-04 12:38:10

我编写了一个命令,可用于识别文件夹中最新创建的文件夹或文件。这看起来很纯粹:)

#/bin/sh
path=/var/folder_name

newest=`find $path -maxdepth 1 -exec stat -t {} \; |sed 1d |sort -r -k 14 | head -1 |awk {'print $1'} | sed 's/\.\///g'`

find $path -maxdepth 1| sed 1d |grep -v $newest

I wrote a command that can be used to identify which folder or files are created in a folder as a newest. That's seems pure :)

#/bin/sh
path=/var/folder_name

newest=`find $path -maxdepth 1 -exec stat -t {} \; |sed 1d |sort -r -k 14 | head -1 |awk {'print $1'} | sed 's/\.\///g'`

find $path -maxdepth 1| sed 1d |grep -v $newest
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文