Write a bash shell script to carry out each of the following tasks: Safe Delete:
ID: 3807407 • Letter: W
Question
Write a bash shell script to carry out each of the following tasks:
Safe Delete:
When you use the “rm” command in Linux, it will delete the specified files, with no chance for recovering them back later. Write a script (called srm) that will safely delete the files passed to it as command-line arguments. For example, typing the command: “srm file1 file2 file3”, the script shall not actually delete these files, but instead it shall move them to a /home/yourname/trash directory. Also, the script shall do the following:
1. When it starts, it shall check the trash directory for files older than 48 hours and delete them.
2. If the script is called with a –r switch (i.e. srm –r file1 file2 file3), then it shall not delete anything, but rather it shall recover these files by searching for them in the trash, and if it finds any one of them it shall move it back to the same directory it was deleted from.
3. If the script is called with a –h switch (i.e. srm –h file1 file2 file3), then it shall do the same as the –r switch, but found files shall be recovered to the current directory.
or in other way:
Safe Delete
Implement, as a script, a "safe" delete command, sdel.sh. Filenames passed as command-line arguments to this script are not deleted, but instead gzipped if not already compressed (use file to check), then moved to a ~/TRASHdirectory. Upon invocation, the script checks the ~/TRASH directory for files older than 48 hours and permanently deletes them. (An better alternative might be to have a second script handle this, periodically invoked by the cron daemon.)
Explanation / Answer
#!/bin/sh
if [ $# -eq 0 ]
then
echo "Missing arguments!!! Please provide arguments"
else
echo "Filename are:"
for i in $@
do
echo $i
mv $i /home/admin/trash
done
fi
Execute using following command: ./test.sh file1 file2...
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.