Looping through directories in Bash

后端 未结 3 855
名媛妹妹
名媛妹妹 2020-12-24 08:26

I require the script to cd to a directory, then delete all but a few files in sub-directories—but leave the folders alone. Would it help to use switch

3条回答
  •  旧巷少年郎
    2020-12-24 09:25

    You should use find :

    for i in $(find . -type d)
    do
        do_stuff "$i"
    done
    

    If you really have a lot of directories, you can pipe the output of find into a while read loop, but it makes coding harder as the loop is in another process.

    About spaces, be sure to quote the variable containing the directory name. That should allow you to handle directories with spaces in their names fine.

提交回复
热议问题