Iterate over a list of files with spaces

前端 未结 11 1629
-上瘾入骨i
-上瘾入骨i 2020-11-22 05:16

I want to iterate over a list of files. This list is the result of a find command, so I came up with:

getlist() {
  for f in $(find . -iname \"f         


        
11条回答
  •  没有蜡笔的小新
    2020-11-22 06:13

    find has an -exec argument that loops over the find results and executes an arbitrary command. For example:

    find . -iname "foo*" -exec echo "File found: {}" \;
    

    Here {} represents the found files, and wrapping it in "" allows for the resultant shell command to deal with spaces in the file name.

    In many cases you can replace that last \; (which starts a new command) with a \+, which will put multiple files in the one command (not necessarily all of them at once though, see man find for more details).

提交回复
热议问题