Bash: processing (recursively) through all files in a directory

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

I know that I can get the corresponding list of files using find:

to find. -name "* .ext"

I want to use this in a script:

  • recatin obatin list of files with a given extension
  • get the full path to the file
  • pass the full path to another script
  • Check the return code from the script. If there is no zero, write down the name of the file that cannot be processed.

My first attempt looks (pseudo-code) as follows:

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

bash script, . , , , , - , script.

Ubuntu

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

| while read , :

find . -name '*.ext' | while IFS=$'\n' read -r FILE; do
  process "$(readlink -f "$FILE")" || echo "error processing: $FILE"
done
+2

Source: https://habr.com/ru/post/1779617/


All Articles