LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   bash script to delete and store location of full path (https://www.linuxquestions.org/questions/linux-newbie-8/bash-script-to-delete-and-store-location-of-full-path-4175481568/)

gasric 10-21-2013 04:20 AM

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. :o

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

rtmistler 10-21-2013 09:56 AM

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


gasric 10-22-2013 02:13 AM

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

catkin 10-22-2013 04:44 AM

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).

rtmistler 10-22-2013 07:49 AM

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.

Reuti 10-22-2013 08:15 AM

Quote:

Originally Posted by gasric (Post 5049526)
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"


jpollard 10-23-2013 12:55 PM

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.

gasric 10-24-2013 09:11 AM

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! :)


All times are GMT -5. The time now is 10:25 PM.