【问题标题】:A shell script to read the content of the file under folders and write them into a text file用于读取文件夹下文件内容并将其写入文本文件的 shell 脚本
【发布时间】:2015-05-30 21:13:38
【问题描述】:

我有 53000 个文件夹(命名为数字 1、2、3 ...),每个文件夹中都有一个名为“prop”的文件。我需要将它们的数据收集到一个文本文件中。我用了那个命令。但是有些文件夹有一个空白的“prop”文件。我需要知道我的文本文件中哪些是空白的。

#!/bin/sh
a=0
while [ $a - le 53000]
do 
a= 'expr $a + 1'
cat $a/prop >> x.txt
done

【问题讨论】:

  • x.txt 文件夹中没有文件时,你想添加什么信息?
  • 我和@higuaro 有同样的问题加上这个问题:首先你说:I have 53000 folders (named as numbers 1,2,3 ...) inside *each* of which there is a single file called "prop",但后来你说:But Some of the folders have no "prop" file. - 所有文件夹都有一个prop 文件或只有一些其中?
  • 对不起,其中一些有一个空白的“道具”文件
  • 问题指出“某些文件夹没有“prop”文件。”它还指出“我需要知道哪些是空白的”。这是两个非常不同的事情。当您说“空白”时,您的意思是文件存在但没有内容还是文件不存在?

标签: shell scripting


【解决方案1】:

这会将所有现有prop 文件的内容复制到x.txt。这还将在missing.txt 中添加详细说明任何丢失文件的消息:

cat {1..53000}/prop >x.txt 2>missing.txt

例如,如果文件515/prop 丢失,那么missing.txt 中将有一行说明:

cat: 515/prop: No such file or directory

这需要bash 或任何其他支持大括号扩展的高级shell。

【讨论】:

    【解决方案2】:

    您可以从包含编号文件夹的目录中,look for directories that don't contain your specific file 使用

    find . -mindepth 1 -maxdepth 1 -type d '!' -exec test -e "{}/prop" ';' -print
    

    例子:

     ls *
    1:
    prop
    
    2:
    prop
    
    3:
    
    
    $ find . -mindepth 1 -maxdepth 1 -type d '!' -exec test -e "{}/prop" ';' -print
    ./3
    

    【讨论】:

      猜你喜欢
      • 2021-04-24
      • 1970-01-01
      • 1970-01-01
      • 2013-03-26
      • 2020-04-23
      • 1970-01-01
      • 2011-12-19
      • 2015-11-15
      • 1970-01-01
      相关资源
      最近更新 更多