Polecenie przeniesienia pliku do Kosza za pośrednictwem terminala


117

Chciałbym wiedzieć, czy istnieje polecenie, które mogę wydać w terminalu, aby klasycznie nie usuwać ( rm) pliku, ale zamiast tego przenieść go do kosza (tj. Zachowanie Nautilus Move to Trash).

W przypadku takiego polecenia chciałbym również wiedzieć, co to jest.


2
Spójrz na tę odpowiedź .
Peachy

Odpowiedzi:


105

Możesz użyć gvfs-trashpolecenia z pakietu, gvfs-binktóry jest domyślnie instalowany w Ubuntu.

Przenieś plik do kosza:

gvfs-trash filename

Zobacz zawartość kosza:

gvfs-ls trash://

Wynieś śmieci:

gvfs-trash --empty

Odwiedź także moje pytanie gvfs .
Pandya

To jest dla mnie najprostsza odpowiedź, która działa. Dziękuję Ci.
Teody C. Seguin

9
Zgodnie z man gvfs-trashnim jest przestarzałe na korzyść gio trash, patrz man gio.
pbhj

67

Zainstaluj trash-cliZainstaluj trash-cli -sudo apt-get install trash-cli

Umieść pliki w koszu za pomocą: trash file1 file2

Wyświetl listę plików w koszu: trash-list

Opróżnij kosz za pomocą: trash-empty


1
To (związane z Ubuntu) narzędzie wskazuje specyfikację kosza . Dość interesujące, choć nie jestem pewien, jak szeroko przyjęte, ale ...
Frank Nocke

Po instalacji uruchamiam polecenie i pojawia się błąd: File "/usr/bin/trash-list", line 4, in <module> ImportError: No module named 'trashcli'
Daniel

25

Od 2017 r. gvfs-trashWydaje się przestarzały.

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

Powinieneś użyć giokonkretnie

gio trash

jest zalecanym sposobem.


2
Czy możesz połączyć źródło gvfs-trashprzestarzałych i co gioto jest?
Melebius

1
Niestety nie mogę podać linku, ale próbuję użyć gvfs-trash na Kubuntu 17.10: pastebin.com/HA4a1pbs
Eugen Tverdokhleb

1
Możesz wkleić tutaj przykład w swojej odpowiedzi, wystarczyłoby mi to wraz z numerem wersji systemu. Używam 16.04 LTS i gvfs-trashjest to jedyna opcja tutaj.
Melebius

To narzędzie ma wiele innych fajnych funkcji. Podoba mi się infopolecenie; wydaje się przydatne.
Raffi Khatchadourian

4

Aktualizuję @Radu Rădeanuodpowiedź. Ponieważ Ubuntu mówi mi, żebym giozamiast tego używał ...

Tak więc do kosza some_file(lub folderu) użyj

gio trash some_file

Do nurkowania w śmietniku użyj

gio list trash://

Aby opróżnić kosz

gio trash --empty

3

Najbardziej podoba mi się ten niski poziom technologiczny. Utworzyłem folder .Trw moim katalogu domowym, wpisując:

mkdir ~/.Tr

i zamiast używać rmdo usuwania plików, przenoszę te pliki do ~/.Trkatalogu, wpisując:

mv fileName ~/.Tr

Jest to skuteczny i prosty sposób na utrzymanie dostępu do plików, których uważasz, że nie chcesz, z dodatkową korzyścią w moim przypadku, gdy nie bałagan w folderach systemu, ponieważ mój poziom wiedzy na temat Ubuntu jest dość niski i martwię się o to, czym mógłbym być spieprzyć, kiedy zadzieram z rzeczami systemowymi. Jeśli masz niski poziom, pamiętaj, że „.” w nazwie katalogu powoduje, że jest to ukryty katalog.


3

Poprzednia odpowiedź wspomina o poleceniu gio trash, które jest w porządku. Jednak na serwerach nie ma odpowiednika katalogu na śmieci. Napisałem skrypt Bash, który wykonuje zadanie; na komputerach (Ubuntu) używa gio trash. (Dodałem alias tt='move-to-trash'do pliku definicji aliasów; ttjest to mnemonik wyrażenia „do kosza”).

#!/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

W KDE 4.14.8 użyłem następującego polecenia, aby przenieść pliki do kosza (tak jakby zostały usunięte w Dolphin):

kioclient move path_to_file_or_directory_to_be_removed trash:/

Dodatek: Dowiedziałem się o poleceniu za pomocą

    ktrash --help
...
    Note: to move files to the trash, do not use ktrash, but "kioclient move 'url' trash:/"
Korzystając z naszej strony potwierdzasz, że przeczytałeś(-aś) i rozumiesz nasze zasady używania plików cookie i zasady ochrony prywatności.
Licensed under cc by-sa 3.0 with attribution required.