How do I apply a shell command to many files in nested (and poorly escaped) subdirectories?

后端 未结 6 1618
别跟我提以往
别跟我提以往 2020-12-06 03:31

I\'m trying to do something like the following:

for file in `find . *.foo`
do
somecommand $file
done

But the command isn\'t working because

6条回答
  •  再見小時候
    2020-12-06 03:37

    I had to do something similar some time ago, renaming files to allow them to live in Win32 environments:

    #!/bin/bash
    IFS=$'\n'
    function RecurseDirs
    {
    for f in "$@"
    do
      newf=echo "${f}" | sed -e 's/[\\/:\*\?#"\|<>]/_/g'
      if [ ${newf} != ${f} ]; then
        echo "${f}" "${newf}"
        mv "${f}" "${newf}"
        f="${newf}"
      fi
      if [[ -d "${f}" ]]; then
        cd "${f}"
        RecurseDirs $(ls -1 ".")
      fi
    done
    cd ..
    }
    RecurseDirs .
    

    This is probably a little simplistic, doesn't avoid name collisions, and I'm sure it could be done better -- but this does remove the need to use basename on the find results (in my case) before performing my sed replacement.

    I might ask, what are you doing to the found files, exactly?

提交回复
热议问题