shell查询指定目录下,大于指定时间,指定时间精确到秒,并且开头为6304-BCUST-的文件,如果有多个,取时间最早的
时间: 2024-03-12 19:50:26 浏览: 25
可以使用以下命令来查询指定目录下大于指定时间,时间精确到秒,并且开头为`6304-BCUST-`的文件,并且取时间最早的文件:
```
find /path/to/directory -type f -name "6304-BCUST-*" -newermt "2022-05-20 10:10:10" -printf '%T@ %p\n' | sort -n | head -n 1 | awk '{print $2}'
```
其中:
- `/path/to/directory`是指定的目录路径,可以替换成需要查询的目录路径。
- `-type f`表示要查找的是文件而非目录。
- `-name "6304-BCUST-*"`表示要查找的文件名必须以`6304-BCUST-`开头。
- `-newermt "2022-05-20 10:10:10"`表示查询大于指定时间的文件,时间精确到秒。
- `-printf '%T@ %p\n'`表示以时间戳和文件名的形式输出结果。
- `sort -n`表示按时间戳排序,时间戳越小表示时间越早。
- `head -n 1`表示取最早的一个文件。
- `awk '{print $2}'`表示只输出文件名,去掉时间戳。
请注意,以上命令中的时间和文件名需要根据实际情况进行替换。
相关问题
shell查询指定目录下满足前缀为6304-BCUST-的文件,如果有多个,取修改时间最小的,输出文件目录
可以使用以下命令来查询指定目录下满足前缀为6304-BCUST-的文件,如果有多个,取修改时间最小的,输出文件目录:
```shell
ls -tr /path/to/directory/6304-BCUST-* | head -n 1
```
其中,`/path/to/directory/` 是指定的目录路径,`6304-BCUST-*` 是文件名的通配符,`ls -tr` 是按照修改时间逆序输出文件列表,`head -n 1` 是取列表中的第一个文件。
注意:如果要查询目录下的所有子目录,可以添加 `-R` 参数,即 `ls -trR /path/to/directory/6304-BCUST-* | head -n 1`。
shell查询指定目录下,大于指定时间,指定时间精确到秒,并且开头为6304-BCUST-的文件,如果没找到,输出“没找到”,如果找到的有多个,输出时间最早的那个文件 ,输出该文件目录。输出该文件文件名,输出该文件的创建日期。注意指定路径和指定时间需要定义为变量
好的,这个问题可以用如下的shell脚本来解决:
```shell
#!/bin/bash
# 定义指定目录和指定时间
dir_path="/path/to/dir"
target_time="2022-01-01 00:00:00"
# 查找满足条件的文件
result=$(find $dir_path -type f -name "6304-BCUST-*" -newermt "$target_time" -print0 2>/dev/null | xargs -0 ls -1 -t | head -n 1)
# 输出结果
if [ -z "$result" ]; then
echo "没找到"
else
echo "目录:$(dirname $result)"
echo "文件名:$(basename $result)"
echo "创建日期:$(date -r $result)"
fi
```
需要替换脚本中的`/path/to/dir`和`2022-01-01 00:00:00`为指定的目录和时间。脚本中使用了`find`命令查找指定目录下的文件,`-type f`表示只查找普通文件,`-name "6304-BCUST-*"`表示文件名以`6304-BCUST-`开头,`-newermt "$target_time"`表示文件修改时间比指定时间新,`-print0`和`xargs -0`用于处理文件名中可能包含空格等特殊字符的情况,`ls -1 -t`用于按照修改时间倒序排序,`head -n 1`用于取第一个文件,即修改时间最早的文件。如果找到了符合条件的文件,就输出该文件所在的目录、文件名和创建日期,否则输出"没找到"。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)