简体   繁体   English

bash打印x使用ls找到文件之前的文件数

[英]bash print x number of files before file found using ls

I have a directory which has a bunch of files that are in yyyymmdd.hhmmss format. 我有一个目录,其中包含一堆格式为yyyymmdd.hhmmss的文件。 I am looking for the x number of files before a certain time is inquired. 我在查询特定时间之前正在寻找文件的x数。

For example, I have a directory with files that are named every 2 minutes (eg, 20190205.000200, 20190205.000400, 20190205.000600, ... , 20190205.235800). 例如,我有一个目录,其中包含每2分钟命名的文件(例如20190205.000200、20190205.000400、20190205.000600,...,20190205.235800)。 I want the name of x files before a time is entered. 在输入时间之前,我想要x个文件的名称。 For an example, let's say I choose the time to be '20190205.200000', and I want 15 files. 例如,假设我选择时间为'20190205.200000',并且我想要15个文件。 This would produce the files from 20190205.193000 to 20190205.20000 (15 files at 2-min timestep = half hour). 这将产生从20190205.193000到20190205.20000的文件(2分钟的时间步长=半小时,包含15个文件)。 Is there a way for the 'ls' command to do this (perhaps even with awk, grep, sed, etc.)? 有没有办法让“ ls”命令执行此操作(甚至使用awk,grep,sed等)? Or would it be easier to convert to a datetime and loop through every 2 minutes? 还是更容易转换为日期时间并每2分钟循环一次?

You could list the content of the directory, grep the name of the file and display X entry before the match 您可以列出目录的内容,grep文件名,并在匹配之前显示X条目

With these files 有了这些文件

 🢒 ls -1   
20190205.0000000
20190205.0000200
20190205.0000400
20190205.0000600
20190205.0000800
20190205.0001000
20190205.0001200
20190205.0001400
20190205.0001600
20190205.0001800
20190205.0002000

You could do 你可以做

 🢒 ls -1 | grep 20190205.0001400 -B 3 
20190205.0000800
20190205.0001000
20190205.0001200
20190205.0001400

Where the -B flag it used to set the number of lines to show before the match -B标志用于设置比赛前要显示的行数

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM