通过终端将文件移至垃圾箱的命令


Answers:



67

安装trash-cli安装垃圾桶clisudo apt-get install trash-cli

使用以下命令将文件放入垃圾箱: trash file1 file2

列出垃圾桶中的文件: trash-list

空垃圾桶: trash-empty


1
该(与Ubuntu相关的工具)指出了垃圾分类。很有意思,但是不确定采用的广泛吗……
Frank Nocke

安装后,我运行命令并收到错误消息: File "/usr/bin/trash-list", line 4, in <module> ImportError: No module named 'trashcli'
Daniel

25

截至2017年,gvfs-trash似乎已弃用。

$ touch test
$ gvfs-trash test
This tool has been deprecated, use 'gio trash' instead.
See 'gio help trash' for more info.

您应该使用gio,特别是

gio trash

是推荐的方法。


2
您可以链接一个gvfs-trash不赞成使用的资源gio吗?
Melebius

1
不幸的是,我无法提供链接,但这就是我试图在Kubuntu 17.10上使用gvfs-trash的原因:pastebin.com/HA4a1pbs
Eugen Tverdokhleb,

1
您可以将示例粘贴在答案中,这对我和系统版本号就足够了。我正在使用16.04 LTS,gvfs-trash是这里的唯一选择。
Melebius

该工具还有许多其他不错的功能。我喜欢这个info命令;似乎很有用。
拉菲·哈查杜安

4

更新@Radu Rădeanu答案。由于Ubuntu告诉我gio改用...

所以,要垃圾some_file(或文件夹)使用

gio trash some_file

去垃圾箱潜水使用

gio list trash://

清空垃圾

gio trash --empty

3

我最喜欢低技术的方式。我.Tr通过键入以下内容在主目录中创建了一个文件夹:

mkdir ~/.Tr

而不是使用rm删除文件,而是~/.Tr通过输入以下内容将这些文件移动到目录中:

mv fileName ~/.Tr

这是一种有效而简单的方式,可以在不破坏系统文件夹的情况下增加对您认为不想要的文件的访问权限,因为我的Ubuntu知识水平很低,因此我担心自己可能会成为什么样的人当我弄乱系统内容时搞砸了。如果您的水平也很低,请注意“。” 在目录名中将其设为隐藏目录。


3

先前的答案提到了command gio trash,就目前而言,它很好。但是,在服务器计算机上,没有等效的废纸directory目录。我已经写了一个Bash脚本来完成这项工作。在(Ubuntu)台式机上,它使用gio trash。(我已将其添加alias tt='move-to-trash'到别名定义文件中;它tt是“废纸“”的助记符。)

#!/bin/bash
# move-to-trash

# Teemu Leisti 2018-07-08

# This script moves the files given as arguments to the trash directory, if they
# are not already there. It works both on (Ubuntu) desktop and server hosts.
#
# The script is intended as a command-line equivalent of deleting a file from a
# graphical file manager, which, in the usual case, moves the deleted file(s) to
# a built-in trash directory. On server hosts, the analogy is not perfect, as
# the script does not offer the functionalities of restoring a trashed file to
# its original location nor of emptying the trash directory; rather, it is an
# alternative to the 'rm' command that offers the user the peace of mind that
# they can still undo an unintended deletion before they empty the trash
# directory.
#
# To determine whether it's running on a desktop host, the script tests for the
# existence of directory ~/.local/share/Trash. In case it is, the script relies
# on the 'gio trash' command.
#
# When not running on a desktop host, there is no built-in trash directory, so
# the first invocation of the script creates one: ~/.Trash/. It will not
# overwrite an existing file in that directory; instead, in case a file given as
# an argument already exists in the custom trash directory, the script first
# appends a timestamp to the filename, with millisecond resolution, such that no
# existing file will be overwritten.
#
# The script will not choke on a nonexistent file. It outputs the final
# disposition of each argument: does not exist, was already in trash, or was
# moved to the trash.


# Exit on using an uninitialized variable, and on a command returning an error.
# (The latter setting necessitates appending " || true" to those arithmetic
# calculations that can result in a value of 0, lest bash interpret the result
# as signalling an error.)
set -eu

is_desktop=0

if [[ -d ~/.local/share/Trash ]] ; then
    is_desktop=1
    trash_dir_abspath=$(realpath ~/.local/share/Trash)
else
    trash_dir_abspath=$(realpath ~/.Trash)
    if [[ -e $trash_dir_abspath ]] ; then
        if [[ ! -d $trash_dir_abspath ]] ; then
            echo "The file $trash_dir_abspath exists, but is not a directory. Exiting."
            exit 1
        fi
    else
        mkdir $trash_dir_abspath
        echo "Created directory $trash_dir_abspath"
    fi
fi

for file in "$@" ; do
    file_abspath=$(realpath -- "$file")
    file_basename=$( basename -- "$file_abspath" )
    if [[ ! -e $file_abspath ]] ; then
        echo "does not exist:   $file_abspath"
    elif [[ "$file_abspath" == "$trash_dir_abspath"* ]] ; then
        echo "already in trash: $file_abspath"
    else
        if (( is_desktop == 1 )) ; then
            gio trash "$file_abspath" || true
        else
            move_to_abspath="$trash_dir_abspath/$file_basename"
            while [[ -e "$move_to_abspath" ]] ; do
                move_to_abspath="$trash_dir_abspath/$file_basename-"$(date '+%Y-%m-%d-at-%H:%M:%S.%3N')
            done
            # While we're reasonably sure that the file at $move_to_abspath does not exist, we shall
            # use the '-f' (force) flag in the 'mv' command anyway, to be sure that moving the file
            # to the trash directory is successful even in the extremely unlikely case that due to a
            # run condition, some other thread has created the file $move_to_abspath after the
            # execution of the while test above.
            /bin/mv -f "$file_abspath" "$move_to_abspath"
        fi
        echo "moved to trash:   $file_abspath"
    fi
done


0

在KDE 4.14.8中,我使用以下命令将文件移至垃圾箱(就像在Dolphin中将其删除一样):

kioclient move path_to_file_or_directory_to_be_removed trash:/

附录:我发现与

    ktrash --help
...
    Note: to move files to the trash, do not use ktrash, but "kioclient move 'url' trash:/"
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.