LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
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 10-21-2013, 04:20 AM   #1
gasric
LQ Newbie
 
Registered: Oct 2013
Posts: 18

Rep: Reputation: Disabled
bash script to delete and store location of full path


Hi all I am needing help on a bash script I'm trying to program, here's a little info on my knowledge and my problem.

I have only just started using linux (ubuntu) so my knowledge is poor but I am really interested in it and slowly learning more and more but my programming skills are also poor but I have never done this style of computing before so for give me for bad programming practices.

My Problem:
I am trying to create a script that deletes files but moves them to a "dustbin" location but at the same time save its file path in a .txt file in case I need to restore it . Here's what I have so far for that and it works.

For i in $1
do mv $i ~/dustbin
echo "Moved File $i"
done

Now I have located solved solution to some thing similar to what I'm trying to achieve here: http://www.linuxquestions.org/questi...re-4175438482/

But because my knowledge is not very good I get a little lost.

Thanks for your time all
 
Old 10-21-2013, 09:56 AM   #2
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,882
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
Please place code or scripts within code blocks. Use advanced mode to assist with this.

A quick test shows that the script works. however there is a problem; which is that $i is gone once you've moved it. Further, you want to echo this information to a file, not just to stdout. Try using a variable:

Here's your original:

Code:
For i in $1
do mv $i ~/dustbin
echo "Moved File $i"
done
Here are some suggested modifications:

Code:
#!/bin/bash
# All scripts should start with either #!/bin/sh or #!/bin/bash depending on which shell you're using

# comments prepended with # are good.

set -xv # use or comment out as needed, this turns on debugging and gives you debug as you run and test

For i in $1
do fn=$i
# use a variable name for your file - note that it will not contain a full path and name, just the name
mv $i ~/dustbin
echo "Moved File $fn" >> log-file.txt; # >> redirects your echo to the file named
# since $fn is not re-set until you do the next loop iteration, it is still valid, wheras $i was invalidated when it was moved.
done
 
1 members found this post helpful.
Old 10-22-2013, 02:13 AM   #3
gasric
LQ Newbie
 
Registered: Oct 2013
Posts: 18

Original Poster
Rep: Reputation: Disabled
Hi rtmistler, thanks for taking the time to answer my question.

I have gone with a different approach which I think you will agree is better overall:

Code:
#!/bin/sh
if [ -f $1 ]
file=`basename $1`
then echo `find -name $file` >> ~/fp.txt && mv $1 ~/dustbin && echo "$1 Moved"
echo "File does not exist"
fi
Now I did try a different method but with an IF statement but I was getting a denied permission which I cant understand as I thought I was using the root user.

Code:
#!/bin/sh
if [ -f $1 ]
then echo "$ readlink -f" >> ~/fp.txt && mv $1 ~/dustbin && echo "$1 Moved"
echo "File does not exist"
fi
Would you mind checking what I did wrong with the "readlink -f"?

Thanks
 
Old 10-22-2013, 04:44 AM   #4
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Not 100% sure what you want to do, but assuming you want to keep a record of the files you have tried to move in ~/fp.txt then you code doesn't do it for two reasons:
  1. The shell does not substitute anything in "$ readlink -f", so exactly that (less the double quotes) is written to ~/fp.txt. If you want the output of readlink -f $1 written to ~/fp.txt then change to echo $(readlink -f $1) >> ~/fp.txt (that is functionally identical to echo `readlink -f $1` >> ~/fp.txt).
  2. In case $1 is a path containing one or more symbolic links, then the readlink output will not be the same as $1 (you knew that already, or you wouldn't be using readlink).
 
Old 10-22-2013, 07:49 AM   #5
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,882
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
I wasn't familiar with readlink, but looked it up to see that it's how to get the information on a symbolic link. I'm confused there because bash supports the "-h" test operator.

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

Quote:
-h

file is a symbolic link
So why bother using readlink?

I recommend you stay basic until you have to do differently. Otherwise you write this complex script which you'll have a lot of issues debugging.

Re-Note my earlier recommendation that you place:
Quote:
set -xv
into your script. Do that with a short script which works and run that script on the command line, you'll see the individual steps of your script output to the command line. Further, test each script at the command line first before you set it up to run automatically, if that's the final intentions.
 
Old 10-22-2013, 08:15 AM   #6
Reuti
Senior Member
 
Registered: Dec 2004
Location: Marburg, Germany
Distribution: openSUSE 15.2
Posts: 1,339

Rep: Reputation: 260Reputation: 260Reputation: 260
Quote:
Originally Posted by gasric View Post
Code:
do mv $i ~/dustbin
echo "Moved File $i"
done
Just two thoughts while reading this thread:

– The mv command has a -v option to give a message about the executed command.

– When the text file with the original paths gets large, it might take some time to get the necessary entry. Depending on your file system and mount options you can store the information as extended attributes:
Code:
$:~/baz> touch foobar
$:~/baz> setfattr -n user.path -v $(pwd) foobar
$:~/baz> getfattr -d foobar
# file: foobar
user.path="/home/reuti/baz"

$:~/baz> mv -v foobar /tmp
‘foobar’ -> ‘/tmp/foobar’
removed ‘foobar’
$:~/baz> cd /tmp
$:/tmp> getfattr -d foobar 
# file: foobar
user.path="/home/reuti/baz"
 
Old 10-23-2013, 12:55 PM   #7
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,912

Rep: Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513
I would suggest using tar to make a copy - that way the full path would preserved within the tar output file, AND you would also have a symbolic links preserved appropriately.

The basic problem is that you would loose your data if it just happened to have the same file name in two different paths - the first one removed would be lost, and your tracking data would be invalid.

Putting the data in a tar file allows you to use compression to reduce storage, AND you can generate a unique file name to hold the tar file. If you wanted a tracking file with paths, just list the contents of the tar file (tar -vtf tarfilename ...) and have the output combined with the unique tar file name appended to the tracking file.
 
Old 10-24-2013, 09:11 AM   #8
gasric
LQ Newbie
 
Registered: Oct 2013
Posts: 18

Original Poster
Rep: Reputation: Disabled
Thanks all for your input but I'm very new to this and would like to keep things simple for now.

Now I have improved my delete script with a few error checkers and it works really well so my next step is to restore but I'm not sure I even have this right as I'm reading random stuff and if anyone has a nice newbie guide on scripts they think is best to read that would be great but here's the code I have right now.

Code:
if [ -f $1 ]
restore=`grep $1 ~/fp.txt` # get location of file
filename=`basename "$restore"` 
location=`$location` 
then 
mv $1 $filename $location #move file to location
fi
im going to read up some more on this and see if i can work this out on my own but any pointers would be great, cheers again for the help!
 
  


Reply



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
[SOLVED] bash script - return full path and filename Andy Alt Programming 44 01-10-2013 09:11 AM
[SOLVED] what is lighter on the system, variable calls, or full path in BASH script? lleb Linux - Newbie 6 05-30-2012 12:53 PM
How to get full path to script file inside script itself? And in case of sym links? maggus Linux - Newbie 3 05-28-2009 08:40 AM
bash script path issue - how to pass a path as a string to a variable PiNPOiNT Programming 5 04-17-2009 05:48 PM
How to parse full path of file name in bash ? hq4ever Programming 2 03-28-2005 03:31 PM

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

All times are GMT -5. The time now is 03:34 AM.

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