我想知道是否可以在终端中发出命令,所以我通常不会删除(rm)文件,而是将其移至垃圾箱(即Nautilus移至垃圾箱行为)。

如果有这样的命令,我也想知道它是什么。

评论

看看这个答案。

这里也是不错的资源:webupd8.org/2010/02/make-rm-move-files-to-trash-instead-of.html

#1 楼

您可以使用Ubuntu中默认安装的gvfs-trash软件包中的gvfs-bin命令。

将文件移至垃圾桶:

gvfs-trash filename


查看内容垃圾桶:

gvfs-ls trash://


清空垃圾桶:

gvfs-trash --empty


评论


也请访问我的gvfs问题。

–潘迪亚
2014-09-17 14:47



对我来说,这是最简单的答案。谢谢。

– Teody C. Seguin
17年8月2日在10:55

根据man gvfs-trash的建议,不赞成使用gio垃圾,请参阅man gio。

– pbhj
'18 Sep 1'在21:14

#2 楼

安装垃圾桶-cli – sudo apt-get install trash-cli

将垃圾桶中的文件放入:trash file1 file2

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

空垃圾桶:

评论


该(与Ubuntu相关的)工具指出了垃圾分类。很有趣,但是不确定采用的广泛性...

–弗兰克·诺克(Frank Nocke)
17年8月15日在17:59

安装后,我运行命令并得到错误: ImportError中的文件“ / usr / bin / trash-list”,第4行:没有名为“ trashcli”的模块

–丹尼尔(Daniel)
18年8月1日在8:48



#3 楼

截至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,特别是
是推荐的方法。

评论


您是否可以链接不推荐使用的gvfs-trash的来源以及gio是什么?

–梅勒比乌斯
17年12月11日在9:04

很遗憾,我无法提供链接,但这就是我尝试在Kubuntu 17.10上使用gvfs-trash的原因:pastebin.com/HA4a1pbs

– Eugen Tverdokhleb
18年1月10日在19:56



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

–梅勒比乌斯
18年1月10日在20:50

该工具还有许多其他不错的功能。我喜欢info命令;似乎很有用。

–拉菲·哈特查杜安(Raffi Khatchadourian)
18年8月2日在18:35

@Melebius在Bionic Beaver(18.04)上,gvfs-trash(1)说它已被弃用。在Focal Fossa(20.04)上,甚至包装说明也已弃用。

–于富兰
20 Feb 25'2:12



#4 楼

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

因此,要丢弃some_file(或文件夹),请使用潜水用途

gio trash some_file


清空垃圾

gio list trash://


评论


gio list垃圾:// gio:垃圾://:不支持操作

–秦寺
20-09-28在6:08



它缺少一个斜杠,应该是gio list trash:///。我试图编辑答案,但不允许我仅编辑一个字符。

–user1754322
20 Dec 19'在10:08

#5 楼

我喜欢更好的简单方法。我在主目录中键入以下内容以创建文件夹.Tr

mkdir ~/.Tr


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


这是一种有效且简单的方法,可用于访问您认为不需要的文件。 (在我的情况下)这还有一个好处,就是不会弄乱系统文件夹,因为我的Ubuntu知识水平很低,而且我担心弄乱系统内容时可能搞砸了什么。如果您的水平也很低,请注意“。”目录名称中的使其成为隐藏目录。

评论


使用此方法的缺点是,移动具有相同名称的文件将覆盖它们。当您使用不会发生的gnome垃圾时,它将保留所有垃圾

–user1754322
20 Dec 19'在10:13

#6 楼

先前的答案提到了gio垃圾桶命令,就目前而言还不错。但是,在服务器计算机上,没有等效的废纸directory目录。我已经写了一个Bash脚本来完成这项工作。在(Ubuntu)台式机上,它使用gio垃圾桶。 (我已经将alias tt='move-to-trash'添加到我的别名定义文件中; tt是“废纸“”的助记符。)我自己一直在使用它。脚本已于2020-08-10更新。
 #!/bin/bash
#
# move-to-trash
#
# Teemu Leisti 2020-08-10
#
# This script moves the files given as arguments to the trash directory, if they
# are not already there. It works both on (Gnome) desktop and server hosts. (The
# gio command only exists for Gnome.)
#
# 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 functionality of restoring a trashed file to its
# original location, nor that of emptying the trash directory; rather, it offers
# an alternative to the 'rm' command, giving the user the peace of mind that
# they can still undo an unintended deletion before emptying the trash
# directory.
#
# To determine whether it's running on a desktop host, the script tests for the
# existence of the gio command and of directory ~/.local/share/Trash. In case
# both exist, the script relies on the 'gio trash' command. Otherwise, it treats
# the host as a server.
#
# There is no built-in trash directory on server hosts, so the script creates
# directory ~/.Trash/, unless it already exists.
#
# The script appends a millisecond-resolution time stamp to all the files it
# moves to the trash directory, both to inform the user of the time of the
# deletion, and to avoid overwrites when moving a file to trash.
#
# 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 trash.


gio_command_exists=0
command -v gio > /dev/null 2>&1
if (( $? == 0 )) ; then
    gio_command_exists=1
fi

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

is_desktop=0

if [[ -d ~/.local/share/Trash ]] && (( gio_command_exists == 1 )) ; 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
            # The name of the moved file shall be the original name plus a
            # millisecond-resolution timestamp.
            beginning="$trash_dir_abspath/$file_basename"_DELETED_ON_
            move_to_abspath="$beginning$(date '+%Y-%m-%d_AT_%H-%M-%S.%3N')"
            while [[ -e "$move_to_abspath" ]] ; do
                # Generate a new name with a new timestamp, as the previously
                # generated one denoted an existing file.
                move_to_abspath="$beginning$(date '+%Y-%m-%d_AT_%H-%M-%S.%3N')"
            done
            # We're now almost certain that the file denoted by name
            # $move_to_abspath does not exist. For that to be the case, an
            # extremely unlikely race condition would have had to take place:
            # some other process would have had to create a file with the name
            # $move_to_abspath after the execution of the existence test above.
            # However, to make absolute sure that moving the file to the trash
            # directory will always be successful, we shall give the '-f'
            # (force) flag to the 'mv' command.
            /bin/mv -f "$file_abspath" "$move_to_abspath"
        fi
        echo "moved to trash:   $file_abspath"
    fi
done
 


#7 楼

这是一个基于开源基于nodejs的版本(如果您想了解幕后发生的事情,或者需要在项目中进行此操作),它还具有命令行支持(如果您满意的话,如果它能正常工作的话)。

> trash pictures/beach.jpg


#8 楼

在KDE 4.14.8中,我使用以下命令将文件移至垃圾箱(就像在海豚中将其删除一样): > EDIT:对于KDE 5.18.5,命令为kioclient5,语法相同。