unix 命令查找最近创建的目录
我想从最近创建的目录复制文件。在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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
这就是我认为您所问问题的答案。
当我处理名称中包含日期/时间戳的许多目录时,我总是采用您所采用的方法,即 YYYYMMDD - 这样做的好处是日期顺序也是按字母顺序排列的命令。在大多数 shell 中(当然在 bash 中,我对其他 shell 有 90% 的把握),'*' 扩展是按字母顺序完成的,默认情况下 'ls' 返回字母顺序。因此,
请提供目录中最早和最晚的日期。
这可以扩展到仅保留最后 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
Give you the earliest and the latest dates in the directory.
This can be extended to only keep the last 5 entries etc.
我不知道如何让反引号与这里的评论系统很好地配合。只需用反引号替换这些撇号即可。
I don't know how to make the backticks play nice with the commenting system here. Just replace those apostrophes with backticks.
经过一些实验,我得出以下结论:
unix stat 命令在这里很有用。 '-t' 选项使 stat 以简洁模式打印其输出(全部在一行中),该简洁输出的第 13 个元素是最后一个的 unix 时间戳(自纪元以来的秒数)修改时间。此命令将按从最新修改到最旧修改的顺序列出所有目录(和子目录):
希望 stat 的“简洁”模式在 stat< 的未来版本中保持一致/em>!
以下是对所使用的命令行选项的一些解释:
返回到您的原始请求,要复制文件,可以尝试以下操作。要仅输出单个目录(最近),请将其附加到命令中(注意初始管道),并将其全部输入到带反引号的“cp”命令中。
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:
Hopefully the "terse" mode of stat will remain consistent in future releases of stat !
Here's some explanation of the command-line options used:
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.
基于 ls 的解决方案的问题在于它们不仅仅针对目录进行过滤。我认为这:
可能会解决问题,但请注意,这只会复制直接目录中的文件。如果您想要一个更通用的答案,将最新目录下的任何内容复制到新目录,我认为您最好使用 rsync 之类的:
但这取决于您想要哪种行为。反引号中的内容的解释是:
.
- 当前目录(您可能需要在此处指定绝对路径)-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:
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:
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 findsort -n -r |head -1 | awk '{print $2}'
- date orders the directory and outputs the name of the most recently modified如果您的目录名为
YYYYMMDD
就像您的问题所建议的那样,请利用字母通配符。将所有目录放入一个数组中,然后选择第一个:(
这实际上是
first_dir="${dirs[0]}";
的快捷方式。)类似,对于最后一个:
丑陋的语法,但这就是它的分解:
我知道这是一个老问题,有一个公认的答案,但我认为这种方法更可取,因为它可以在 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:
(This is actually a shortcut for
first_dir="${dirs[0]}";
.)Similarly, for the last one:
Ugly syntax, but this is what it breaks down to:
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 ofYYYYMMDD
names.)请尝试使用以下命令
ls -1tr |尾部-1
please try with following command
ls -1tr | tail -1
这是我最近学到的,简单又有用。
该命令将以相反的时间顺序显示结果。
This one is simple and useful which I learned recently.
This command will show the results in reverse chronological order.
我编写了一个命令,可用于识别文件夹中最新创建的文件夹或文件。这看起来很纯粹:)
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 :)