【Shell】ファイルをゴミ箱へ移動する方法

ファイルを削除するときに、

rm コマンドを使うと間違って消してしまった時に復活できないのが困った。

なので、

いったんゴミ箱へ移動させることに。

ゴミ箱は、 ~/.Trash でディレクトリ指定できる。

trash.sh

dir="$(dirname $0)/*"
find $dir -maxdepth 0 -type f -iname '*.png' -o -iname '*.jpg' | while read file
do
    mv "${file}" ~/.Trash
done

しかし、「戻す」の機能は使えない。

重要追記:

AppleScript コードを使うことにした。これなら「戻す」までできる。

dir_path="$(dirname $0)/*"
find $dir_path -maxdepth 0 -type f -iname '*.png' -o -iname '*.jpg' | while read file
do

osascript <<EOS
tell application "Finder"
  delete (POSIX file "${file}") as alias
end tell
return
EOS

done

参考サイト