LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
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 12-02-2010, 07:23 AM   #1
bettsy583
LQ Newbie
 
Registered: Dec 2010
Posts: 7

Rep: Reputation: 0
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'


Thanks in advance for help!

Last edited by bettsy583; 12-02-2010 at 04:35 PM.
 
Old 12-02-2010, 07:25 AM   #2
kaushalpatel1982
Member
 
Registered: Aug 2007
Location: INDIA
Distribution: CentOS, RHEL, Fedora, Debian, Ubuntu, LinuxMint, Kali Linux, Raspbian
Posts: 166

Rep: Reputation: 10
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.
 
Old 12-02-2010, 08:17 AM   #3
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
The .Trash system includes meta data about deleted files so the mv command is not equivalent to deleting files in a GUI file manager that uses .Trash
 
Old 12-02-2010, 10:13 AM   #4
bettsy583
LQ Newbie
 
Registered: Dec 2010
Posts: 7

Original Poster
Rep: Reputation: 0
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.
 
Old 12-02-2010, 10:28 AM   #5
i92guboj
Gentoo support team
 
Registered: May 2008
Location: Lucena, Córdoba (Spain)
Distribution: Gentoo
Posts: 4,083

Rep: Reputation: 405Reputation: 405Reputation: 405Reputation: 405Reputation: 405
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?
 
Old 12-02-2010, 10:35 AM   #6
bettsy583
LQ Newbie
 
Registered: Dec 2010
Posts: 7

Original Poster
Rep: Reputation: 0
Ok sorry if I have not been clear, all I need is a script to move a file/directory to either a dustbin directory or the trash folder.

So when I run the script it moves the file I have specified in the script to the dustbin or trash.

Is this any better?????
 
Old 12-02-2010, 10:45 AM   #7
GrapefruiTgirl
LQ Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 556Reputation: 556Reputation: 556Reputation: 556Reputation: 556Reputation: 556
Perhaps something like this simple example:
Code:
#!/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.
 
Old 12-02-2010, 10:49 AM   #8
i92guboj
Gentoo support team
 
Registered: May 2008
Location: Lucena, Córdoba (Spain)
Distribution: Gentoo
Posts: 4,083

Rep: Reputation: 405Reputation: 405Reputation: 405Reputation: 405Reputation: 405
Quote:
Originally Posted by bettsy583 View Post
Ok sorry if I have not been clear, all I need is a script to move a file/directory to either a dustbin directory or the trash folder.

So when I run the script it moves the file I have specified in the script to the dustbin or trash.

Is this any better?????
We understood that part. What we don't get is why do you need an script to do that, when you can just

Code:
$ mv your_file ~/Trash/
If you imperatively feel that you need to create a custom script for that, just put this into a text file and make it executable:

Code:
#!/bin/bash

[ ! -d ~/Trash/ ] && mkdir ~/Trash/
mv "$@" ~/Trash/
Call it "trash", then just run

Code:
./trash thisfile.txt otherfile.log "my file with spaces.jpg"
 
Old 12-02-2010, 11:06 AM   #9
bettsy583
LQ Newbie
 
Registered: Dec 2010
Posts: 7

Original Poster
Rep: Reputation: 0
I have changed it to 'Bucket' as it said 'Trash' already exists. I now get this error

mv: missing destination file operand after `/root/Bucket/'
Try 'mv --help' for more information

Is this because I have not specified which files to move>? If so where in the script to I right this? "$@" >?

Thanks
 
Old 12-02-2010, 11:22 AM   #10
i92guboj
Gentoo support team
 
Registered: May 2008
Location: Lucena, Córdoba (Spain)
Distribution: Gentoo
Posts: 4,083

Rep: Reputation: 405Reputation: 405Reputation: 405Reputation: 405Reputation: 405
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)
 
Old 12-02-2010, 11:24 AM   #11
bettsy583
LQ Newbie
 
Registered: Dec 2010
Posts: 7

Original Poster
Rep: Reputation: 0
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>?
 
Old 12-02-2010, 11:29 AM   #12
GrapefruiTgirl
LQ Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 556Reputation: 556Reputation: 556Reputation: 556Reputation: 556Reputation: 556
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.
 
Old 12-02-2010, 12:24 PM   #13
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
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.
 
Old 12-03-2010, 03:41 AM   #14
pwc101
Senior Member
 
Registered: Oct 2005
Location: UK
Distribution: Slackware
Posts: 1,847

Rep: Reputation: 128Reputation: 128
https://code.google.com/p/trash-cli/

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.
 
  


Reply



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
Write a script to move specific files in various folders to one folder linuxisthedevil Linux - General 13 11-18-2010 06:29 AM
[SOLVED] Bash script: how to move files to Trash instead of deleting? KonfuseKitty Programming 1 07-10-2010 12:44 PM
how to uninstall folders/programs that do not have a move to trash option robert n. Linux - Software 2 08-13-2008 07:34 PM
Can't move files/folders to trash... adred Linux - General 1 06-22-2008 05:40 AM
making a script that will move a file or files in a trash folder Paxmaster Programming 5 12-12-2004 06:00 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 06:07 PM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration