是否有一個 Linux 腳本/應用程式可以將檔案移動到特殊的「垃圾」位置,而不是刪除檔案?我希望用它來替代rm
(甚至可能是後者的別名;這有優點和缺點)。
我所說的「垃圾」是指一個特殊的資料夾。單一mv "$@" ~/.trash
是第一步,但理想情況下,這也應該處理垃圾同名的多個文件,而不覆蓋舊的垃圾文件,並允許恢復使用簡單的命令(一種“撤消”)將文件恢復到其原始位置。此外,如果垃圾在重新啟動時自動清空(或類似的機制來防止無限增長),那就太好了。
對此存在部分解決方案,但「恢復」操作尤其重要。是否有任何現有的解決方案不依賴圖形外殼的垃圾系統?
(順便說一句,關於這種方法是否合理(而不是使用頻繁備份和VCS)是否合理的討論一直在無休止地進行。雖然這些討論都有道理,但我相信我的要求仍然有其市場空間。 )
答案1
答案2
前面的答案提到了命令trash-cli
和rmtrash
。預設情況下,在 Ubuntu 18.04 上找不到這兩個命令,但命令gio
是。命令gio help trash
輸出:
Usage:
gio trash [OPTION…] [LOCATION...]
Move files or directories to the trash.
Options:
-f, --force Ignore nonexistent files, never prompt
--empty Empty the trash
我測試使用gio trash FILENAME
在命令列上,它的工作方式就像我在文件瀏覽器中選擇文件並單擊 DEL 按鈕一樣:文件被移到桌面的「垃圾箱」資料夾中。 (即使我沒有使用該-f
選項,該命令也不會提示確認。)
以這種方式刪除文件是可逆的,同時比為了安全而重新定義並且必須確認每個刪除更方便rm
,rm -i
如果您不小心確認了不應該刪除的文件,這仍然會讓您運氣不佳。
我加入alias tt='gio trash'
我的別名定義檔;tt
是「丟垃圾」的助記符。
2018年6月27日編輯新增:在伺服器電腦上,沒有相當於垃圾目錄的東西。我編寫了以下 Bash 腳本來完成這項工作;在桌上型電腦上,它使用gio trash
,在其他電腦上,將作為參數給出的檔案移動到它創建的垃圾目錄。該腳本經過測試可以正常工作;我自己也一直用它。腳本於 2024 年 4 月 12 日更新。
#!/bin/bash
# move_to_trash
#
# Teemu Leisti 2024-04-12
#
# USAGE:
#
# Move the file(s) given as argument(s) to the trash directory, if they are
# not already there.
#
# RATIONALE:
#
# The script is intended as a command-line equivalent of deleting a file or
# directory from a graphical file manager. On hosts that implement the
# FreeDesktop.org specification on trash directories (hereon called "the trash
# specification"; see
# https://specifications.freedesktop.org/trash-spec/trashspec-latest.html),
# that action moves the target file(s) to a built-in trash directory, and that
# is exactly what this script does.
#
# On other hosts, this script uses a custom trash directory (~/.Trash/). The
# analogy of moving a file to trash is not perfect, as the script does not
# offer the functionalities of restoring a trashed file to its original
# location or emptying the trash directory. Rather, it offers an alternative
# to the 'rm' command, thereby giving the user the peace of mind that they can
# still undo an unintended deletion before emptying the custom trash
# directory.
#
# IMPLEMENTATION:
#
# To determine whether it's running on a host that implements the trash
# specification, the script tests for the existence of (a) the gio command and
# (b) either directory $XDG_DATA_HOME/Trash/, or, if that environment variable
# hasn't bee set, of directory ~/.local/share/Trash/. If the test yields true,
# the script relies on calling 'gio trash'.
#
# On other hosts:
# - There is no built-in trash directory, so the script creates a custom
# directory ~/.Trash/, unless it already exists. (The script aborts if
# there is an existing non-directory ~/.Trash.)
# - The script appends a millisecond-resolution timestamp to all the files
# it moves to the custom trash directory, to both inform the user of the
# time of the trashing, and to avoid overwrites.
# - The user will have to perform an undo by commanding 'mv' on a file or
# directory moved to ~/.Trash/.
# - The user will have to empty the custom trash directory by commanding:
# rm -rf ~/.Trash/* ~/.Trash/.*
#
# The script will not choke on a nonexistent file. It outputs the final
# disposition of each filename argument: does not exist, was already in trash,
# or was moved to trash.
#
# COPYRIGHT WAIVER:
#
# The author dedicates this Bash script to the public domain by waiving all of
# their rights to the work worldwide under copyright law, including all
# related and neighboring rights, to the extent allowed by law. You can copy,
# modify, distribute, and perform the script, even for commercial purposes,
# all without asking for permission.
if [ -z "$XDG_DATA_HOME" ] ; then
xdg_trash_directory=$(realpath ~/.local/share/Trash/)
else
xdg_trash_directory=$(realpath $XDG_DATA_HOME/Trash/)
fi
gio_command_exists=0
if $(command -v gio > /dev/null 2>&1) ; then
gio_command_exists=1
fi
host_implements_trash_specification=0
if [[ -d "${xdg_trash_directory}" ]] && (( gio_command_exists == 1 )) ; then
# Executing on a host that implements the trash specification.
host_implements_trash_specification=1
trash_directory="${xdg_trash_directory}"
else
# Executing on other host, so attempt to use a custom trash directory.
trash_directory=$(realpath ~/.Trash)
if [[ -e "${trash_directory}" ]] ; then
# It exists.
if [[ ! -d "${trash_directory}" ]] ; then
# But is not a directory, so abort.
echo "Error: ${trash_directory} exists, but is not a directory."
exit 1
fi
else
# It does not exists, so create it.
mkdir "${trash_directory}"
echo "Created directory ${trash_directory}"
fi
fi
# Deal with all filenames (a concept that covers names of both files and
# directories) given as arguments.
for file in "$@" ; do
file_to_be_trashed=$(realpath -- "${file}")
file_basename=$(basename -- "${file_to_be_trashed}")
if [[ ! -e ${file_to_be_trashed} ]] ; then
echo "does not exist: ${file_to_be_trashed}"
elif [[ "${file_to_be_trashed}" == "${trash_directory}"* ]] ; then
echo "already in trash: ${file_to_be_trashed}"
else
# ${file_to_be_trashed} exists and is not yet in the trash directory,
# so move it there.
if (( host_implements_trash_specification == 1 )) ; then
gio trash "${file_to_be_trashed}"
else
# Move the file to the custom trash directory, with a new name that
# appends a millisecond-resolution timestamp to the original.
head="${trash_directory}/${file_basename}"_TRASHED_ON_
move_file_to="${head}$(date '+%Y-%m-%d_AT_%H-%M-%S.%3N')"
while [[ -e "${move_file_to}" ]] ; do
# Generate a new name with a new timestamp, as the previously
# generated one denoted an existing file or directory. It's very
# unlikely that this loop needs to be executed even once.
move_file_to="${head}$(date '+%Y-%m-%d_AT_%H-%M-%S.%3N')"
done
# There is no file or directory named ${move_file_to}, so
# we can use it as the move target.
/bin/mv "${file_to_be_trashed}" "${move_file_to}"
fi
echo "moved to trash: ${file_to_be_trashed}"
fi
done
答案3
垃圾-cli是一個 Linux 應用程序,可以在 Ubuntu 中使用 apt-get 或在 Fedora 中使用 yum 安裝。使用該指令trash listOfFiles
會將指定的內容移到垃圾箱中。
答案4
首先定義一個move_to_trash
函數:
move_to_trash () {
mv "$@" ~/.trash
}
然後別名rm
為:
alias rm='move_to_trash'
您始終可以rm
透過使用反斜線轉義來呼叫 old,如下所示:\rm
。
我不知道如何在重新啟動時清空垃圾目錄(根據您的系統,您可能需要查看腳本rc*
),但建立cron
定期清空目錄的任務也可能是值得的。