ファイル名を印刷するときにforループのスペースを無視しますか?

ファイル名を印刷するときにforループのスペースを無視しますか?

サイズが1kより大きく、拡張子がtxtのファイルをインポートしようとしていますが、コードは次のようになります。

files=$(find foldername -size +1k -name \*.txt -exec {} \;)

for item in $files
do 
echo $item
done

ところで、以下のように予期せぬ結果が出ました。助けてください! ! !

DEST/sample - Copy - Copy.txt: line 1: Hello: command not found
DEST/sample - Copy - Copy.txt: line 2: This: command not found
DEST/sample - Copy - Copy.txt: line 3: In: command not found
DEST/sample - Copy - Copy.txt: line 4: $'\r': command not found
DEST/sample - Copy - Copy.txt: line 5: User: command not found
DEST/sample - Copy - Copy.txt: line 6: -s: command not found
DEST/sample - Copy - Copy.txt: line 7: -d: command not found
DEST/sample - Copy - Copy.txt: line 8: -t: command not found
DEST/sample - Copy - Copy.txt: line 9: $'\r': command not found
DEST/sample - Copy - Copy.txt: line 10: $'\r': command not found
DEST/sample - Copy - Copy.txt: line 11: Hello: command not found
DEST/sample - Copy - Copy.txt: line 12: This: command not found
DEST/sample - Copy - Copy.txt: line 13: In: command not found
DEST/sample - Copy - Copy.txt: line 14: $'\r': command not found
DEST/sample - Copy - Copy.txt: line 15: User: command not found
DEST/sample - Copy - Copy.txt: line 16: -s: command not found
DEST/sample - Copy - Copy.txt: line 17: -d: command not found
DEST/sample - Copy - Copy.txt: line 18: -t: command not found
DEST/sample - Copy - Copy.txt: line 19: $'\r': command not found
DEST/sample - Copy - Copy.txt: line 20: Hello: command not found
DEST/sample - Copy - Copy.txt: line 21: This: command not found
DEST/sample - Copy - Copy.txt: line 22: In: command not found
DEST/sample - Copy - Copy.txt: line 23: $'\r': command not found
DEST/sample - Copy - Copy.txt: line 24: User: command not found
DEST/sample - Copy - Copy.txt: line 25: -s: command not found
DEST/sample - Copy - Copy.txt: line 26: -d: command not found
DEST/sample - Copy - Copy.txt: line 27: -t: command not found
DEST/sample - Copy.txt: line 1: Hello: command not found
DEST/sample - Copy.txt: line 2: This: command not found
DEST/sample - Copy.txt: line 3: In: command not found
DEST/sample - Copy.txt: line 4: $'\r': command not found
DEST/sample - Copy.txt: line 5: User: command not found
DEST/sample - Copy.txt: line 6: -s: command not found
DEST/sample - Copy.txt: line 7: -d: command not found
DEST/sample - Copy.txt: line 8: -t: command not found
DEST/sample - Copy.txt: line 9: $'\r': command not found
DEST/sample - Copy.txt: line 10: $'\r': command not found
DEST/sample - Copy.txt: line 11: Hello: command not found
DEST/sample - Copy.txt: line 12: This: command not found
DEST/sample - Copy.txt: line 13: In: command not found
DEST/sample - Copy.txt: line 14: $'\r': command not found
DEST/sample - Copy.txt: line 15: User: command not found
DEST/sample - Copy.txt: line 16: -s: command not found
DEST/sample - Copy.txt: line 17: -d: command not found
DEST/sample - Copy.txt: line 18: -t: command not found
DEST/sample - Copy.txt: line 19: $'\r': command not found
DEST/sample - Copy.txt: line 20: Hello: command not found
DEST/sample - Copy.txt: line 21: This: command not found
DEST/sample - Copy.txt: line 22: In: command not found
DEST/sample - Copy.txt: line 23: $'\r': command not found
DEST/sample - Copy.txt: line 24: User: command not found
DEST/sample - Copy.txt: line 25: -s: command not found
DEST/sample - Copy.txt: line 26: -d: command not found
DEST/sample - Copy.txt: line 27: -t: command not found

ベストアンサー1

明らかな問題:findコマンドは、-exec {}見つかったすべてのファイルを実行しようとしているものを実行しています(ほとんどのエラーメッセージのソース)。たとえば、次の意味があります-print

files=$(find foldername -size +1k -name \*.txt -print)

名前にスペースが含まれているファイルが見つかると、リストに問題が発生する可能性があります。ただし、ファイルが正しく見つかったら、この問題を確認できます。

これを行う方法はいくつかあります。スクリプトを少し変更した方法は次のとおりです。

find foldername -size +1k -name \*.txt -print | \
while IFS= read -r item
do 
echo "$item"
done

おすすめ記事