Linux - NewbieThis 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.
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.
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.
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.
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.
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
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.
#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
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.