[SOLVED] I need help with a script to list deleted files with dates
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.
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.
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.
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.
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.
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
Hello,
I created a script to move files to a "trashbin" This is my code:
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.
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.
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 "'"!
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
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.
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.
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?
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.
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.
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.
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.
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)
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.