猫がキーボードの上を歩くことがよくあります.いつの間にかCapsLockとかNumLockとかが掛かってしまったりします.
その程度ならいいのですが,先日ファイラーが開いた状態でDel key を押されファイルが消えてしまいました.外付HDD でゴミ箱も設定していなかったのでゴミ箱からの復活もできません.
外付けHDDのゴミ箱を有効にしました.
猫がキーボードの上を歩くことがよくあります.いつの間にかCapsLockとかNumLockとかが掛かってしまったりします.
その程度ならいいのですが,先日ファイラーが開いた状態でDel key を押されファイルが消えてしまいました.外付HDD でゴミ箱も設定していなかったのでゴミ箱からの復活もできません.
外付けHDDのゴミ箱を有効にしました.
rm コマンドはとても強力です.バックアップもなく誤って削除したファイルは復旧できない可能性が高いです.
そこで rm の alias に rm -i を指定して確認メッセージを出すなどしている環境もありますが,いつも使っていると手癖で即削除してしまったりして確実ではありません.GUI環境のファイラーなどではゴミ箱機能が使えて削除してもゴミ箱を空にするまで復元が可能です.
これをcuiでも利用できる trash-cli というツールがあります.デスクトップと共通(freedesktop.org trashcanと共通)のゴミ箱が使えて便利です.
削除してしまったファイルを復元したい場合なるべく早く削除したファイルシステムを umount or mount -o ro してファイル復元ツールを試してみるともしかしたら復元できるかもしれません.Debian の場合 forensics-all/forensics-extra pkg 等を覗いてみると様々なツールが見つかると思います.(例えばext3/ext4ファイルシステムでの復元ツールのextundeleteや汎用のscalpel等)
$ sudo apt install trash-cli $ dpkg -L trash-cli|grep bin/ /usr/bin/restore-trash /usr/bin/trash /usr/bin/trash-empty /usr/bin/trash-list /usr/bin/trash-put /usr/bin/trash-rm
$ touch hoge #ファイル作成 $ trash hoge #ファイルを捨てる $ mkdir -p hoge/fuga/piyo #ディレクトリ作成 $ trash hoge #ディレクトリを捨てる
$ trash-list | grep hoge 2018-09-11 05:36:19 /home/matoken/tmp/hoge 2018-09-12 08:57:31 /home/matoken/tmp/hoge 2018-09-12 08:37:09 /home/matoken/tmp/hoge 2018-09-12 08:57:35 /home/matoken/tmp/hoge 2018-09-12 08:36:56 /home/matoken/tmp/hoge 2018-09-11 05:36:34 /home/matoken/tmp/hoge
$ trash-empty $ trash-list
$ restore-trash 0 2018-09-14 05:15:11 /home/matoken/tmp/piyo 1 2018-09-14 05:13:22 /home/matoken/tmp/hoge 2 2018-09-14 05:15:11 /home/matoken/tmp/fuga What file to restore [0..2]: 0 #復元したいファイルの番号を指定
$ alias rm='trash' $ rm hoge $ rm -rf dir
という感じで簡単に使えます.sudo を使うことも可能です.ゴミ箱の実体は環境によりますが,手元の環境では ~/.local/share/Trash/ 以下でした.このあたりは以下のページが参考になると思います.
ゴミ箱を空にしないとどんどんファイルは溜まっていきます.autotrash を使って掃除するのがおすすめです.
<追記>
Google+のコメントでmasakaさんに教えてもらったのですが,trash-empty 日付 と日付を指定することで日付以上の日数が経過したファイルが削除できるようです.日付指定だけならautotrash を導入しなくてもいいですね.
Masakazu Takahashi
ちなみにtrash-cliでも`trash-empty 30`とか実行すると、削除して30日超のファイルをゴミ箱から消してくれます。
ARGUMENTS
To remove all trashed files, use 'emtpy-trash'.
To remove files that have been in the trash more than a given number of
days, use 'trash-empty x', 'x' representing the number of days.</追記>
$ dpkg-query -W trash-cli trash-cli 0.12.9.14-2.1 $ lsb_release -d Description: Debian GNU/Linux unstable (sid) $ uname -m x86_64
#trash-cli は現在 0.17.1.14 で 0.12.9.14 のリリース日は 15 Sep 2012 となっている.
ゴミ箱は便利ですがどんどん溜まっていくので定期的に捨てないといけません.気が向いたときにゴミ箱を空にするのでもいいのですがそれだと1月前にゴミ箱に入れて確実にゴミであろうファイルもさっきゴミ箱に入れたもしかしたら誤って削除したファイルも一緒に消すことになってしまい心配です.
autotrashを使うとゴミ箱に入れた日数やファイル名,空き容量が何GBより少なくなったなどの様々な条件によりゴミ箱内のファイルを削除することができます.crontabに登録しておくと自動化もできてお薦めです.
※以下はmanのexampleで日本語部分は間違っているかもしれません –dry-run と –verbose オプションを付けて動作確認をした上で使うようにしてください.
autotrash -d 30
Purge any file that has been in the trash for more then 30 days.autotrash --max-free 1024 -d 30
Only purge files from the trash if there is less than 1GB of space left on the trash filesystem. If so, only trash files that are older than 30 days.autotrash --min-free 2048
Purge files from trash, oldest first, till there is at least 2GB of space on the trash filesystem. There is no restriction on how old trashed files are.autotrash --min-free 2048 -D '.*\.bak' -D '.*\.avi'
Purge files from trash till there is at least 2GB of space on the trash filesystem. If we need to remove files, make sure we remove *.bak files first, then
all *.avi files and after that the oldest to the newest. There is no restriction on how old trashed files can get. Please note that '.*\.bak' and '.*\.avi'
are regular expressions and not glob patterns. Given that they are regular expressions, using -D '.*\.(png|gif|jpg|jpeg)' will match images with any of the
given extensions.autotrash --max-free 4000 --min-free 2048 -d 30
Start reading the trash if there is less than 4000MB of free space, then start keeping an eye on. At that point, remove files older than 30 days and if
there is less than 2GB of free space after that remove even newer files.私は単純にゴミ箱に入れて33日経ったファイルを削除するようにしています.
この例では毎日02:08に優先度を下げて実行しています.vixie-cronの場合 @daily でもいいし, @reboot でシステム起動時に処理とかでもいいかもしれません.詳細は man 5 crontab を参照してください.
$ crontab -l|grep autotrash 8 2 * * * nice -n 19 ionice -c 3 autotrash -d 33
ただ,これはゴミ箱を使っているユーザ毎に設定しないといけないのがちょっと面倒です.一人で使っているシステムならいいのですが,利用者が1000人だと /etc/crontab に1000行書くなどしないといけません.
autotrash 0.2.1 では -t オプションで全ユーザのチェックが出来るようです.(手元の環境では 0.1.5 なので未確認)
You can also make autotrash process all user trash directories (not just in your home directory) by adding this crontab entry: @daily /usr/bin/autotrash -td 30
$ dpkg-query -W autotrash autotrash 0.1.5-1.1 $ lsb_release -d Description: Debian GNU/Linux unstable (sid) $ uname -m x86_64
#sidだけどupstreamに追従してないですね><