LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
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 03-29-2011, 09:22 PM   #1
JangoFett353
LQ Newbie
 
Registered: Mar 2011
Posts: 3

Rep: Reputation: 0
Question I need help with a script to list deleted files with dates


Hello,

I created a script to move files to a "trashbin" This is my code:

Code:
mv $1 /home/giovanni/trashbin
echo "$1 Deleted"
exit 0
But I need to also set up a script in which I can bring them back if I needed to. Can someone shed some light on how to begin this or lead me in the right direction?

I need a way of displaying the date and time in which a certain file was deleted. But using a simple "ls" in my script has proven unsuccessful.

Thanks
 
Old 03-30-2011, 12:09 AM   #2
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,564

Rep: Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939
Well the initial script is a little limiting as it only allows for a single file to be moved, but may I suggest placing the date at the end as part of
the move. You could use something like stat to get last transaction, but hits would potentially be simpler and as you are doing a move it can be incorporated.
Code:
mv "${1}_$(date +%Y%m%d)" /home/giovanni/trashbin
echo "$1 Deleted"
exit 0
So then of course you can do the reverse to move it back, but of course if you have several files with the same name and different delete dates you will need
more logic to search through and find the one you are looking for.

Let us know if you need further help?
 
Old 03-30-2011, 12:22 AM   #3
zjoske
LQ Newbie
 
Registered: Dec 2009
Posts: 18

Rep: Reputation: 2
Hi Jango,
Moving back is very simple, just reverse your code:

Code:
mv /home/giovanni/trashbin $1
echo "$1 Restored"
exit 0
Exposing the "delete date" is more tricky as I presume a simple move does not count as accessing a file.
There is a simple workaround though, just do a "touch" on the file when moving the file to your socalled trashbin.

Code:
mv $1 /home/giovanni/trashbin
touch /home/giovanni/trashbin/`basename $1`
echo "$1 Deleted"
exit 0
This enables you to see when files are deleted when listing you directory with "ls -al"
The "touch" updates your file's time stamp, the `basename $1` strips the directory in case you call your script with complete paths. Important is that you use "`", not "'"!

Quote:
Originally Posted by JangoFett353 View Post
Hello,

I created a script to move files to a "trashbin" This is my code:

Code:
mv $1 /home/giovanni/trashbin
echo "$1 Deleted"
exit 0
But I need to also set up a script in which I can bring them back if I needed to. Can someone shed some light on how to begin this or lead me in the right direction?

I need a way of displaying the date and time in which a certain file was deleted. But using a simple "ls" in my script has proven unsuccessful.

Thanks
 
Old 03-30-2011, 12:27 AM   #4
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1949Reputation: 1949Reputation: 1949Reputation: 1949Reputation: 1949Reputation: 1949Reputation: 1949Reputation: 1949Reputation: 1949Reputation: 1949Reputation: 1949
Change $1 to $@ and it will be able to handle multiple files.

Unless you add IFS=$'\n' at the beginning of the script, or at least enclose the variable in double-quotes, it will be unable to correctly process filenames with spaces.

Finally, I suggest using the mv -t directory files pattern to the command instead.
 
1 members found this post helpful.
Old 03-30-2011, 12:31 AM   #5
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1949Reputation: 1949Reputation: 1949Reputation: 1949Reputation: 1949Reputation: 1949Reputation: 1949Reputation: 1949Reputation: 1949Reputation: 1949Reputation: 1949
Two things here.
Quote:
The "touch" updates your file's time stamp, the `basename $1` strips the directory in case you call your script with complete paths. Important is that you use "`", not "'"!
$(..) is recommended over `..`

And you can use parameter substitution to strip the path instead.
Code:
touch "/home/giovanni/trashbin/${1##*/}"
Again, quote all variables or change the IFS to handle spaces and other restricted characters.

Edit: It might also be easier to add the trash directory to its own variable, then you can more easily change the location any time you wish.
Code:
IFS=$'\n'
trash=/home/giovanni/trashbin

mv -t "$trash" "$@"
touch "${@/#*\//$trash/}"
echo "$@ Deleted"

exit 0
The parameter substitution gets a little harrier though when using $@. Of course, you could also just do the touch before the move and save yourself the headache.

Last edited by David the H.; 03-30-2011 at 12:48 AM. Reason: broken code tag + additions
 
1 members found this post helpful.
Old 03-30-2011, 12:56 AM   #6
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1949Reputation: 1949Reputation: 1949Reputation: 1949Reputation: 1949Reputation: 1949Reputation: 1949Reputation: 1949Reputation: 1949Reputation: 1949Reputation: 1949
Just noticed this:
Quote:
Moving back is very simple, just reverse your code:
No, it's not that simple, because the reversing script doesn't know where the file originally came from.

The only way I can see to do that would be to log the original location somewhere in a permanent text file as it's moved into the trash. Then the reverse script would look in this log in order to learn where to return the file.
 
1 members found this post helpful.
Old 03-30-2011, 09:51 AM   #7
JangoFett353
LQ Newbie
 
Registered: Mar 2011
Posts: 3

Original Poster
Rep: Reputation: 0
Reversing my code would only bring the file back if I knew what it was. But I need to list what is in the trashbin and then choose from a file. Other people use this terminal and they would need to access this also.
 
Old 03-30-2011, 11:05 AM   #8
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,564

Rep: Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939
Quote:
Other people use this terminal and they would need to access this also.
To their own trash bin? As having access to yours would not be particularly secure.

If your wanting to be presented with a list then the issue becomes more complicated. You could look into using select or even a pager
to scroll through all entries and then allow you to enter / choose a file. Mainly your issue will kind of depend on how large you are allowing
the trash bin to grow. If there are potentially 1000's of file and / or directories then the above suggestions would be impractical.

Is there a reason you cannot just use whatever file manager you have to peruse the trash bin?
 
Old 03-30-2011, 11:25 AM   #9
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1949Reputation: 1949Reputation: 1949Reputation: 1949Reputation: 1949Reputation: 1949Reputation: 1949Reputation: 1949Reputation: 1949Reputation: 1949Reputation: 1949
There are other issues too. What happens if you delete multiple files with the same filename? What happens if you want to return a file to a directory that no longer exists?

By the way, I've seen other trashcan scripts on the web, so you might want to search a bit before reinventing the wheel. There's also the libtrash library, which integrates with your system so that your regular rm commands send the files to a trash directory instead. I'm not sure how to go about setting it up, personally.

I haven't seen anything that directly allows for a return trip, however.
 
1 members found this post helpful.
Old 03-30-2011, 12:40 PM   #10
JangoFett353
LQ Newbie
 
Registered: Mar 2011
Posts: 3

Original Poster
Rep: Reputation: 0
I am very new to Linux so I don't think I am even using a file manager. Is that something that comes with my version of Ubuntu or something I can install? I will look into that libtrash David.

Thanks to everybody for their input.
 
Old 03-30-2011, 01:01 PM   #11
Harju
LQ Newbie
 
Registered: Sep 2008
Location: Gothenburg, Sweden
Posts: 25

Rep: Reputation: 1
I recently made (with help from this board) a shellscript that can list the 100 newest files of a folder (recursively). Maybe you can use it, here it is anyways. It's made so that it will paste it into a text-file. Maybe you can use this same command to make a register of where the files was before they got deleted. This way you could make an alias to list your recently deleted files as well. Guess you might want to remove the -type f to list both directories and files as well.

Code:
find . -type f -printf "%TY-%Tm-%Td, %TH:%TM:%TS %p\n" | sort -k1n | tail -100 > $ftpDirMovLs
Hope it helps you on your project, good luck!
 
Old 03-30-2011, 02:37 PM   #12
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1949Reputation: 1949Reputation: 1949Reputation: 1949Reputation: 1949Reputation: 1949Reputation: 1949Reputation: 1949Reputation: 1949Reputation: 1949Reputation: 1949
A "file manager" is just a program that lets you list and manipulate files, like Windows Explorer. There are a bunch of them available on Linux, such as kde's dolphin and konqueror, gnome's nautilus, xfce's thunar, etc. There are also cli file managers too, like mc/midnight commander.
 
Old 03-31-2011, 01:43 AM   #13
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,564

Rep: Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939
On Ubuntu the default is gnomes nautilus (as mentioned by David above). Just as you would use Windows Explorer simply open your home directory from 'Places' in the
top toolbar and then go to your trashbin directory (which by the way, Ubuntu uses a trashbin process by default when you delete something. You actually have
to add another button to right click's to allow a file to be completely deleted)
 
Old 03-31-2011, 11:19 AM   #14
Telengard
Member
 
Registered: Apr 2007
Location: USA
Distribution: Kubuntu 8.04
Posts: 579
Blog Entries: 8

Rep: Reputation: 147Reputation: 147
Quote:
Originally Posted by David the H. View Post
Just noticed this:

No, it's not that simple, because the reversing script doesn't know where the file originally came from.

The only way I can see to do that would be to log the original location somewhere in a permanent text file as it's moved into the trash. Then the reverse script would look in this log in order to learn where to return the file.
Maintaining a database of file names and paths will be a steeper challenge, although it is certainly doable in a Bash script.

One might also consider using tar to store files into trash.tar. By doing so the original path relative to / (root directory) may be preserved within the archive. Again, this is a steeper challenge, but entirely doable.

On the problem of files with the same name, suffixing all trashed file names with the date and time of processing might be one solution. All that is needed is a way to uniqe-ify the file name before it is trashed. I know there is a library or program to do so automatically (possibly related to /tmp/?), but I can't seem to recall much about it.

My advice is don't bother making this script. Use libtrash instead. Unless this is project is intended only to enhance your own learning, I see no reason to re-invent the wheel.
 
  


Reply

Tags
command line, trash, undelete


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Need advice on a script to search many files for list of terms, append hits to list jimmy the saint Programming 1 07-11-2010 03:59 AM
List of deleted files Linux imran111 Linux - Server 3 03-22-2010 05:27 AM
Need Script to own files owned by deleted users KnightHawk Linux - Server 2 03-20-2009 05:48 PM
Need Script to own files owned by deleted users KnightHawk Linux - Server 1 03-20-2009 04:11 PM
List/compare a directory's files' creation and modification dates LittleTrish Linux - Newbie 3 10-22-2007 02:38 PM


All times are GMT -5. The time now is 10:26 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration