Skip to content

Instantly share code, notes, and snippets.

@MarshalW
Last active July 1, 2022 06:30
Show Gist options
  • Save MarshalW/cf3a015bcee0a59b73c89fb277b81d79 to your computer and use it in GitHub Desktop.
Save MarshalW/cf3a015bcee0a59b73c89fb277b81d79 to your computer and use it in GitHub Desktop.
macos zsh 删除命令,指定不删除的个别文件和目录

macos zsh 删除命令,指定不删除的个别文件和目录

❯ setopt extended_glob
❯ tree . -a
.
├── .config
├── b.txt
└── config
    └── default.yml

1 directory, 3 files

❯ rm -r ^(b.txt|config)

❯ tree . -a
.
├── b.txt
└── config
    └── default.yml

将删除当前目录下所有文件和目录,除了 b.txtconfig 目录。

其中 setopt extended_glob 等同于 bash 中的 shopt -s extglob

另外,如果需要保留 dotfile,那么需要:

rm -r ^(b.txt|.config)(D)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment