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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
|
|
11-15-2016, 07:18 AM
|
#1
|
LQ Newbie
Registered: Nov 2016
Posts: 9
Rep:
|
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.
|
|
|
11-15-2016, 07:21 AM
|
#2
|
LQ Guru
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 27,189
|
Quote:
Originally Posted by Arsenal1994
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.
|
|
|
11-15-2016, 07:28 AM
|
#3
|
LQ Newbie
Registered: Nov 2016
Posts: 9
Original Poster
Rep:
|
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
|
|
|
11-15-2016, 07:35 AM
|
#4
|
LQ Guru
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 27,189
|
Quote:
Originally Posted by Arsenal1994
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.
|
|
|
11-15-2016, 07:44 AM
|
#5
|
LQ Newbie
Registered: Nov 2016
Posts: 9
Original Poster
Rep:
|
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.
|
|
|
11-15-2016, 07:59 AM
|
#6
|
LQ Newbie
Registered: Nov 2016
Posts: 9
Original Poster
Rep:
|
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
|
|
|
11-15-2016, 08:17 AM
|
#7
|
LQ Guru
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 27,189
|
Quote:
Originally Posted by Arsenal1994
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: - 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.
- then - If the file DOES exist.....
- rm $1 - ...remove it. Again, remove it from WHERE? Specify the path, unless you're doing it on the command line.
- else - Otherwise, the file does NOT exist, so.....
- 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.
|
|
|
11-15-2016, 08:20 AM
|
#8
|
Senior Member
Registered: Aug 2006
Location: Detroit, MI
Distribution: GNU/Linux systemd
Posts: 4,278
|
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.
|
11-15-2016, 08:23 AM
|
#9
|
LQ Newbie
Registered: Nov 2016
Posts: 9
Original Poster
Rep:
|
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
|
|
|
11-15-2016, 08:27 AM
|
#10
|
LQ Guru
Registered: May 2005
Location: boston, usa
Distribution: fedora-35
Posts: 5,326
|
most desktop environments have a trash. are you re-inventing this wheel as a practical exercise ?
|
|
|
11-15-2016, 08:27 AM
|
#11
|
Moderator
Registered: Aug 2002
Posts: 26,199
|
Probably not. Is $1 a relative or absolute path. How are you going to account for sub-directories?
|
|
|
11-15-2016, 08:30 AM
|
#12
|
LQ Newbie
Registered: Nov 2016
Posts: 9
Original Poster
Rep:
|
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.
|
|
|
11-15-2016, 08:53 AM
|
#13
|
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
|
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.
|
|
|
11-15-2016, 09:40 AM
|
#14
|
LQ Newbie
Registered: Nov 2016
Posts: 9
Original Poster
Rep:
|
What variable would i use so it can work with boths path types?
|
|
|
11-15-2016, 09:43 AM
|
#15
|
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
|
Quote:
Originally Posted by Arsenal1994
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.
|
|
|
All times are GMT -5. The time now is 05:27 PM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|