Visit Our UNIX and Linux User Community

Moving files to 'trash'

Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Moving files to 'trash'
# 1  
Old 01-17-2009
Moving files to 'trash'

Hi I'm new to Linux, and I'm trying to write a shell script where I remove a specific file (i.e file1 or file1.txt), but i don't actually remove it, i move it to a directory called 'trash' - in vi I created a file with the path /usr/bin/del and in it I have:

mv $file /home/trash, but I'm getting an error message that says missing argument.

I also tried mv *.* /home/trash, which sort of worked and I thought I had got it until I realised it moved ALL the files in the directory I was in to the 'trash' directory...

Any help would be awesome.

# 2  
Old 01-17-2009
Hi Jodi

Methinks you need to read and understand a book on Linux before you wreck your system.

A reminder about filenames:
Long after the creation of Unix came MSDOS from M$.
In MSDOS (which became M$ Windows) the suffix of the filename conveyed the file type. This does not apply to Unix and Linux though there are
informal conventions. The fullstop character is just another character in the filename.

Assuming your script /usr/bin/del contains the one line
mv $file /home/trash
This is not enough.
Unless there is a value in the variable $file , the command will fail.

Unfortunately because you appear to have no basic training I cannot continue to advise.

Last edited by methyl; 01-17-2009 at 08:58 PM.. Reason: typo
# 3  
Old 01-18-2009
I don't care how much training you have, or if you destroy your system/server/etc..

So here we go! Put that in your .bashrc file. An alias (from what I can tell/find) can not have variables, but you can use a function and it acts just like an alias, except a lot more powerful.

The code below accepts one argument.. it tests if the argument provided is a normal file that exists, and if it is, moves it to ../trash (actually thats commented out), if the file is not a normal file, or does not exist, it will say no such file. This code can EASILY be modified, and expanded.. its not that great at all, but I am going to sleep. Have fun!

function delstuff() {
        if [ -f $1 ]; then
                echo "Moving file to trash..."
                # mv $1 ../trash
                echo "No such file.";

# 4  
Old 01-18-2009
Restoring to original directory?

Alright there guys, I have a question, obviously, and that would be -

So I don't completely screw up my system, I made a 'dustbin' directory, and made a script where instead of removing the files completely they moved the unwanted files to that directory. Sort of like Windows moving it to the Recycle bin, I guess. But I was wondering, if anyone knew of a script that I could use to restore the files back to their original directory - I tried to attempt this myself, but I got confused (it happens easily lol) - Any help would be smashing.

# 5  
Old 01-18-2009
It would basically be the same as the delete script..

function restfile() {
    if [ -f ../trash/$1 ]; then
        mv ../trash/$1 .
        echo "That file is not in the trash.";

I would suggest an absolute path, but I am not sure exactly where you have the trash bin. This script can easily be improved.
# 6  
Old 01-18-2009
I have the dustbin file located within the home directory, so /home/dustbin

I tried the code you suggested but it's giving me a list of restore options with different flags

restore 0.4b39 (using libext2fs 1.35 of 28-Feb-2004)
usage: restore -C [-cdlMvVy] [-b blocksize] [-D filesystem] [-f file]
[-F script] [-L limit] [-s fileno]
restore -i [-acdhlmMouvVy] [-A file] [-b blocksize] [-f file]
[-F script] [-Q file] [-s fileno]
restore -P file [-acdhlmMuvVy] [-A file] [-b blocksize]
[-f file] [-F script] [-s fileno] [-X filelist] [file ...]
restore -r [-cdlMuvVy] [-b blocksize] [-f file] [-F script]
[-s fileno] [-T directory]
restore -R [-cdlMuvVy] [-b blocksize] [-f file] [-F script]
[-s fileno] [-T directory]
restore -t [-cdhlMuvVy] [-A file] [-b blocksize] [-f file]
[-F script] [-Q file] [-s fileno] [-X filelist] [file ...]
restore -x [-acdhlmMouvVy] [-A file] [-b blocksize] [-f file]
[-F script] [-Q file] [-s fileno] [-X filelist] [file ...]

Which is double dutch to me.
# 7  
Old 01-18-2009
Alright.. you need to make sure that you put that in your .bash_profile file, then login to the shell again.

You can't name the function restore() as that is an actual command in *nix (not sure which versions it is actually in), you can type: man restore

Name it restfile or undelete or something, I am not sure, but do not use restore. Smilie

Previous Thread | Next Thread
Test Your Knowledge in Computers #948
Difficulty: Easy
A computer virus is a type of computer program that, when executed, replicates itself by modifying other computer programs and inserting its own code.
True or False?

9 More Discussions You Might Find Interesting

1. AIX

Moving Hidden files to normal files

I have a bunch of hidden files in a directory in AIX. I would like to move these hidden files as regular files to another directory. Say i have the following files in directory /x .test~1234~567 .report~5678~123 .find~9876~576 i would like to move them to directory /y as test~1234~567... (10 Replies)
Discussion started by: umesh.narain
10 Replies

2. UNIX for Dummies Questions & Answers

Moving files..

Selected directories on our system generate alerts when they exceed 60% of the disk space so I have used gzip to make the files smaller on one of the directories in question (AdminServer logs). I want to move these to another directory what is the best way to make this happen? Thanks.. (4 Replies)
Discussion started by: nosuchluck
4 Replies

3. Shell Programming and Scripting

Finding files with wc -l results = 1 then moving the files to another folder

Hi guys can you please help me with a script to find files with one row/1 line of content then move the file to another directory my script below runs but nothing happens to the files....Alternatively Ca I get a script to find the *.csv files with "wc -1" results = 1 then create a list of those... (5 Replies)
Discussion started by: Dj Moi
5 Replies

4. UNIX for Dummies Questions & Answers

Moving Multiple files to destination files

I am running a code like this foreach list ($tmp) mv *_${list}.txt ${chart}_${list}.txt #mv: when moving multiple files, last argument must be a directory mv *_${list}.doc ${chart}_${list}.doc #mv: when moving multiple files, last argument must be a... (3 Replies)
Discussion started by: animesharma
3 Replies

5. Shell Programming and Scripting

moving the files in a.txt files to a different directory

HI All, I am coding a shell script which will pick all the .csv files in a particular directoryand write it in to a .txt file, this .txt file i will use as a source in datastage for processing. now after the processing is done I have to move and archive all the files in the .txt file to a... (5 Replies)
Discussion started by: subhasri_2020
5 Replies

6. UNIX for Dummies Questions & Answers

Moving files

Hi I need to be able to move files from one central locations to different servers on our network. So i want all of our operators to place files to one area on the main storage area. From there i need a script that first checks the file is stable (finished copying) then copy to another server,... (5 Replies)
Discussion started by: treds
5 Replies

7. Shell Programming and Scripting

Moving files

I wrote a script which moves files on first in first out basis. for i in `ls -ltr | grep ^- | head -10 | awk '{print $9}'` do mv $i Test/ done But donno some reason, this is not working on my Linux box. May i know the reason? Can the above script be done by using positional... (2 Replies)
Discussion started by: venkatesht
2 Replies

8. Shell Programming and Scripting

Moving Files

Hi There, I am trying to move files, the file is present in this location: /iAm4Free/test/generate/txt/information.txt I need to move it to: /iAm4Free/test1/generate/txt/information.txt The only difference is the "test" is replaced with "test1". But the constraint is. The parent... (5 Replies)
Discussion started by: iAm4Free
5 Replies

9. UNIX for Dummies Questions & Answers

moving only files...

hi.. I want to move a set of files that contain a particular string. I wished to do that with find but i am unable to do that. can anybody give me a good method? :) (12 Replies)
Discussion started by: sskb
12 Replies

Featured Tech Videos