[英]Exracting 200 first lines from a zip file and creating this file into a different folder
我有一个名为 /home/myusername/originalFiles 的文件夹,其中有大量 *.gz 大文件。 在这个文件夹及其子文件夹中,我们也有大量的 *.gz 大文件。 在不删除或修改我需要的任何 *.gz 文件的情况下:
a) 对于 /home/myusername/originalFiles(和子文件夹)中的每个文件 f,展开它,
b) 从扩展的 f 中提取前 200 行
c) 将 b) 中的“200 行”文件再次转换为 gz 文件
d) 将 c) 中的“gzipped 200 行”文件复制到另一个名为 /home/myusername/newSampleFiles 的文件夹中,但要遵守 /home/myusername/originalFiles 中的文件夹结构和名称。 因此,如果原始文件 f 位于 /home/myusername/originalFiles/year2020 之类的子文件夹中,则 c) 中的相应“gzipped 200 行”文件必须位于 /home/myusername/newSampleFiles/year2020 中并使用相同的名称和扩展名/home/myusername/originalFiles 中的原始文件
e) 不要保留在 a) 中获得的任何扩展文件
f) 仅使用 Linux cmds 执行此操作
我试过了
寻找。 -type f -name "*.gz" -print | xargs -I@ sh -c 'head -n200 @ > /home/myusername/newSampleFiles/@'
但我收到错误消息:
/home/myusername/newSampleFiles/./someFile.txt.gz:没有这样的文件或目录
while read file;
do
file2="${file%.*}"
gzip -cd "$file" | head -n200 > "/home/myusername/newSampleFiles$file2";
gzip -c "/home/myusername/newSampleFiles$file2" > "/home/myusername/newSampleFiles$file"
done <<< "$(find /path/to/dir -type f -name "*.gz")"
将 find 命令重定向到 while 循环中,将 output 的每一行读取到变量文件中,然后使用参数扩展从 file1 中剥离任何文件扩展名并将结果读取到 file2 中。 然后在 gzip 命令中使用这些变量。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.