如何在 bash 脚本中处理 find 的结果? [英] How can I process the results of find in a bash script?

查看:19
本文介绍了如何在 bash 脚本中处理 find 的结果?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过 find 命令使用数组来存储文件名列表.

I'm trying to use an array to store a list of file names using the find command.

由于某种原因,该阵列无法在学校使用的 bash 中运行,但我的程序可以在我自己的笔记本电脑上运行.

For some reason the array fails to work in the bash used by the school, my program works on my own laptop though.

所以我想知道是否还有其他方法可以做到这一点,这就是我所拥有的:

So I was wondering if there's another way to do it, this is what i have:

array = (`find . -name "*.txt"`)  #this will store all the .txt files into the array

然后我可以使用 cat 命令访问数组项并复制所有文件.

Then I can access the array items and make a copies of all the files using the cat command.

还有其他不使用数组的方法吗?

Is there another way to do it without using an array?

推荐答案

你可以这样使用:

find . -name '*.txt' | while read line; do
    echo "Processing file '$line'"
done

例如,制作副本:

find . -name '*.txt' | while read line; do
    echo "Copying '$line' to /tmp"
    cp -- "$line" /tmp
done

这篇关于如何在 bash 脚本中处理 find 的结果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆