庆典:处理(递归)通过目录中的所有文件 [英] bash: processing (recursively) through all files in a directory

查看:83
本文介绍了庆典:处理(递归)通过目录中的所有文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想编写一个bash脚本,(递归)处理某一类型的所有文件。

我知道我可以通过使用发现正是如此获得匹配的文件列表:

找到。 -name* .EXT

我想在脚本中使用这样的:


  1. 与给定​​扩展名的文件的递归obatin列表

  2. 获得完整的文件路径名

  3. 的完整路径传递到另一个脚本

  4. 检查从脚本的返回code。如果不为零,日志无法处理的文件的名称。

我第一次尝试的外观(伪code)是这样的:

  ROOT_DIR =〜/工作/项目
CD $ ROOT_DIR
对于F中的`找到。 -name* .EXT`

    #need以砍掉从文件名开头的./',但我还没有制定出如何使用
    #cut但
    NEWNAME =`回声$ F |切-c 3
    文件名=$ ROOT_DIR / $ NEWNAME    RET code = $ ./some_other_script名    如果$沤code NE 0
       LOGERROR(无法处理文件:$文件名)
DONE

这是我在写一个bash脚本的第一次尝试,所以上面的片段是不可能运行。虽然希望的什么,我试图做的逻辑是再清楚不过了,有人可以显示如何连接点和转换伪code入一个工作脚本。

我的Ubuntu上运行


解决方案

 找到。 -name'* .EXT'\\(-exec ./some_other_script$ PWD/ {} \\; -print -o \\)

I want to write a bash script that (recursively) processes all files of a certain type.

I know I can get the matching file list by using find thusly:

find . -name "*.ext"

I want to use this in a script:

  1. recursively obatin list of files with a given extension
  2. obtain the full file pathname
  3. pass the full pathname to another script
  4. Check the return code from the script. If non zero, log the name of the file that could not be processed.

My first attempt looks (pseudocode) like this:

ROOT_DIR = ~/work/projects
cd $ROOT_DIR
for f in `find . -name "*.ext"`
do
    #need to lop off leading './' from filename, but I havent worked out how to use
    #cut yet
    newname = `echo $f | cut -c 3
    filename = "$ROOT_DIR/$newname"

    retcode = ./some_other_script $filename

    if $retcode ne 0
       logError("Failed to process file: $filename")
done

This is my first attempt at writing a bash script, so the snippet above is not likely to run. Hopefully though, the logic of what I'm trying to do is clear enough, and someone can show how to join the dots and convert the pseudocode above into a working script.

I am running on Ubuntu

解决方案

find . -name '*.ext' \( -exec ./some_other_script "$PWD"/{} \; -o -print \)

这篇关于庆典:处理(递归)通过目录中的所有文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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