はじめに

コマンド上では、rmでファイル削除を行うが、その場合にはファイルの復元操作などは出来ない。
GUIでの操作の削除はゴミ箱を経由するが、trashコマンドでCLI上でもGUIと同じようにゴミ箱を経由してくれるようになる。
今回は、trashコマンドをインストールして使ってみる。

https://formulae.brew.sh/formula/trash

環境

1
2
Mac OS Sonoma 14.4
Homebrew 4.2.12

trashのインストール

1
brew install trash
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
brew install trash
==> Downloading https://formulae.brew.sh/api/formula.jws.json

==> Downloading https://formulae.brew.sh/api/cask.jws.json

==> Downloading https://ghcr.io/v2/homebrew/core/trash/manifests/0.9.2-1
################################################################################################################################################# 100.0%
==> Fetching trash
==> Downloading https://ghcr.io/v2/homebrew/core/trash/blobs/sha256:c26e06202022f708790f22f4477b65e3337d611c42e9a814ada1526bda03d923
################################################################################################################################################# 100.0%
==> Pouring trash--0.9.2.sonoma.bottle.1.tar.gz
🍺  /usr/local/Cellar/trash/0.9.2: 5 files, 61.0KB
==> Running `brew cleanup trash`...
Disable this behaviour by setting HOMEBREW_NO_INSTALL_CLEANUP.
Hide these hints with HOMEBREW_NO_ENV_HINTS (see `man brew`).

trashを使う

適当なファイルを作成する

1
echo "Hello,World" >> hello.txt
1
2
3
~$ echo "Hello,World" >> hello.txt
~$ cat hello.txt 
Hello,World

trashコマンドでゴミ箱へ移動する

1
trash hello.txt

ゴミ箱を見てみる。

1
trash -l
1
2
trash -l
/Users/anonymous/.Trash/hello.txt

存在してた。

復元をしてみる

復元のオプションはないようなので、ゴミ箱から移動させる。

1
mv /Users/anonymous/.Trash/hello.txt hello.txt

ゴミ箱を空にする

1
trash -e
1
2
3
4
5
trash -e
There are currently 15 items in the trash.
Are you sure you want to permanently delete these items?
(y = permanently empty the trash, l = list items in trash, n = don't empty)
[y/l/N]: y

削除できたかを確認する。

1
trash -l

何も表示されなければOK

おわりに

CLI上では、rmを使うが後で復元するかも…?みたいな作業の際に今回のコマンドは使えるのかもしれない。
バックアップを取れって話ではあるのだが…。