Bash: loop through files that DO NOT match extension

后端 未结 6 360
眼角桃花
眼角桃花 2021-01-13 17:32

I\'m writing a bash script that needs to loop files inside a directory that do not match a specific extension. So far, I\'ve found that the following code loops all files th

6条回答
  •  一生所求
    2021-01-13 18:00

    If you are ok for a GNU solution, give a try to this:

    for f in $(find . -maxdepth 1 -type f \! -name \*.txt) ; do
      printf "%s\n" "${f}"
    done
    

    This is going to break if special chars are contained in the filenames, such as (space).

    For something safe, still GNU, try:

    find . -maxdepth 1 -type f \! -name \*.txt -printf "%p\0" | xargs -0 sh -c '
        for f ; do
          printf "%s\n" "${f}"
        done' arg0
    

提交回复
热议问题