鱼Shell:删除除

Fish Shell: Delete All Except

使用Fish,如何删除目录中除某些文件(或目录)之外的内容。类似于 bash 中的 rm !(file1|file2),但更难闻。

fish 中没有这样的功能 - 这是问题 #1444

你可以这样做

rm (string match -rv '^file1$|^file2$' -- *)

请注意,这将在文件名中包含换行符时失败。

或者你可以做更丑陋的事情:

set -l files *
for file in file1 file2
    if set -l index (contains -i -- $file $files)
        set -e files[$index]
    end
end
rm $files

无论文件名包含什么内容都应该有效。

或者,如该期所述,您可以使用查找,例如

find . -mindepth 1 -maxdepth 1 -type f -a ! \( -name 'file1' -o -name 'file2' \)