Delete restore and empty trash

 
Thread Tools Search this Thread
Homework and Emergencies Homework & Coursework Questions Delete restore and empty trash
# 1  
Old 12-07-2010
Delete restore and empty trash

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted!

1. The problem statement, all variables and given/known data:


A set of Linux shell scripts is required to allow users to ‘remove' files without them really disappearing until the ‘dustbin' is emptied. The three shell scripts required are :-

del <filename> - This script should move the file called <filename> (a full or relative pathname) to the
dustbin directory.

trash [-a] - This script should remove the contents of the dustbin directory.
If the -a option is not used, the script should print the filenames in the dustbin one by one and ask the
user for confirmation that they should be deleted.
Otherwise, If the -a option is used, the script should simply remove ALL files from the dustbin.

restore [-n] <filename> - This script should move the file called <filename> (a full or relative pathname)
back to its original directory.
If the -n option is used, the script should allow the file to be moved to a directory nominated by the user.


2. Relevant commands, code, scripts, algorithms:

bash code
using ubuntu
I used a dustbin directory to keep my deleted files


3. The attempts at a solution (include all code and scripts):

del script
Code:
#!/bin/bash
echo "Do you want to delete this file?"
echo "Y/N"
read ans
case "$ans" in
  Y) echo "`readlink -f $1`" >>/home/ian/store & mv $1 /home/ian/dustbin ;;
  N) echo "File not deleted" ;;
esac

trash script
Code:
#!/bin/bash

if ["$1" == "-a"]
then 
  cd /home/ian/dustbin
  rm -rf*
  cd /home/ian
  rm store
  touch store
else
  cd /home/ian/dustbin
  ls > dustbin
  for line in `cat dustbin`
  do
    echo "Do you want to delete?" $line
    echo "Y/N"
    read ans
    case "ans" in
      Y) rm $line;;
      N) "";;
    esac
  done
  rm dustbin
  cd
  rm store
  touch store
fi

restore script
Code:
#!/bin/bash
if [ "$1" == "-n" ]
then
  cd /home/ian/dustbin
  restore =`grep $2 /home/ian/store`		
  filename = `basename "$restore"`		
  echo "Where do you want to save to?"
  read location
  location1 = `readlink -f $location`		
  mv -i $filename "$location1"/$filename		
else
##if the restore script is run without the -n switch the file will be restored to its original location
  cd /home/ian/dustbin				
  restore = `grep $1 /home/ian/store`		
  filename=`basename "$restore"`			
  mv -i $filename $location			
fi

4. Complete Name of School (University), City (State), Country, Name of Professor, and Course Number (Link to Course):
Edinburgh Napier University , Edinburgh , UK , Dr Alistair Armitage , CSN08101 Systems and Services

Note: Without school/professor/course information, you will be banned if you post here! You must complete the entire template (not just parts of it).


Note: Without school/professor/course information, you will be banned if you post here! You must complete the entire template (not just parts of it).


***********************************

I am just looking to see if there is anything I can do to improve the scripts?

Any help would be appreciated

Last edited by Scott; 12-07-2010 at 03:11 PM.. Reason: Added code-tags and indentation for readability
# 2  
Old 12-08-2010
Restore function should test to ensure file they are trying to restore is actually in the dustbin.

Check for upper or lower case responses in your case statements (could also consider looping with a trap for non YorN answers):

Code:
     case $ans in
        Y|y) rm "$file";;
        N|n) ;;
        *) echo "Please answer Y or N" ;;


Couple of simplification tips:

To truncate a file you can use:

Code:
> store

Instead of piping ls to a tempfile and reading it back, try:

(also consider a renamed of variable to "file" to make things more self-documenting):
Code:
for file in *
do
   . . .
done


Last edited by Chubler_XL; 12-08-2010 at 08:10 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Can't delete a file in the trash on OSX 10.11.6

I use a Mac running OSX El Capitan = UNIX user the hood. After rearranging files on an external hard drive, I ended up with a folder in the trash containing a file called "~INITPicker 2.0". I cannot empty the trash, and so delete the file. I even tried terminal, using the rm file command and... (38 Replies)
Discussion started by: djfilms
38 Replies

2. Shell Programming and Scripting

Delete/Restore Script

Hi there is anyone able to show me two scripts for a delete and a restore command. Delete - move a file to a dustibin directory that i have created. check to see if that file exsits first in the dustbin if so delete the file before moving the new one. Restore - return the file to its... (1 Reply)
Discussion started by: arsenal1994
1 Replies

3. UNIX for Dummies Questions & Answers

How to delete some empty folders?

I have an amount of folders and I want to delete only the empty ones. But I have more than 200 empty folders, so I would preffer do not delete one by one... I know it is possible, but I don't know how. I've tried with the size, using 'du' command, and saving the result in a file. After that, I made... (3 Replies)
Discussion started by: saitsug
3 Replies

4. UNIX for Dummies Questions & Answers

Recreate/Restore Repository and/or change/delete Public Keys???

I have a BIG problem... I host a public (Debian) repository on Cydia (for jailbroken iphones). I use my home Mac running Ubuntu in VMWare Fusion. For the repository, I have to have 5 items in a folder to make it 'live:' 1. deb_files folder (to hold my Debian packages) 2. Packages file 3.... (0 Replies)
Discussion started by: thazsar
0 Replies

5. Shell Programming and Scripting

scripts to Initialise,backup,restore,delete and empty

you are to write scripts for a customer that would require a system enable the backing up of files and directories. The script should (these names should be used): 1. INITIALISE: Initialise the directory for the backup(called backup and should be in the home directory) and any other... (1 Reply)
Discussion started by: babby01
1 Replies

6. Shell Programming and Scripting

Del - trash - restore CSH commands!!

Anyone who could help me with those three commands, i'm really new in cshell script so please be understandable.. Need to demostrate those three commands del trash (incl -ai) i that you will be asked to confirmed that they can be deleted a to move all those files from dustbin and to... (2 Replies)
Discussion started by: Visi_Ks
2 Replies

7. UNIX for Advanced & Expert Users

How to delete empty lines

abc# abc#this is a test abc#this is a test to delete abc# xyz# xyz#this is a test two xyz# In the above example '#' is common. How to do delete the emply lines. In specific to observe the output as: abc#this is a test abc#this is a test to delete xyz#this is a test two . . . . (5 Replies)
Discussion started by: Aejaz
5 Replies

8. Linux

empty trash

Hello i am trying to empty the trash using rm or rmdir command . Any idea how to delete the folders and subfolders using one of the above commands? Thank You (2 Replies)
Discussion started by: exhumation
2 Replies

9. UNIX for Dummies Questions & Answers

Can't empty trash in OS X!

I trashed Adobe Acrobat (not Acrobat Reader today) because Adobe screwed up my licensing; their solution was to issue me a new serial number (thanks, guys). Anyhoo, I can't delete all of the Acrobat folder from my trash in OS X, nor can I move it out of the trash (it just copies). Any suggestions?... (4 Replies)
Discussion started by: chenly
4 Replies
Login or Register to Ask a Question