How do I capture the output from the ls or find command to store all file names in an array?

前端 未结 4 2280
余生分开走
余生分开走 2020-12-30 20:12

Need to process files in current directory one at a time. I am looking for a way to take the output of ls or find and store the resulting value as

4条回答
  •  离开以前
    2020-12-30 20:35

    You actually don't need to use ls/find for files in current directory.

    Just use a for loop:

    for files in *; do 
        if [ -f "$files" ]; then
            # do something
        fi
    done
    

    And if you want to process hidden files too, you can set the relative option:

    shopt -s dotglob
    

    This last command works in bash only.

提交回复
热议问题