如何通过壳/ bash中使用find执行每个文件循环? [英] How to perform a for each file loop by using find in shell/bash?

查看:93
本文介绍了如何通过壳/ bash中使用find执行每个文件循环?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何的ls -l <​​/ code>在bash /壳每个 .TXT 文件?

 于行找到-iname'* .TXT'。做
     回声$线
     ls -l命令$线;
DONE

为什么我得到的输出:

  LS:无效选项 - 'E'
尝试`LS --help'以获取更多信息。


解决方案

下面是遍历文件的更好的方法,因为它处理空格和换行符的文件名:

 找到。型的F -iname的* .txt-print0 |而IFS =读-r -d $'\\ 0'线;做
    回声$线
    ls -l命令$线
DONE

How to ls -l each .txt file in bash/shell?

for line in "find . -iname '*.txt'"; do 
     echo $line
     ls -l $line; 
done

Why do I get the output:

ls: invalid option -- 'e'
Try `ls --help' for more information.

解决方案

Here is a better way to loop over files as it handles spaces and newlines in file names:

find . -type f -iname "*.txt" -print0 | while IFS= read -r -d $'\0' line; do
    echo "$line"
    ls -l "$line"    
done

这篇关于如何通过壳/ bash中使用find执行每个文件循环?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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