我有一个包含20个子文件夹的文件夹,每个子文件夹包含一个文件。我想从子文件夹中读取所有的20个文件,并将它们附加到另一个文件中。我该怎么做?这方面有什么awk命令吗?无论是python/perl脚本还是linux命令都非常有用。
发布于 2017-06-23 05:07:43
最简单的方法可能是(除非您需要比文件内容更多的信息):
cat <directory>/*/* >> <resulting_file>
发布于 2017-06-23 05:23:25
使用find命令,您可以使用该命令限制目录路径/文件类型、修改次数等,默认情况下,find命令用于递归搜索(即从目录到树的最后一个子目录)。
在你的情况下,我想你只是在找任何文件。你可以按下面的方式做。
find <PATH from where to search> -type f > log file # adding filters that you are looking for files.
Ex: find /users/hero/parent -type f > listoffiles.log
如果您正在查找任何特定类型的文件,如日志文件(或)c文件等,则可以添加如下所示的筛选器。
find /path -type f -name *.log > listoflogfiles
注意:默认情况下,查找执行递归搜索。希望这会有所帮助:)
发布于 2017-06-23 05:32:02
使用python(3),您可以使用以下代码:它首先打开新的target_file,然后打开每个source_file并将其内容写入target_file。
import os
path_with_subdirs = '/dir/with/subdirectories'
source_files = [os.path.join(dp, f) for dp, dn, filenames in os.walk(path_with_subdirs) for f in filenames]
target_file = '/target/location/output_file.txt'
with open(target_file, 'w') as target:
for source_file in source_files:
with open(source_file, 'r') as source:
[target.write(line) for line in source]
https://stackoverflow.com/questions/44722127
复制相似问题