简体   繁体   English

如何迭代连接子目录中的文件

[英]how to concatenate files in the subdirectory iteratively

I have a bunch of directories, in each directory I have bunch of sub directories, in each sub directory I have 1 files named x1 and might have y1 z1 as well. 我有一堆目录,在每个目录中都有一堆子目录,在每个子目录中,我有1个名为x1文件, 也可能y1 z1

I want to have a shell script that for each directory, goes through sub directories and concatenate all the x1 s to one file and also all the y1 s and all the z1 s. 我想为每个目录提供一个shell脚本,该脚本会遍历子目录,并将所有x1链接到一个文件以及所有y1和所有z1

I have tried 我努力了

cat ./*/*.x1 >test

I don't know how does it work with for each 我不知道每个如何使用

If you're using zsh this for loop work: 如果您使用zsh for循环工作:

for dir in *(/); do
  cat "$dir"/*.x1 > "$dir"/combined.x1
  [[ -n "$dir"/*.y1 ]] && cat "$dir"/*.y1 > "$dir"/combined.y1
  [[ -n "$dir"/*.z1 ]] && cat "$di"r/*.z1 > "$dir"/combined.z1
done

Update 更新资料

For bash it seems you only need to update the dir pattern: 对于bash ,看来您只需要更新dir模式:

for dir in */; do
  cat "$dir"/*.x1 > "$dir"/combined.x1
  [[ -n "$dir"/*.y1 ]] && cat "$dir"/*.y1 > "$dir"/combined.y1
  [[ -n "$dir"/*.z1 ]] && cat "$di"r/*.z1 > "$dir"/combined.z1
done

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM