BASH script to move files/folders to either the .Trash file or to another direcotry
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.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
BASH script to move files/folders to either the .Trash file or to another direcotry
Completely new to Linux, I am looking for a script/advice or guidance on how to write a script so that when I use the 'del' command it removes/sends the files/folders to a I specify for example 'dustbin'
Distribution: CentOS, RHEL, Fedora, Debian, Ubuntu, LinuxMint, Kali Linux, Raspbian
Posts: 166
Rep:
MV is the command that helps you to do this task. I don't know why you require the script but MV is the command that I personally use it and it is not difficult to use.
Cheers guys for the input, basically I need the script to make Linux a bit more like Windows with 'housekeeping' for files.
So basically I need to either create a directory called 'trash' 'dustbin' 'bucket' or something like that, I then need the script to move files and folders to that directory. I am not really to bothered about the script creating the directory because I can create that in the GUI.
In regards to CatKin, could I use the 'trash' folder on the desktop to store these file when I have 'deleted' them?
I am completely new to this and would appreciate some advice/help.
Well, the answer to this question really depends on what exactly do you want, and how nice and clean do you want the solution to be.
As others have already said, you don't need to write any script if all you want is to move a file into a folder, and that fact doesn't change just because the destination folder is called "~/.Trash/" instead of "/my/foo/bar/". To move files or directories you use "mv" as the standard tool. There are many more that can do the work. In addition, most GUI file navigators will send the deleted files into that folder by default without you needing to do anything.
So, I don't understand what your purpose is.
Explain a bit better what do you exactly mean.
What actions would the user do, *HOW* would s/he do the actions, and what would the ideal results be when doing such actions?
#!/bin/bash
[ ! -d ~/.Trash ] && mkdir ~/.Trash
for each in ${@} ; do
mv -vb "$each" ~/.Trash
done
you could save it as /usr/bin/trash or some name you like that doesn't exist, and then call it like this:
Code:
shell$ trash file1 file2 file3
and it would send the 3 files to the ~/.Trash directory. Note that there's extremely little error-checking or duplicate checking, so files will eventually get overwritten inside the .Trash folder. Also, you *could* put the same code as above, into a function inside your .bashrc or /etc/profile (system-wide), which would save having this script lying around somewhere.
I have not tested the example here, so it may need improvements or repairs. And, it doesn't have any provision for filenames with spaces in the input, so you'd need to "quote" those.
You should read the bash man page if you plan on doing shell scripting (or the man page for whatever shell you prefer).
In bash, "$@" expands to a list containing all the positional parameters: [ "$1" "$2" .... "$n" ], being "n" the number of parameters you supply.
A few things:
if this script is meant to be run as a regular user (non-root) you probably don't have write permissions under /root/, choose a world-writable directory instead.
if you don't supply parameters it will fail, this script is quite basic and doesn't do any checks nor does it provide any help in such case, that's left as an exercise to you if you are really interested in scripting
it doesn't do any checks either for the correctness of the input parameter(s)
GrapefruiTgirl, when you say 'you could save it as /usr/bin/trash' do you mean I could save the script in there? How do I navigate to that folder to put the script there>?
One way would be to start nano editor as root (or whatever editor you like), enter the script, and save it as /usr/bin/trash. Another method would be to compose the script as your regular user, save it, then change to root and copy or move it into /usr/bin (or whatever location you wish).
You need to be root, to be able to write/save files into root-owned directories like /usr/bin.
Note that the final script, whatever and wherever you call it and put it, should be set to "executable" - see the man page for the `chmod` command to learn about this.
EDIT: P.S. - you should also be aware that the "~" symbol (the tilde) which we have been using in the above scripts, will expand to the home directory of the user running the script. That means that each unique user who uses the `trash` script, will end up with his own ~/.Trash folder (or whatever you called it).
Last edited by GrapefruiTgirl; 12-02-2010 at 11:33 AM.
If you search LQ you'll find there's 'rm2thrashcan' scripts all over the place. That however does not necessarily mean using it is a Good Idea. Instead of reproducing things I'll just recycle one of my posts on the subject http://www.linuxquestions.org/questi...9/#post3987860 if you don't mind.
You can then use the command "trash" to move files to the same Trash bin as your file manager uses. It also has restore-trash and empty-trash, which are probably quite self-explanatory.
This project will probably be packaged up by your distro, whatever that may be.
I don't use this, so can't vouch for its functionality, but it does seem to fulfill the OP's requirements.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.