Linux learning assignment - settings of recycle bin

Posted by Darkness Soul on Thu, 03 Mar 2022 21:32:04 +0100

Linux recycle bin – create a simple recycle bin with a shell script

Requirements to be met

  1. Write a script – myrm SH, delete files. It has the function of recycle bin. All deleted files are stored in / my_backup directory;
  2. Write a script that can recover deleted files_ back. SH, which can restore the deleted file to the original path.
First think about deleting the script – myrm Production of SH
  • This requirement seems to be to delete files. In fact, the operation is to move the files you want to delete in the current folder to / my_ Under the back directory
  • This requirement can be realized through mkdir and mv statements

Thinking about this, you can start writing script code

#!/bin/bash
# mkdir is created every time to ensure that the folder exists
mkdir /my_backup
# Move the code you want to delete with mv statement to achieve the purpose of looking like deletion
# This place $* means that the script can pass in multiple parameters and delete multiple files
mv $* /my_backup
# You can then use the ls statement to view the files in the recycle bin
ls /my_backup

Considering that the mkdir statement is redundant when the folder already exists, I want to improve the above code: what if the mkdir statement is not used when the folder already exists? (don't ask, ask is Virgo)
Whether this method can be used to test whether the file exists:
Use of simple supplementary test statement

test -d /bin/lianxi  # This is to judge whether there is a folder named lianxi under the bin directory

[ -d /bin/lianxi ] # This is the equivalent of the above statement, which can achieve the same purpose

test -f /etc/passwd  # This is to judge whether there is a file named passwd in the etc directory

[ -f /etc/passwd ] # This is the equivalent of the above statement

Judging with the above statement, there will be a return value when the return value ($?) If it is 0, it proves that the file / folder exists. Otherwise, it means that the file / folder does not exist.

Therefore, using this statement and if then else fi flow control statement can improve the code:

#!/bin/bash
if [ -d /my_backup ]  # So, when / my_ When backup exists, there will be no redundant mkdir statement. It seems that the code is more comfortable!
then
	mv $* /my_backup
	ls /my_backup
else
	mkdir /my_backup
	mv $* /my_backup
	ls /my_backup
fi
First think about the recovery script – my_ back. Production of SH
  • When you start making recovery scripts, you must first think about how to achieve this goal;
  • The easiest thing to think of is the log file. You can complete the recovery operation by deleting the log every time and deleting the log after recovery;
  • Contents to be recorded in the log file: file name and original storage path.

ok, start to practice after thinking about it. First, delete the above script - myrm SH to add a statement stored in the log

#!/bin/bash
if [ ! -d /my_backup ]  # If it doesn't exist, create it. Note that the code has been changed here and a new one has been added!
then
	mkdir /my_backup
	touch /my_backup/logging.txt
else
	touch /my_backup/logging.txt
fi
# Add a for process control statement to redirect the content to the log file
for temp in $*
do
	a=$(find / -name $temp)
	echo $temp $a >> /my_backup/logging.txt
done
# Then use mv statement to achieve the purpose of deletion
mv $* /my_backup

ls /my_backup

Start writing recovery script

  • Receive parameter - the file name to be recovered;
  • Find the file name of the file you want to restore in the log file and restore it;
  • Remember to delete the log after the recovery is completed, otherwise an error will occur if the same file is recovered next time.
#!/bin/bash

# Through the process control of the for loop, multiple files can be recovered
for temp in $*
do
	a_path=$(cat /my_backup/logging.txt|grep $temp|awk '{print $2}')
	a_num=$(cat /my_backup/logging.txt|grep -n $temp|awk -F':' '{print $1}')
	mv /my_backup/$temp $a_path
	sed -i "${a_num}d" /my_backup/logging.txt
done
  • Through the above myrm SH and my_back.sh can achieve the effect of this simple recycle bin.
  • You can then grant executable permissions to the script file using the statement: chmod +x script file name.
  • Then use the alias statement to set the alias of rm, and the effect of using rm statement to delete files and set the recycle bin is achieved.

Similarly, the simple recycle bin has the following problems:

  1. It's silly to encounter a file with the same name;
  2. When restoring, you can consider adding an if process control to judge. If the target file exists, you can restore it. If it does not exist, you can tell the user that the file you want to restore does not exist.

At present, there are these problems. You can try to improve in the later learning process. If you have better ideas, you can communicate together!

Topics: Linux shell