rmを使用して単一のチェック(rm -i)で複数のファイルを削除する方法は?

rmを使用して単一のチェック(rm -i)で複数のファイルを削除する方法は?

エイリアスがあり、rm='rm -irv'複数のファイルやディレクトリを削除したいのですが、確認メッセージは1つだけ必要です。rm: descend into directory 'myfolder'?

すべてのディレクトリをチェックするのは大丈夫ですが、すべてのディレクトリのすべてのファイルをチェックするのは気に入らません。rm *あるいは、zsh機能はうまく機能しますが、時にはファイルまたは個々のファイルをrm something/*削除しますが、それでも少なくとも1つの確認が必要です。rm *.txtrm document.txt

このソリューション 私が探しているものと非常に近いですが、すべてのケースで動作するわけではありません。 「myfolder」ディレクトリに100個のファイルが含まれていると仮定すると、ファイルが次のように表示されることを願っています。

~ > ls -F
myfolder/    empty.txt    file.txt    main.c

~ > rm *
zsh: sure you want to delete all 4 files in /home/user [yn]? n

~ > rm myfolder
rm: descend into directory 'myfolder'? y
removed 'file1.txt'
removed 'file2.txt'
...
removed 'file100.txt'
removed directory 'myfolder'

~ > rm main.c
rm: remove regular file 'main.c'? y
removed 'main.c'

~> rm *.txt
rm: remove all '*.txt' files? y
removed 'empty.txt'
removed 'file.txt'

ベストアンサー1

質問に記載されている正確なプロンプトと出力を取得することは事実上不可能ですが(または多くの作業が必要です)、以下はすべての実際の目的を網羅する必要があります。

# Disable the default prompt that says
# 'zsh: sure you want to delete all 4 files in /home/user [yn]?'
setopt rmstarsilent

# For portability, use the `zf_rm` builtin instead of any external `rm` command.
zmodload -Fa zsh/files b:zf_rm

rm() {
  # For portability, reset all options (in this function only).
  emulate -L zsh

  # Divide the files into dirs and other files.
  # $^ treats the array as a brace expansion.
  # (N) eliminates non-existing matches.
  # (-/) matches dirs, incl. symlinks pointing to dirs.
  # (-^/) matches everything else.
  # (T) appends file type markers to the file names.
  local -a dirs=( $^@(TN-/) ) files=( $^@(TN-^/) )

  # Tell the user how many dirs and files would be deleted.
  print "Sure you want to delete these $#dirs dirs and $#files files in $PWD?"

  # List the files in columns à la `ls`, dirs first.
  print -c - $dirs $files

  # Prompt the user to confirm.
  # If `y`, delete the files.
  #   -f skips any confirmation.
  #   -r recurses into directories.
  #   -s makes sure we don't accidentally the whole thing.
  # If this succeeds, print a confirmation.
  read -q "?[yn] " &&
      zf_rm -frs - $@ && 
      print -l '' "$#dirs dirs and $#files files deleted from $PWD."
}

詳細については、zf_rm次を参照してください。http://zsh.sourceforge.net/Doc/Release/Zsh-Modules.html#The-zsh_002files-Module

glob修飾子の詳細については、(TN-^/)こちらをご覧ください。http://zsh.sourceforge.net/Doc/Release/Expansion.html#Glob-Qualifiers

おすすめ記事