위의 답변은 명령 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-06-27 편집에 추가됨 : 서버 컴퓨터에는 휴지통 디렉토리에 해당하는 것이 없습니다. 작업을 수행하는 다음 Bash 스크립트를 작성했습니다. 데스크탑 시스템에서는을 사용 gio trash
하고 다른 시스템에서는 매개 변수로 지정된 파일을 작성된 휴지통 디렉토리로 이동합니다. 2019-09-05에 스크립트가 업데이트되었습니다.
#!/bin/bash
#
# move-to-trash
#
# Teemu Leisti 2019-09-05
#
# 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 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 utility 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 first invocation
# of 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.
move_to_abspath="$trash_dir_abspath/$file_basename-$(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="$trash_dir_abspath/$file_basename-$(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, as for that to be the case, an
# extremely unlikely run 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
gvfs-trash
과거에,하지만 당신은 나의 호기심을 촉발 때까지 명령 줄에서 복원 할 필요가 없었어요. 연결된 질문에 대한 답변이 도움이 될 수 있습니다.