Does the Linux system have a recycle bin? The answer is no, Linux system does not have a recycle bin in Windows, but Linux can implement the function of the recycle bin, how to achieve it? The following small series will introduce you to the way to open the recycle bin function in Linux. Let's take a look at it.
This article modeled on the Windows Recycle Bin function, using Bash scripts on Linux did achieve, create a script instead of rm command to delete a file or directory deletion do gymnastics. The script implements the following functions: directly delete files or directories larger than 2G, otherwise put them into the $HOME/trash directory; restore the deleted files in the trash directory to the original directory; the files are automatically stored in the trash directory for more than seven days. delete.
Overview
Deletion is an operation with a high risk factor, which can cause unpredictable losses if accidentally deleted. This danger is particularly evident in Linux systems. A simple statement: rm –rf /* will remove the entire system, and Linux will not refuse to execute because of the unreasonableness of this statement. In Windows, the recycle bin feature is provided to prevent accidental deletion. After the user performs the delete operation, the file is not deleted directly from the hard disk, but is placed in the recycle bin. If you find that a file has been deleted by mistake before emptying the recycle bin, you can restore the files in the recycle bin to their original location. Linux does not provide similar functions. Delete the command rm Once the execution is confirmed, the file will be deleted directly from the system and it will be difficult to recover.
Recycle Bin Composition
This article shares three main functions for the Recycle Bin: the Delete script, the logTrashDir script, and the restoreTrash script. The Delete script is a core script that repackages the rm command. Relative to the direct deletion of rm, the command will first move the file or directory to the $home/trash directory. If the user wants to delete the file directly, you can use the -f option, and the delete script will directly call the rm –f command to remove the file from the hard disk. The logTrashDir script is used to record the information of the deleted file into a hidden file under the trash directory. The restoreTrash script is used to restore the files or directories in the trash to the original path. On Linux systems, just put these three scripts in the /bin/directory and use chmod +X filename to give executable permissions, you can use them directly. The main part of each script is described below
Delete script
Create directory
First create a directory to hold deleted files, this article is in the user root directory $HOME Create a trash directory to hold the files. The specific code is as follows:
Listing 1. Create Recycle Bin Directory
realrm=“/bin/rm”
if [ ! -d ~/trash ]
then
mkdir -v ~/trash
chmod 777 ~/trash
fi
As shown, first determine if the directory has been created. If it is not established, the first time you run the script, create a trash directory. The variable realrm holds the Linux rm script location for calling under certain conditions to delete files or directories directly.
Output Help Information
This script outputs brief help information when the user enters only the script name without inputting the parameters. The code is as follows:
Listing 2. Output help information
if [ $# -eq 0 ]
then
echo “Usage:delete file1 [file2 file3. . . "&"There is a ff" The path to the deleted file or directory, separated by spaces.
Directly deleting files
Some files that users confirm to be invalid and want to delete directly should not be placed in the recycle bin, but should be deleted directly from the hard disk. The Delete script provides the -f option to do this:
Listing 3. Deleting files directly
while getopts “dfiPRrvW” opt
do
case $opt in
f)
exec $realrm “$@”
;;
*)
# do nothing
;;
esac
done
Previous12Next Total 2 Pages
The access control list (ACL) under Linux is mainly used to control the permissions
On Linux systems, programs are generally run under the system user. Of course, other users can also
StarDict is a translation software that can perform English-Chinese translation, Ja
In order to use Qt under Linux, you need to install G++ environment before installi
Linux system 11 terminal commands that cannot be known
Linux common commands (including vi/gdb) operation skills
Linux recovery root password two methods
Linux uses PXE to install virtual machines
How to use the history command in Linux
How to install and set up Samba service
Amazon Amazon AWS Access Key Creation Tutorial
How to configure a multi-language environment for Linux systems?
Steps to install Fluentd on RedHat
How to solve Linux Chinese garbled?
How to solve the problem of cant find libjava.so under Ubuntu?
Unable to update to Windows 8.1, "0xc1900101
How to open the Win8.1 preview version to hide the "God mode"
Win7 system CMD command is disabled solution
How to install the Metro application of the Win10 10074 store to another disk?
Solve the problem that Windows XP printer can't share the problem