如何列出包含文件的所有子目录

如何解决如何列出包含文件的所有子目录

| 我必须完成以下任务: 对于给定的文件名列表和给定的目录,请为每个文件名打印其中出现的所有子目录。子目录将根据文件的创建日期以降序打印。 我不知道该怎么做     

解决方法

for file in $(< filelist) 
do 
    find -name \"$file\" -printf \"%C@ %h\\n\" | sort -rn | sed \'s/^[0-9.]* //\'
done 
$(<文件列表)是重定向,避免了新进程(与cat相对) printf%C @以秒为单位打印自1970年1月1日以来的时间 %h打印不带文件名的前导目录 sort -n对数字排序,-r反向 sed删除排序后的时间 问题: 从这个问题来看,没有指示要打印文件名本身,这在现实世界中是不常见的。这很容易解决。 带有换行符的路径名以及其他花哨字符(如垂直制表符,换页符等)处理不善。     ,因为提出个人最喜欢的解决方案非常有趣,所以我将加入: 解决方案(扰流板) @用户未知: 我知道您正在拖钓,但出于休闲读者的利益: 我承认那里的内联扩展确实不安全。我真的在展示我打算如何结合使用命令(ls,find,uniq)等。可以保证安全:
find -type f -printf \'%f\\0\' | sort -uz |
    xargs -0i -n1 find -name {} -printf \"%f:%p\\n\"
现在,格式化不太方便(然后,您将如何在路径名中使用特殊字符来格式化?)。问题在这个问题上说得很少,所以... 最后一点:
the first read is already broken with (...) blanks in filenames
不正确
while read
仅用换行符打断,请尝试:
read line <<< \"a b c\" && echo \"\'$line\'\"
原始的教学指针 采用
dirname
     a/b/c/file -> a/b/c
basename
     a/b/c/file -> file
ls -t / ls -tr
     sort files by timestamp; note that \'creation date\' is likely a fob because hardly any filesystem stores these reliably (it is usually the last change date for the inode)

find -type f
find -type d
     find files or directories
sort | uniq
sort -u
     sort lines and remove adjacent (subsequent) duplicates
让我知道是否有帮助。除了指向UNIX命令的指针外,我还可以进行一些细微的改进     ,您可以使用
for
(用于循环浏览文件)和
find
的组合来搜索文件。
cat > filenames <<EOF
file-a
file-b
file-c
EOF

export DIR=dir-d

for filename in `cat filenames`; do find $DIR -name $filename ; done
查看“查找”手册页,以了解您可以做些什么;您可以轻松地运行其他命令(使用
-exec
)。     ,它仅在Linux下工作,但在OP中:
#!/bin/sh

for file in fileNameA fileNameB; do
  echo \"File ${file} is in the following directories:\"
  find sourceDir -type f -name ${file} -printf \"%T@\\t%h\\n\"|sort -rn|cut -f2
done
提示:如果省略“ 11”部分,则可以看到文件的创建(最后修改)时间。     ,该文件的创建日期通常不可用。在标准文件系统上,您可以访问最后的修改时间,最后的访问时间以及元数据的最后更改时间。除了这个问题,这里还有一个不错的脚本来完成其他部分: #!/ bin / sh F = $(mktemp) 陷阱\“ rm -f $ F \” 0 找到$ {1?用法:$(basename $ 0)path [file ...]}> $ F 转移 测试$#-gt 0;做   grep $ 1 $ F   转移 做完了 该脚本的一个不错的功能是它只运行一次查找。要进行排序,一种好的技术是将要对其排序的统计信息打印到tempfile中,然后简单地传递while循环的输出以进行排序。如评论中所述,grep并不是特别可靠,因为它可能会匹配不完全匹配的路径。     

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


依赖报错 idea导入项目后依赖报错,解决方案:https://blog.csdn.net/weixin_42420249/article/details/81191861 依赖版本报错:更换其他版本 无法下载依赖可参考:https://blog.csdn.net/weixin_42628809/a
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下 2021-12-03 13:33:33.927 ERROR 7228 [ main] o.s.b.d.LoggingFailureAnalysisReporter : *************************** APPL
错误1:gradle项目控制台输出为乱码 # 解决方案:https://blog.csdn.net/weixin_43501566/article/details/112482302 # 在gradle-wrapper.properties 添加以下内容 org.gradle.jvmargs=-Df
错误还原:在查询的过程中,传入的workType为0时,该条件不起作用 &lt;select id=&quot;xxx&quot;&gt; SELECT di.id, di.name, di.work_type, di.updated... &lt;where&gt; &lt;if test=&qu
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct redisServer’没有名为‘server_cpulist’的成员 redisSetCpuAffinity(server.server_cpulist); ^ server.c: 在函数‘hasActiveC
解决方案1 1、改项目中.idea/workspace.xml配置文件,增加dynamic.classpath参数 2、搜索PropertiesComponent,添加如下 &lt;property name=&quot;dynamic.classpath&quot; value=&quot;tru
删除根组件app.vue中的默认代码后报错:Module Error (from ./node_modules/eslint-loader/index.js): 解决方案:关闭ESlint代码检测,在项目根目录创建vue.config.js,在文件中添加 module.exports = { lin
查看spark默认的python版本 [root@master day27]# pyspark /home/software/spark-2.3.4-bin-hadoop2.7/conf/spark-env.sh: line 2: /usr/local/hadoop/bin/hadoop: No s
使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams[&#39;font.sans-serif&#39;] = [&#39;SimHei&#39;] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -&gt; systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping(&quot;/hires&quot;) public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate&lt;String
使用vite构建项目报错 C:\Users\ychen\work&gt;npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-