Find the answer to your Linux question:
Results 1 to 9 of 9
in CLI where to get Trash items. suppose startX is not working and i deleted a file.... where to get it???...
  1. #1
    Just Joined! rjayavrp's Avatar
    Join Date
    Apr 2009
    Location
    Incredible India
    Posts
    42

    Question command line RecycleBin

    in CLI where to get Trash items.

    suppose startX is not working and i deleted a file.... where to get it???

  2. #2
    Linux Guru Lakshmipathi's Avatar
    Join Date
    Sep 2006
    Location
    3rd rock from sun - Often seen near moon
    Posts
    1,568

    Exclamation

    only when you delete a file using GUI interface for example , right click and Trash will be displayed on your Trash box and files will be placed under ~/.local/share/Trash.

    If you delete files using rm command,they are same as shift + del , so files are not placed in any directories,they are lost.

    Unless you have some kind of files protection tool( like giis ,if you are using ext3) you can't get them back without proper recovery tools .
    - Lakshmipathi.G
    -------------------
    FOSS India Award winning ext3fs Undelete tool and tutorials www.giis.co.in
    First they criticize you,Then they laugh at you,Then they fight with you,Then you win. - M.K.Gandhi
    -------------------

  3. #3
    Linux Newbie egan's Avatar
    Join Date
    Feb 2009
    Location
    Mountain View, CA
    Posts
    132
    If you are really afraid of rm, you could alias it as copying to the trash.

  4. #4
    Linux User
    Join Date
    Jan 2006
    Posts
    414
    You could always write your own little trash script, something like:
    Code:
    #!/bin/sh
    if [ -f $1]; then
    mv $1 ~/.trash/.;
    elif [ -d $1]; then
    mv -R $1 ~/.trash/.;
    else
    echo "trash: $1: unknown file or directory";
    fi
    exit 0;
    name is trash, put it in /usr/bin and make it executable. Then you can trash things with:
    Code:
    $ trash myfile

  5. #5
    Linux Guru
    Join Date
    Nov 2007
    Location
    Córdoba (Spain)
    Posts
    1,513
    Quote Originally Posted by darkrose0510 View Post
    You could always write your own little trash script, something like:
    Code:
    #!/bin/sh
    if [ -f $1]; then
    mv $1 ~/.trash/.;
    elif [ -d $1]; then
    mv -R $1 ~/.trash/.;
    else
    echo "trash: $1: unknown file or directory";
    fi
    exit 0;
    Tis' weird to say the least

    Ending colons are not needed and I can't tell why do you use it on some lines and not anothers. You only need them to separate two instructions on the same line, like with "if...; then"

    Another thing, please, when dealing with filenames, always quote them. Otherwise you are going to get lots of fun each time that a file name has spaces in the middle. The dots at the end of a path are useless as well. Redundant. You are already on that directory, you don't need to emphasize it any more.

    Another small thing, this:

    Code:
    if [ -f $1]; then
    Is incorrect even if we obviate all the above. "]" must be a separate token, you missed a blank space in between "$1" and "]".

    Finally, mv has no "-R" flag. It already moves recursively directories. Which in turn means that the two cases that you separated really need no separation (unless you really want to treat directories in a different way to get some extra security. I assume that the answer to that is "no", so I will also add that you need write permissions on them because you are actually moving them to another place.

    Adding it all, this would be the -syntactically correct- version of your script.

    Code:
    #!/bin/sh
    
    if [ -w "$1" ] && [ -r "$1" ]; then
      mv "$1" ~/.trash/
    else
      echo "$0: $1: unknown file or directory"
    fi
    
    exit 0
    I would add some extra magic to check if the trash folder is writable, just in case. But that's just me.

    Note the usage of "$0", which is expanded to the name of the script. Not a big deal, but that way you won't get confused if you save the script with a different name.

  6. #6
    Linux User
    Join Date
    Jan 2006
    Posts
    414
    heh, yeah I don't do much shell scripting as you can tell, just threw that together quickly as an idea of what could be done

    As for the dots at the ends of paths, just habit incase some obscure shell decides to move things and rename them trash instead of putting them in the directory... used to happen on some old unix boxen.

  7. #7
    Linux User saivin's Avatar
    Join Date
    Dec 2008
    Location
    Bengaluru, India
    Posts
    305
    hmm... wanted to ask about the ending dot but found it answered...

    Also, $1 is only for first filename. What if we want to delete multiple files? I used 'for loop' in the above code to achieve this. But is there any other method?
    Code:
    #/bin/bash
    # Script to put files in trash in traditional 'Del' fashion than to 
    # remove it completely in 'Shft+Del' fashion.
    
    for i in "$@"
    do
    if [ -w "$i" ] && [ -r "$i" ] ; then
       mv "$i" ~/.trash/
    else
       echo "File or directory $i does not exit."
    fi
    done
    exit 0
    A candle looses nothing by lighting other candles. - Khalil Zibran.
    Registered Linux User #490076

  8. #8
    Linux Guru
    Join Date
    Nov 2007
    Location
    Córdoba (Spain)
    Posts
    1,513
    Quote Originally Posted by saivin View Post
    hmm... wanted to ask about the ending dot but found it answered...

    Also, $1 is only for first filename. What if we want to delete multiple files? I used 'for loop' in the above code to achieve this. But is there any other method?
    Code:
    #/bin/bash
    # Script to put files in trash in traditional 'Del' fashion than to 
    # remove it completely in 'Shft+Del' fashion.
    
    for i in "$@"
    do
    if [ -w "$i" ] && [ -r "$i" ] ; then
       mv "$i" ~/.trash/
    else
       echo "File or directory $i does not exit."
    fi
    done
    exit 0
    There lots of methods You could use $# along with shift so you don't need an $i. Something in the lines of

    Code:
    while [ $# -ne 0 ]
    do
      if [ -w "$1" ] && [ -r "$1" ] ; then
        mv "$1" ~/.trash/
        shilft
      else
        echo "File or directory $1 does not exit."
        exit 1
      fi
    done
    Shift acts like in DOS bat scripts. It discards the first positional parameter and "shifts" the rest of them to the left so $2 becomes $1. $# holds the number of parameters at a given time, when it's zero there's nothing else to look at. We iterate through the list and look always at $1, we don't need $i If a file doesn't exist, we exit with error (1).

  9. #9
    Linux User saivin's Avatar
    Join Date
    Dec 2008
    Location
    Bengaluru, India
    Posts
    305
    Oh yes, totally forgot the 'shift'ing. Gracias i92guboj
    A candle looses nothing by lighting other candles. - Khalil Zibran.
    Registered Linux User #490076

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...