1,105,585 Community Members

Recycle bin in bash problem

Member Avatar
anjoz
Light Poster
34 posts since Aug 2008
Reputation Points: -9 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

I need to make a recycle bin code using bash my problem is that i dont know how to get the previous path of the files that ive deleted so when i restore it it will go to the directory that it was deleted from ive tried putting the paths to a txt file so i can read it but my problem is if there are multiple files how will i know what to read. Can you give mo suggestions on how to approch this problem im kinda lost now heres to code

#!/bin/bash

mkdir -p "$HOME/Trash"
if [ $1 = -restore ]; then
	while read file; do
	mv $HOME/Trash/$2 /$file
	done < try.txt
else
	if [ $1 = -restoreall ]; then
		mv $HOME/Trash/* /$PWD
	else
		if [ $1 = -empty ]; then
			rm -rfv /$HOME/Trash/*
		else
			mv $PWD/"$1"/$HOME/Trash
			echo -n "$PWD" >> /$HOME/Bash/try
		fi
	fi
fi
Member Avatar
JeoSaurus
Posting Whiz in Training
227 posts since Sep 2008
Reputation Points: 32 [?]
Q&As Helped to Solve: 36 [?]
Skill Endorsements: 0 [?]
 
0
 

Hello anjoz!

'readlink -f' will give you the full path to a filename, but I'm not sure how to overcome the issue of storing those in a text file if there's more than one file with the same name.

I think a better solution might be to either store the file with the full path (/home/Trash/full/path/to/file)

EXAMPLE:

ABSOLUTE=`readlink -f path/to/testfile.sh`
FILEPATH=`dirname $ABSOLUTE`
mkdir -p $HOME/Trash/$FILEPATH
mv $ABSOLUTE $HOME/Trash/$FILEPATH/

OR

tar/gzip the files, preserving the full path by changing the working directory to / before storing and before REstoring. You still might run into a problem with files that have the same name, but you could probably work out a naming system to get around that.

EXAMPLE:

ABSOLUTE=`readlink -f path/to/testfile.sh`
FILENAME=`basename $ABSOLUTE`
cd / && tar czf $HOME/Trash/$FILENAME.tar.gz $ABSOLUTE

I hope this helps!
-G

Member Avatar
anjoz
Light Poster
34 posts since Aug 2008
Reputation Points: -9 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

heres what i have done so far my problem is that when i move a file with the same name in the trash folder i just overwrites the previous file can you give me any suggestions on how to approch this problem

#!/bin/bash

mkdir -p "$HOME/Trash"
touch "$HOME/.info"
if [ $1 = -restore ]; then
	argv=($*)
	for (( x = 1 ; x < ${#argv[*]} ; x++ ))
	do
		while read file; do
			path=$(basename $file)
			if [ "$path" = "${argv[x]}" ]; then
				mv $HOME/Trash/${argv[x]} /$file
				sed "/$path/d" /$HOME/.info >tmp
				mv tmp /$HOME/.info
			fi
		done < /$HOME/.info
	done
else
	if [ $1 = -restoreall ]; then
		while read file; do
			path=$(basename $file)
			mv $HOME/Trash/$path /$file
			sed "/$path/d" /$HOME/.info >tmp
			mv tmp /$HOME/.info
		done < /$HOME/.info
		mv $HOME/Trash/* /$PWD
	else
		if [ $1 = -empty ]; then
			rm -rfv /$HOME/Trash/*
			sed "/$HOME/d" /$HOME/.info >tmp
			mv tmp /$HOME/.info
		else
			args=($*)
			for (( i=0 ; i<${#args[*]} ; i++ ))
			do
				ABSOLUTE=$(readlink -f ${args[i]})
				mv $ABSOLUTE $HOME/Trash
				echo -e $ABSOLUTE>> /$HOME/.info
			done
		fi
	fi
fi
Member Avatar
JeoSaurus
Posting Whiz in Training
227 posts since Sep 2008
Reputation Points: 32 [?]
Q&As Helped to Solve: 36 [?]
Skill Endorsements: 0 [?]
 
0
 

heres what i have done so far my problem is that when i move a file with the same name in the trash folder i just overwrites the previous file can you give me any suggestions on how to approch this problem
...

Hi again!

I'm glad the 'readlink' command worked for you! I did make a suggestion in my post regarding the problem of files with the same name. Was that not what you were looking for? Here they are again:

----------
I'm not sure how to overcome the issue of storing those in a text file if there's more than one file with the same name.

I think a better solution might be to either store the file with the full path (/home/Trash/full/path/to/file)

OR

tar/gzip the files, preserving the full path by changing the working directory to / before storing and before REstoring.
----------

If the goal is to not have sub-directories in your Trash directory, you could use the tar.gz option, and use the path in the filename (replace '/' with '_' or something similar).

I hope this helps!
-G

Member Avatar
anjoz
Light Poster
34 posts since Aug 2008
Reputation Points: -9 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

i think im almost done ive taken care of the same name problem with time stamps my problem now is when i delete 2 same files in diffent folders example new.txt in desktop and new.txt in home the time is the same im thinking when there are the same file names in the arguments to use sleep but i dont know how to compare all the arguments if there are duplicates

#!/bin/bash

mkdir -p "$HOME/Trash"
touch "$HOME/Trash/.info"
if [ $1 = -restore ]; then
	argv=($*)
	for (( x = 1 ; x < ${#argv[*]} ; x++ ))
	do
		if [ -e $HOME/Trash/${argv[x]} ]; then
			while read file; do
				path=$(basename $file)
				if [ "$path" = "${argv[x]}" ]; then
					notimestamp=${file%.*-*}
					mv $HOME/Trash/${argv[x]} /$notimestamp
					sed "/$path/d" /$HOME/.info >tmp
					mv tmp /$HOME/.info
				fi
			done < /$HOME/.info
		else
			echo "File does not exists"
		fi
	done
elif [ $1 = -restoreall ]; then
	while read file; do
		path=$(basename $file)
		notimestamp=${file%.*-*}
		mv $HOME/Trash/$path /$notimestamp
		sed "/$path/d" /$HOME/.info >tmp
		mv tmp /$HOME/.info
	done < /$HOME/.info
elif [ $1 = -empty ]; then
	rm -rfv /$HOME/Trash/*
	sed "/$HOME/d" /$HOME/.info >tmp
	mv tmp /$HOME/.info
else
	args=($*)
	for (( i=0 ; i<${#args[*]} ; i++ ))
	do
		absolutepath=$(readlink -f ${args[i]})
		basepath=$(basename $absolutepath)
		timestamp=$(date +'%Y-%m-%d--%H-%M-%S-%N')
		if [ -e $absolutepath ]; then
			mv "$absolutepath" "/$HOME/Trash/${basepath}.${timestamp}"
			echo -e $absolutepath.${timestamp} >> /$HOME/.info
		else
			echo "File/Command does not exists"
		fi
	done	
fi
Member Avatar
JeoSaurus
Posting Whiz in Training
227 posts since Sep 2008
Reputation Points: 32 [?]
Q&As Helped to Solve: 36 [?]
Skill Endorsements: 0 [?]
 
0
 

Hello again anjoz,

You should be able to add the logic for that pretty easily... Just check to see if the destination file already exists, and if it does, sleep and get the timestamp again, or simply increment the last number.

Here's another thought... If you want the timestamp. why not save THAT into the text file, and use a number that will ALWAYS be unique as the identifier in the filename? ls -i will give you the inode where the file lives. That number should always be unique unless the two files are hard-linked, in which case they're actually the same file.

So you could do something like:

absolutepath=$(readlink -f ${args[i]})
basepath=$(basename $absolutepath)
timestamp=$(date +'%Y-%m-%d--%H-%M-%S-%N')
suffix=$(ls -i $basepath|awk '{print $1}')
if [ -e $absolutepath ]; then
   mv "$absolutepath" "/$HOME/Trash/${basepath}.${suffix}"
   echo "$timestamp $absolutepath.${$suffix}" >> /$HOME/.info
else
   echo "File/Command does not exists"
fi

I hope this helps!
-G

Member Avatar
anjoz
Light Poster
34 posts since Aug 2008
Reputation Points: -9 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

thanks that help me alot the code is done now hehhe

Member Avatar
JeoSaurus
Posting Whiz in Training
227 posts since Sep 2008
Reputation Points: 32 [?]
Q&As Helped to Solve: 36 [?]
Skill Endorsements: 0 [?]
 
0
 

Great! Glad we could help :)

You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article