LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 11-15-2016, 07:18 AM   #1
Arsenal1994
LQ Newbie
 
Registered: Nov 2016
Posts: 9

Rep: Reputation: Disabled
Linux Script For Delete/Restore Help!


Hi guys,

im pretty new to linux and was looking for a way to create two separate scripts.

Delete - moves the file to the dustbin directory and if there is a file with the same name it will delete the one already in the dustbin.

Restore - returns the deleted file to its original location and if the directory it was in was deleted it restores that and all its sub directories.
 
Old 11-15-2016, 07:21 AM   #2
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 27,189

Rep: Reputation: 8068Reputation: 8068Reputation: 8068Reputation: 8068Reputation: 8068Reputation: 8068Reputation: 8068Reputation: 8068Reputation: 8068Reputation: 8068Reputation: 8068
Quote:
Originally Posted by Arsenal1994 View Post
Hi guys,
im pretty new to linux and was looking for a way to create two separate scripts.

Delete - moves the file to the dustbin directory and if there is a file with the same name it will delete the one already in the dustbin.

Restore - returns the deleted file to its original location and if the directory it was in was deleted it restores that and all its sub directories.
Great...now that we know what you WANT, can you show us your efforts to DO it? Read the "Question Guidelines" link in my posting signature. We are happy to help you if you're stuck, but you have to post what YOU have written/done/tried on your own first. We aren't going to write your scripts (or do your homework) for you.
 
Old 11-15-2016, 07:28 AM   #3
Arsenal1994
LQ Newbie
 
Registered: Nov 2016
Posts: 9

Original Poster
Rep: Reputation: Disabled
Delete -

mv $1 ~/Dustbin
if [-e $1]
then
...
im having trouble trying to figure out how to overwrite the older file already in the dustbin if it exsits
 
Old 11-15-2016, 07:35 AM   #4
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 27,189

Rep: Reputation: 8068Reputation: 8068Reputation: 8068Reputation: 8068Reputation: 8068Reputation: 8068Reputation: 8068Reputation: 8068Reputation: 8068Reputation: 8068Reputation: 8068
Quote:
Originally Posted by Arsenal1994 View Post
Delete -
Code:
mv $1 ~/Dustbin
if [-e $1]
then
...
im having trouble trying to figure out how to overwrite the older file already in the dustbin if it exsits
Please use CODE tags when posting code. And this is your script...what do you WANT it to do?

The "mv" will overwrite the destination to start with, so the mv before the check for the file existing will ALWAYS return true, since the file will always be moved FIRST. Check for the file existing first, and if it was me, I'd prompt the user to verify it was ok to overwrite, and get them to answer Y or N. Yes runs the mv command, No exits.
 
Old 11-15-2016, 07:44 AM   #5
Arsenal1994
LQ Newbie
 
Registered: Nov 2016
Posts: 9

Original Poster
Rep: Reputation: Disabled
what i want it to do is act like a recycle bin by moving the file to the dustbin but to also check if a file of the same name exists, if so delete the file thats already there. Sorry im new to this forum.

It doesnt require for the user to confirm anything

Last edited by Arsenal1994; 11-15-2016 at 07:46 AM.
 
Old 11-15-2016, 07:59 AM   #6
Arsenal1994
LQ Newbie
 
Registered: Nov 2016
Posts: 9

Original Poster
Rep: Reputation: Disabled
Code:
if [-e $1]
then
rm $1
else
mv $1 ~/Dustbin
Would this work?
Im looking to see if the file exists and if so deleted the one in the dustbin directory and move the new one.
if it doesnt exist then move the file
 
Old 11-15-2016, 08:17 AM   #7
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 27,189

Rep: Reputation: 8068Reputation: 8068Reputation: 8068Reputation: 8068Reputation: 8068Reputation: 8068Reputation: 8068Reputation: 8068Reputation: 8068Reputation: 8068Reputation: 8068
Quote:
Originally Posted by Arsenal1994 View Post
what i want it to do is act like a recycle bin by moving the file to the dustbin but to also check if a file of the same name exists, if so delete the file thats already there. Sorry im new to this forum.

It doesnt require for the user to confirm anything
I understand what you want...but what is the point of checking if the file is there, if you don't do anything with that knowledge?
Quote:
Originally Posted by Arsenal1994
Code:
if [-e $1]
then
rm $1
else
mv $1 ~/Dustbin
Would this work? Im looking to see if the file exists and if so deleted the one in the dustbin directory and move the new one. if it doesnt exist then move the file.
The best way to learn scripting is to go through things one line at a time, and think about what they do. Check out the scripting tutorial here: http://tldp.org/HOWTO/Bash-Prog-Intro-HOWTO.html And they also have more advanced ones too. Start your script with "#!/bin/bash" as the very first line. And to go through it one line at a time:
  1. if [-e $1] - Check for the existence of a file named $1 (first command line variable). That said...WHERE are you checking for it? Because unless you specify a path, it's going to check the current directory, wherever you run the script from. You need to tell it where to look.
  2. then - If the file DOES exist.....
  3. rm $1 - ...remove it. Again, remove it from WHERE? Specify the path, unless you're doing it on the command line.
  4. else - Otherwise, the file does NOT exist, so.....
  5. mv $1 ~/Dustbin - ...move the file to the Dustbin directory.
You're a good bit there. However, there are some things to keep in mind when writing a good script. While this may satisfy your immediate NEED for your homework, think a bit further down the road. How will the user *KNOW* to specify a path on the command line? Or even how many arguments your script needs to run, and what they are? If the file exists...shouldn't you ask the user if they want to overwrite the file first, before deleting it? What if the one in the Dustbin is older (or is something completely different with the same name)...now you're deleting they're work, leaving them no way to recover.

I would put an option in there to check to see if $1 was empty or not, and if so, pop up a little 'help blurb' like "Please provide a file name (with path) to move to the Dustbin; for example, "/some/path/to/filename"". I'd also put something in after the check (step 1), to inform the user that filename exists in the Dustbin, and show them the file sizes/dates (a simple "ls -l" could do this), and ask them to confirm they want to overwrite the Dustbin file with the new one.
 
Old 11-15-2016, 08:20 AM   #8
szboardstretcher
Senior Member
 
Registered: Aug 2006
Location: Detroit, MI
Distribution: GNU/Linux systemd
Posts: 4,278

Rep: Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694
Great starting point for tests like this:

http://tldp.org/LDP/Bash-Beginners-G...ect_07_01.html

Code:
#backup idea
if [ -e "~/Dustbin/$1" ]
then
  rm -f ~/Dustbin/$1
  mv $1 ~/Dustbin/
else
  mv $1 ~/Dustbin/
fi
 
1 members found this post helpful.
Old 11-15-2016, 08:23 AM   #9
Arsenal1994
LQ Newbie
 
Registered: Nov 2016
Posts: 9

Original Poster
Rep: Reputation: Disabled
I will have a look at your links thank you.
Quote:
#backup idea
if [ -e "~/Dustbin/$1" ]
then
rm -f ~/Dustbin/$1
mv $1 ~/Dustbin/
else
mv $1 ~/Dustbin/
fi
so this will check if the file exists in the dustbin, then if it does it will remove it then replace it with then current one and if it doesnt it will move it.
thats all i need it to do at the moment
 
Old 11-15-2016, 08:27 AM   #10
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fedora-35
Posts: 5,326

Rep: Reputation: 919Reputation: 919Reputation: 919Reputation: 919Reputation: 919Reputation: 919Reputation: 919Reputation: 919
most desktop environments have a trash. are you re-inventing this wheel as a practical exercise ?
 
Old 11-15-2016, 08:27 AM   #11
michaelk
Moderator
 
Registered: Aug 2002
Posts: 26,199

Rep: Reputation: 6089Reputation: 6089Reputation: 6089Reputation: 6089Reputation: 6089Reputation: 6089Reputation: 6089Reputation: 6089Reputation: 6089Reputation: 6089Reputation: 6089
Probably not. Is $1 a relative or absolute path. How are you going to account for sub-directories?
 
Old 11-15-2016, 08:30 AM   #12
Arsenal1994
LQ Newbie
 
Registered: Nov 2016
Posts: 9

Original Poster
Rep: Reputation: Disabled
Yes its just an excersie, to create a two scripts. One to move the file to the dustbin and another to return a file in the dustbin to its original location.
The script has to cope with absolute pathnames, relative
pathnames, multiple pathnames and wildcards.
 
Old 11-15-2016, 08:53 AM   #13
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
sounds like your teacher is thinking up situations that rarely or never take place, for his or her students to get real world experience.

though this same scheme could be used towards any directory and file within the system.

logical steps,
find file

check if destination already has a same copy of it,
if not then move it to destination
if yes then delete that one that is already in the destination.

to deal with absolute and relative paths use a different variable other then $1


which will incur a little more coding then before.


to restore
you have to find out how the system is actually storing that information then use it to your advantage.

http://www.tldp.org/LDP/abs/html/othertypesv.html

http://linuxcommand.org/wss0130.php

Last edited by BW-userx; 11-15-2016 at 09:04 AM.
 
Old 11-15-2016, 09:40 AM   #14
Arsenal1994
LQ Newbie
 
Registered: Nov 2016
Posts: 9

Original Poster
Rep: Reputation: Disabled
What variable would i use so it can work with boths path types?
 
Old 11-15-2016, 09:43 AM   #15
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
Quote:
Originally Posted by Arsenal1994 View Post
What variable would i use so it can work with boths path types?
one you create all by yourself. did you not read that BASH Scripting web page someone gave you?

do you know how to test your variables and code?

do one thing at a time, for each piece of code you write. if it can be done in the terminal then it can be done in a BASH Script.

Last edited by BW-userx; 11-15-2016 at 09:45 AM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Two scripts, delete and restore highland Linux - Newbie 3 11-24-2014 09:10 PM
I need a script to Delete Selected BackupFiles and Restore selected backup file finalwar Linux - Newbie 3 07-20-2010 02:10 AM
Site definitely hacked. Can't delete files to restore backup. painterj Linux - Security 9 04-10-2010 07:38 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 05:27 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration