Using find - Deleting all files/directories (in Linux ) except any one

后端 未结 11 1107
野趣味
野趣味 2021-02-02 15:12

If we want to delete all files and directories we use, rm -rf *.

But what if i want all files and directories be deleted at a shot, except one particular fi

11条回答
  •  爱一瞬间的悲伤
    2021-02-02 15:58

    I don't know of such a program, but I have wanted it in the past for some times. The basic syntax would be:

    IFS='
    ' for f in $(except "*.c" "*.h" -- *); do
      printf '%s\n' "$f"
    done
    

    The program I have in mind has three modes:

    • exact matching (with the option -e)
    • glob matching (default, like shown in the above example)
    • regex matching (with the option -r)

    It takes the patterns to be excluded from the command line, followed by the separator --, followed by the file names. Alternatively, the file names might be read from stdin (if the option -s is given), each on a line.

    Such a program should not be hard to write, in either C or the Shell Command Language. And it makes a good excercise for learning the Unix basics. When you do it as a shell program, you have to watch for filenames containing whitespace and other special characters, of course.

提交回复
热议问题