Command to move a file to Trash via Terminal












111















I would like to know if there is a command I can issue in a terminal so I do not classically remove (rm) the file, but instead move it to trash (i.e. Nautilus Move to Trash behavior).



In case there is such a command, I would also be interested in knowing what it is.










share|improve this question




















  • 2





    Have a look at this answer.

    – Peachy
    Nov 6 '12 at 8:49








  • 1





    Also a good resource here: webupd8.org/2010/02/make-rm-move-files-to-trash-instead-of.html

    – Rinzwind
    Nov 6 '12 at 8:52
















111















I would like to know if there is a command I can issue in a terminal so I do not classically remove (rm) the file, but instead move it to trash (i.e. Nautilus Move to Trash behavior).



In case there is such a command, I would also be interested in knowing what it is.










share|improve this question




















  • 2





    Have a look at this answer.

    – Peachy
    Nov 6 '12 at 8:49








  • 1





    Also a good resource here: webupd8.org/2010/02/make-rm-move-files-to-trash-instead-of.html

    – Rinzwind
    Nov 6 '12 at 8:52














111












111








111


31






I would like to know if there is a command I can issue in a terminal so I do not classically remove (rm) the file, but instead move it to trash (i.e. Nautilus Move to Trash behavior).



In case there is such a command, I would also be interested in knowing what it is.










share|improve this question
















I would like to know if there is a command I can issue in a terminal so I do not classically remove (rm) the file, but instead move it to trash (i.e. Nautilus Move to Trash behavior).



In case there is such a command, I would also be interested in knowing what it is.







command-line files trash






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jun 26 '18 at 17:52









Dawoodjee

197215




197215










asked Nov 6 '12 at 8:46









RasmusRasmus

3,71492852




3,71492852








  • 2





    Have a look at this answer.

    – Peachy
    Nov 6 '12 at 8:49








  • 1





    Also a good resource here: webupd8.org/2010/02/make-rm-move-files-to-trash-instead-of.html

    – Rinzwind
    Nov 6 '12 at 8:52














  • 2





    Have a look at this answer.

    – Peachy
    Nov 6 '12 at 8:49








  • 1





    Also a good resource here: webupd8.org/2010/02/make-rm-move-files-to-trash-instead-of.html

    – Rinzwind
    Nov 6 '12 at 8:52








2




2





Have a look at this answer.

– Peachy
Nov 6 '12 at 8:49







Have a look at this answer.

– Peachy
Nov 6 '12 at 8:49






1




1





Also a good resource here: webupd8.org/2010/02/make-rm-move-files-to-trash-instead-of.html

– Rinzwind
Nov 6 '12 at 8:52





Also a good resource here: webupd8.org/2010/02/make-rm-move-files-to-trash-instead-of.html

– Rinzwind
Nov 6 '12 at 8:52










7 Answers
7






active

oldest

votes


















100














You can use gvfs-trash command from the package gvfs-bin which is installed by default in Ubuntu.



Move file to trash:



gvfs-trash filename


See the content of the trash:



gvfs-ls trash://


Empty the trash:



gvfs-trash --empty





share|improve this answer
























  • Also visit my gvfs-question.

    – Pandya
    Sep 17 '14 at 14:47













  • This is the simplest answer for me that works. Thank you.

    – Teody C. Seguin
    Aug 2 '17 at 10:55






  • 4





    According to man gvfs-trash it is deprecated in favour of gio trash, see man gio.

    – pbhj
    Sep 1 '18 at 21:14



















63














Install trash-cli Install trash-clisudo apt-get install trash-cli



Put files in the trash with: trash file1 file2



List files in trash: trash-list



Empty trash with: trash-empty






share|improve this answer


























  • That (Ubuntu-related) tool points forward to a trash spec. Pretty interesting, not sure how widely adopted, though...

    – Frank Nocke
    Aug 15 '17 at 17:59











  • After installation, I run the command and get the error: File "/usr/bin/trash-list", line 4, in <module> ImportError: No module named 'trashcli'

    – Daniel
    Aug 1 '18 at 8:48





















22














As of 2017, gvfs-trash seems to be deprecated.



$ touch test
$ gvfs-trash test
This tool has been deprecated, use 'gio trash' instead.
See 'gio help trash' for more info.


You should use gio, specifically



gio trash


is the recommended way.






share|improve this answer





















  • 2





    Could you link a source for gvfs-trash being deprecated and what gio is?

    – Melebius
    Dec 11 '17 at 9:04






  • 1





    I can't provide a link unfortunately, but this is what I get trying to use gvfs-trash on Kubuntu 17.10: pastebin.com/HA4a1pbs

    – Eugen Tverdokhleb
    Jan 10 '18 at 19:56








  • 1





    You could paste the example here in your answer, it would be sufficient for me together with system version number. I am using 16.04 LTS and gvfs-trash is the only option here.

    – Melebius
    Jan 10 '18 at 20:50











  • This tool has a bunch of other nice features. I like the info command; it seems useful.

    – Raffi Khatchadourian
    Aug 2 '18 at 18:35



















2














I like the low tech ways the best. I made a folder .Tr in my home directory by typing:



mkdir ~/.Tr


and instead of using rm to delete files, I move those files to the ~/.Tr directory by typing:



mv fileName ~/.Tr


This is an effective and simple way of keeping access to files you think you don't want with the added benefit in my case of not messing with the system's folders, as my Ubuntu knowledge levels are fairly low and I worry about what I might be screwing up when I mess with system stuff. If you are also low level please note that the "." in the directory name makes it a hidden directory.






share|improve this answer

































    2














    A previous answer mentions the command gio trash, which is fine as far as it goes. However, on server machines, there is no equivalent of a trash directory. I've written a Bash script that does the job; on (Ubuntu) desktop machines, it uses gio trash. (I've added alias tt='move-to-trash' to my alias definitions file; tt is a mnemonic for "to trash".)



    #!/bin/bash
    # move-to-trash

    # Teemu Leisti 2018-07-08

    # This script moves the files given as arguments to the trash directory, if they
    # are not already there. It works both on (Ubuntu) desktop and server hosts.
    #
    # The script is intended as a command-line equivalent of deleting a file from a
    # graphical file manager, which, in the usual case, moves the deleted file(s) to
    # a built-in trash directory. On server hosts, the analogy is not perfect, as
    # the script does not offer the functionalities of restoring a trashed file to
    # its original location nor of emptying the trash directory; rather, it is an
    # alternative to the 'rm' command that offers the user the peace of mind that
    # they can still undo an unintended deletion before they empty the trash
    # directory.
    #
    # To determine whether it's running on a desktop host, the script tests for the
    # existence of directory ~/.local/share/Trash. In case it is, the script relies
    # on the 'gio trash' command.
    #
    # When not running on a desktop host, there is no built-in trash directory, so
    # the first invocation of the script creates one: ~/.Trash/. It will not
    # overwrite an existing file in that directory; instead, in case a file given as
    # an argument already exists in the custom trash directory, the script first
    # appends a timestamp to the filename, with millisecond resolution, such that no
    # existing file will be overwritten.
    #
    # The script will not choke on a nonexistent file. It outputs the final
    # disposition of each argument: does not exist, was already in trash, or was
    # moved to the trash.


    # Exit on using an uninitialized variable, and on a command returning an error.
    # (The latter setting necessitates appending " || true" to those arithmetic
    # calculations that can result in a value of 0, lest bash interpret the result
    # as signalling an error.)
    set -eu

    is_desktop=0

    if [[ -d ~/.local/share/Trash ]] ; then
    is_desktop=1
    trash_dir_abspath=$(realpath ~/.local/share/Trash)
    else
    trash_dir_abspath=$(realpath ~/.Trash)
    if [[ -e $trash_dir_abspath ]] ; then
    if [[ ! -d $trash_dir_abspath ]] ; then
    echo "The file $trash_dir_abspath exists, but is not a directory. Exiting."
    exit 1
    fi
    else
    mkdir $trash_dir_abspath
    echo "Created directory $trash_dir_abspath"
    fi
    fi

    for file in "$@" ; do
    file_abspath=$(realpath -- "$file")
    file_basename=$( basename -- "$file_abspath" )
    if [[ ! -e $file_abspath ]] ; then
    echo "does not exist: $file_abspath"
    elif [[ "$file_abspath" == "$trash_dir_abspath"* ]] ; then
    echo "already in trash: $file_abspath"
    else
    if (( is_desktop == 1 )) ; then
    gio trash "$file_abspath" || true
    else
    move_to_abspath="$trash_dir_abspath/$file_basename"
    while [[ -e "$move_to_abspath" ]] ; do
    move_to_abspath="$trash_dir_abspath/$file_basename-"$(date '+%Y-%m-%d-at-%H:%M:%S.%3N')
    done
    # While we're reasonably sure that the file at $move_to_abspath does not exist, we shall
    # use the '-f' (force) flag in the 'mv' command anyway, to be sure that moving the file
    # to the trash directory is successful even in the extremely unlikely case that due to a
    # run condition, some other thread has created the file $move_to_abspath after the
    # execution of the while test above.
    /bin/mv -f "$file_abspath" "$move_to_abspath"
    fi
    echo "moved to trash: $file_abspath"
    fi
    done





    share|improve this answer

































      1














      Here is a open source nodejs-based version (if you want to know, what happens under the hood, or need this in a project), that also has command line support (if you are happy, if it just works.



      > trash pictures/beach.jpg





      share|improve this answer































        0














        Updating @Radu Rădeanu answer. Since Ubuntu is telling me to use gio instead...



        So, to trash some_file (or folder) use



        gio trash some_file


        To go dumpster diving use



        gio list trash://


        To empty trash



        gio trash --empty





        share|improve this answer























          Your Answer








          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "89"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f213533%2fcommand-to-move-a-file-to-trash-via-terminal%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          7 Answers
          7






          active

          oldest

          votes








          7 Answers
          7






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          100














          You can use gvfs-trash command from the package gvfs-bin which is installed by default in Ubuntu.



          Move file to trash:



          gvfs-trash filename


          See the content of the trash:



          gvfs-ls trash://


          Empty the trash:



          gvfs-trash --empty





          share|improve this answer
























          • Also visit my gvfs-question.

            – Pandya
            Sep 17 '14 at 14:47













          • This is the simplest answer for me that works. Thank you.

            – Teody C. Seguin
            Aug 2 '17 at 10:55






          • 4





            According to man gvfs-trash it is deprecated in favour of gio trash, see man gio.

            – pbhj
            Sep 1 '18 at 21:14
















          100














          You can use gvfs-trash command from the package gvfs-bin which is installed by default in Ubuntu.



          Move file to trash:



          gvfs-trash filename


          See the content of the trash:



          gvfs-ls trash://


          Empty the trash:



          gvfs-trash --empty





          share|improve this answer
























          • Also visit my gvfs-question.

            – Pandya
            Sep 17 '14 at 14:47













          • This is the simplest answer for me that works. Thank you.

            – Teody C. Seguin
            Aug 2 '17 at 10:55






          • 4





            According to man gvfs-trash it is deprecated in favour of gio trash, see man gio.

            – pbhj
            Sep 1 '18 at 21:14














          100












          100








          100







          You can use gvfs-trash command from the package gvfs-bin which is installed by default in Ubuntu.



          Move file to trash:



          gvfs-trash filename


          See the content of the trash:



          gvfs-ls trash://


          Empty the trash:



          gvfs-trash --empty





          share|improve this answer













          You can use gvfs-trash command from the package gvfs-bin which is installed by default in Ubuntu.



          Move file to trash:



          gvfs-trash filename


          See the content of the trash:



          gvfs-ls trash://


          Empty the trash:



          gvfs-trash --empty






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Sep 17 '14 at 6:21









          Radu RădeanuRadu Rădeanu

          119k35251326




          119k35251326













          • Also visit my gvfs-question.

            – Pandya
            Sep 17 '14 at 14:47













          • This is the simplest answer for me that works. Thank you.

            – Teody C. Seguin
            Aug 2 '17 at 10:55






          • 4





            According to man gvfs-trash it is deprecated in favour of gio trash, see man gio.

            – pbhj
            Sep 1 '18 at 21:14



















          • Also visit my gvfs-question.

            – Pandya
            Sep 17 '14 at 14:47













          • This is the simplest answer for me that works. Thank you.

            – Teody C. Seguin
            Aug 2 '17 at 10:55






          • 4





            According to man gvfs-trash it is deprecated in favour of gio trash, see man gio.

            – pbhj
            Sep 1 '18 at 21:14

















          Also visit my gvfs-question.

          – Pandya
          Sep 17 '14 at 14:47







          Also visit my gvfs-question.

          – Pandya
          Sep 17 '14 at 14:47















          This is the simplest answer for me that works. Thank you.

          – Teody C. Seguin
          Aug 2 '17 at 10:55





          This is the simplest answer for me that works. Thank you.

          – Teody C. Seguin
          Aug 2 '17 at 10:55




          4




          4





          According to man gvfs-trash it is deprecated in favour of gio trash, see man gio.

          – pbhj
          Sep 1 '18 at 21:14





          According to man gvfs-trash it is deprecated in favour of gio trash, see man gio.

          – pbhj
          Sep 1 '18 at 21:14













          63














          Install trash-cli Install trash-clisudo apt-get install trash-cli



          Put files in the trash with: trash file1 file2



          List files in trash: trash-list



          Empty trash with: trash-empty






          share|improve this answer


























          • That (Ubuntu-related) tool points forward to a trash spec. Pretty interesting, not sure how widely adopted, though...

            – Frank Nocke
            Aug 15 '17 at 17:59











          • After installation, I run the command and get the error: File "/usr/bin/trash-list", line 4, in <module> ImportError: No module named 'trashcli'

            – Daniel
            Aug 1 '18 at 8:48


















          63














          Install trash-cli Install trash-clisudo apt-get install trash-cli



          Put files in the trash with: trash file1 file2



          List files in trash: trash-list



          Empty trash with: trash-empty






          share|improve this answer


























          • That (Ubuntu-related) tool points forward to a trash spec. Pretty interesting, not sure how widely adopted, though...

            – Frank Nocke
            Aug 15 '17 at 17:59











          • After installation, I run the command and get the error: File "/usr/bin/trash-list", line 4, in <module> ImportError: No module named 'trashcli'

            – Daniel
            Aug 1 '18 at 8:48
















          63












          63








          63







          Install trash-cli Install trash-clisudo apt-get install trash-cli



          Put files in the trash with: trash file1 file2



          List files in trash: trash-list



          Empty trash with: trash-empty






          share|improve this answer















          Install trash-cli Install trash-clisudo apt-get install trash-cli



          Put files in the trash with: trash file1 file2



          List files in trash: trash-list



          Empty trash with: trash-empty







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Mar 11 '17 at 19:03









          Community

          1




          1










          answered Nov 6 '12 at 9:12









          user55822user55822

          2,40911213




          2,40911213













          • That (Ubuntu-related) tool points forward to a trash spec. Pretty interesting, not sure how widely adopted, though...

            – Frank Nocke
            Aug 15 '17 at 17:59











          • After installation, I run the command and get the error: File "/usr/bin/trash-list", line 4, in <module> ImportError: No module named 'trashcli'

            – Daniel
            Aug 1 '18 at 8:48





















          • That (Ubuntu-related) tool points forward to a trash spec. Pretty interesting, not sure how widely adopted, though...

            – Frank Nocke
            Aug 15 '17 at 17:59











          • After installation, I run the command and get the error: File "/usr/bin/trash-list", line 4, in <module> ImportError: No module named 'trashcli'

            – Daniel
            Aug 1 '18 at 8:48



















          That (Ubuntu-related) tool points forward to a trash spec. Pretty interesting, not sure how widely adopted, though...

          – Frank Nocke
          Aug 15 '17 at 17:59





          That (Ubuntu-related) tool points forward to a trash spec. Pretty interesting, not sure how widely adopted, though...

          – Frank Nocke
          Aug 15 '17 at 17:59













          After installation, I run the command and get the error: File "/usr/bin/trash-list", line 4, in <module> ImportError: No module named 'trashcli'

          – Daniel
          Aug 1 '18 at 8:48







          After installation, I run the command and get the error: File "/usr/bin/trash-list", line 4, in <module> ImportError: No module named 'trashcli'

          – Daniel
          Aug 1 '18 at 8:48













          22














          As of 2017, gvfs-trash seems to be deprecated.



          $ touch test
          $ gvfs-trash test
          This tool has been deprecated, use 'gio trash' instead.
          See 'gio help trash' for more info.


          You should use gio, specifically



          gio trash


          is the recommended way.






          share|improve this answer





















          • 2





            Could you link a source for gvfs-trash being deprecated and what gio is?

            – Melebius
            Dec 11 '17 at 9:04






          • 1





            I can't provide a link unfortunately, but this is what I get trying to use gvfs-trash on Kubuntu 17.10: pastebin.com/HA4a1pbs

            – Eugen Tverdokhleb
            Jan 10 '18 at 19:56








          • 1





            You could paste the example here in your answer, it would be sufficient for me together with system version number. I am using 16.04 LTS and gvfs-trash is the only option here.

            – Melebius
            Jan 10 '18 at 20:50











          • This tool has a bunch of other nice features. I like the info command; it seems useful.

            – Raffi Khatchadourian
            Aug 2 '18 at 18:35
















          22














          As of 2017, gvfs-trash seems to be deprecated.



          $ touch test
          $ gvfs-trash test
          This tool has been deprecated, use 'gio trash' instead.
          See 'gio help trash' for more info.


          You should use gio, specifically



          gio trash


          is the recommended way.






          share|improve this answer





















          • 2





            Could you link a source for gvfs-trash being deprecated and what gio is?

            – Melebius
            Dec 11 '17 at 9:04






          • 1





            I can't provide a link unfortunately, but this is what I get trying to use gvfs-trash on Kubuntu 17.10: pastebin.com/HA4a1pbs

            – Eugen Tverdokhleb
            Jan 10 '18 at 19:56








          • 1





            You could paste the example here in your answer, it would be sufficient for me together with system version number. I am using 16.04 LTS and gvfs-trash is the only option here.

            – Melebius
            Jan 10 '18 at 20:50











          • This tool has a bunch of other nice features. I like the info command; it seems useful.

            – Raffi Khatchadourian
            Aug 2 '18 at 18:35














          22












          22








          22







          As of 2017, gvfs-trash seems to be deprecated.



          $ touch test
          $ gvfs-trash test
          This tool has been deprecated, use 'gio trash' instead.
          See 'gio help trash' for more info.


          You should use gio, specifically



          gio trash


          is the recommended way.






          share|improve this answer















          As of 2017, gvfs-trash seems to be deprecated.



          $ touch test
          $ gvfs-trash test
          This tool has been deprecated, use 'gio trash' instead.
          See 'gio help trash' for more info.


          You should use gio, specifically



          gio trash


          is the recommended way.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Jan 24 '18 at 7:54









          Melebius

          4,86751939




          4,86751939










          answered Oct 29 '17 at 10:49









          Eugen TverdokhlebEugen Tverdokhleb

          32124




          32124








          • 2





            Could you link a source for gvfs-trash being deprecated and what gio is?

            – Melebius
            Dec 11 '17 at 9:04






          • 1





            I can't provide a link unfortunately, but this is what I get trying to use gvfs-trash on Kubuntu 17.10: pastebin.com/HA4a1pbs

            – Eugen Tverdokhleb
            Jan 10 '18 at 19:56








          • 1





            You could paste the example here in your answer, it would be sufficient for me together with system version number. I am using 16.04 LTS and gvfs-trash is the only option here.

            – Melebius
            Jan 10 '18 at 20:50











          • This tool has a bunch of other nice features. I like the info command; it seems useful.

            – Raffi Khatchadourian
            Aug 2 '18 at 18:35














          • 2





            Could you link a source for gvfs-trash being deprecated and what gio is?

            – Melebius
            Dec 11 '17 at 9:04






          • 1





            I can't provide a link unfortunately, but this is what I get trying to use gvfs-trash on Kubuntu 17.10: pastebin.com/HA4a1pbs

            – Eugen Tverdokhleb
            Jan 10 '18 at 19:56








          • 1





            You could paste the example here in your answer, it would be sufficient for me together with system version number. I am using 16.04 LTS and gvfs-trash is the only option here.

            – Melebius
            Jan 10 '18 at 20:50











          • This tool has a bunch of other nice features. I like the info command; it seems useful.

            – Raffi Khatchadourian
            Aug 2 '18 at 18:35








          2




          2





          Could you link a source for gvfs-trash being deprecated and what gio is?

          – Melebius
          Dec 11 '17 at 9:04





          Could you link a source for gvfs-trash being deprecated and what gio is?

          – Melebius
          Dec 11 '17 at 9:04




          1




          1





          I can't provide a link unfortunately, but this is what I get trying to use gvfs-trash on Kubuntu 17.10: pastebin.com/HA4a1pbs

          – Eugen Tverdokhleb
          Jan 10 '18 at 19:56







          I can't provide a link unfortunately, but this is what I get trying to use gvfs-trash on Kubuntu 17.10: pastebin.com/HA4a1pbs

          – Eugen Tverdokhleb
          Jan 10 '18 at 19:56






          1




          1





          You could paste the example here in your answer, it would be sufficient for me together with system version number. I am using 16.04 LTS and gvfs-trash is the only option here.

          – Melebius
          Jan 10 '18 at 20:50





          You could paste the example here in your answer, it would be sufficient for me together with system version number. I am using 16.04 LTS and gvfs-trash is the only option here.

          – Melebius
          Jan 10 '18 at 20:50













          This tool has a bunch of other nice features. I like the info command; it seems useful.

          – Raffi Khatchadourian
          Aug 2 '18 at 18:35





          This tool has a bunch of other nice features. I like the info command; it seems useful.

          – Raffi Khatchadourian
          Aug 2 '18 at 18:35











          2














          I like the low tech ways the best. I made a folder .Tr in my home directory by typing:



          mkdir ~/.Tr


          and instead of using rm to delete files, I move those files to the ~/.Tr directory by typing:



          mv fileName ~/.Tr


          This is an effective and simple way of keeping access to files you think you don't want with the added benefit in my case of not messing with the system's folders, as my Ubuntu knowledge levels are fairly low and I worry about what I might be screwing up when I mess with system stuff. If you are also low level please note that the "." in the directory name makes it a hidden directory.






          share|improve this answer






























            2














            I like the low tech ways the best. I made a folder .Tr in my home directory by typing:



            mkdir ~/.Tr


            and instead of using rm to delete files, I move those files to the ~/.Tr directory by typing:



            mv fileName ~/.Tr


            This is an effective and simple way of keeping access to files you think you don't want with the added benefit in my case of not messing with the system's folders, as my Ubuntu knowledge levels are fairly low and I worry about what I might be screwing up when I mess with system stuff. If you are also low level please note that the "." in the directory name makes it a hidden directory.






            share|improve this answer




























              2












              2








              2







              I like the low tech ways the best. I made a folder .Tr in my home directory by typing:



              mkdir ~/.Tr


              and instead of using rm to delete files, I move those files to the ~/.Tr directory by typing:



              mv fileName ~/.Tr


              This is an effective and simple way of keeping access to files you think you don't want with the added benefit in my case of not messing with the system's folders, as my Ubuntu knowledge levels are fairly low and I worry about what I might be screwing up when I mess with system stuff. If you are also low level please note that the "." in the directory name makes it a hidden directory.






              share|improve this answer















              I like the low tech ways the best. I made a folder .Tr in my home directory by typing:



              mkdir ~/.Tr


              and instead of using rm to delete files, I move those files to the ~/.Tr directory by typing:



              mv fileName ~/.Tr


              This is an effective and simple way of keeping access to files you think you don't want with the added benefit in my case of not messing with the system's folders, as my Ubuntu knowledge levels are fairly low and I worry about what I might be screwing up when I mess with system stuff. If you are also low level please note that the "." in the directory name makes it a hidden directory.







              share|improve this answer














              share|improve this answer



              share|improve this answer








              edited Sep 15 '17 at 19:14









              wjandrea

              9,29842664




              9,29842664










              answered Sep 25 '15 at 17:29









              user2981989user2981989

              714




              714























                  2














                  A previous answer mentions the command gio trash, which is fine as far as it goes. However, on server machines, there is no equivalent of a trash directory. I've written a Bash script that does the job; on (Ubuntu) desktop machines, it uses gio trash. (I've added alias tt='move-to-trash' to my alias definitions file; tt is a mnemonic for "to trash".)



                  #!/bin/bash
                  # move-to-trash

                  # Teemu Leisti 2018-07-08

                  # This script moves the files given as arguments to the trash directory, if they
                  # are not already there. It works both on (Ubuntu) desktop and server hosts.
                  #
                  # The script is intended as a command-line equivalent of deleting a file from a
                  # graphical file manager, which, in the usual case, moves the deleted file(s) to
                  # a built-in trash directory. On server hosts, the analogy is not perfect, as
                  # the script does not offer the functionalities of restoring a trashed file to
                  # its original location nor of emptying the trash directory; rather, it is an
                  # alternative to the 'rm' command that offers the user the peace of mind that
                  # they can still undo an unintended deletion before they empty the trash
                  # directory.
                  #
                  # To determine whether it's running on a desktop host, the script tests for the
                  # existence of directory ~/.local/share/Trash. In case it is, the script relies
                  # on the 'gio trash' command.
                  #
                  # When not running on a desktop host, there is no built-in trash directory, so
                  # the first invocation of the script creates one: ~/.Trash/. It will not
                  # overwrite an existing file in that directory; instead, in case a file given as
                  # an argument already exists in the custom trash directory, the script first
                  # appends a timestamp to the filename, with millisecond resolution, such that no
                  # existing file will be overwritten.
                  #
                  # The script will not choke on a nonexistent file. It outputs the final
                  # disposition of each argument: does not exist, was already in trash, or was
                  # moved to the trash.


                  # Exit on using an uninitialized variable, and on a command returning an error.
                  # (The latter setting necessitates appending " || true" to those arithmetic
                  # calculations that can result in a value of 0, lest bash interpret the result
                  # as signalling an error.)
                  set -eu

                  is_desktop=0

                  if [[ -d ~/.local/share/Trash ]] ; then
                  is_desktop=1
                  trash_dir_abspath=$(realpath ~/.local/share/Trash)
                  else
                  trash_dir_abspath=$(realpath ~/.Trash)
                  if [[ -e $trash_dir_abspath ]] ; then
                  if [[ ! -d $trash_dir_abspath ]] ; then
                  echo "The file $trash_dir_abspath exists, but is not a directory. Exiting."
                  exit 1
                  fi
                  else
                  mkdir $trash_dir_abspath
                  echo "Created directory $trash_dir_abspath"
                  fi
                  fi

                  for file in "$@" ; do
                  file_abspath=$(realpath -- "$file")
                  file_basename=$( basename -- "$file_abspath" )
                  if [[ ! -e $file_abspath ]] ; then
                  echo "does not exist: $file_abspath"
                  elif [[ "$file_abspath" == "$trash_dir_abspath"* ]] ; then
                  echo "already in trash: $file_abspath"
                  else
                  if (( is_desktop == 1 )) ; then
                  gio trash "$file_abspath" || true
                  else
                  move_to_abspath="$trash_dir_abspath/$file_basename"
                  while [[ -e "$move_to_abspath" ]] ; do
                  move_to_abspath="$trash_dir_abspath/$file_basename-"$(date '+%Y-%m-%d-at-%H:%M:%S.%3N')
                  done
                  # While we're reasonably sure that the file at $move_to_abspath does not exist, we shall
                  # use the '-f' (force) flag in the 'mv' command anyway, to be sure that moving the file
                  # to the trash directory is successful even in the extremely unlikely case that due to a
                  # run condition, some other thread has created the file $move_to_abspath after the
                  # execution of the while test above.
                  /bin/mv -f "$file_abspath" "$move_to_abspath"
                  fi
                  echo "moved to trash: $file_abspath"
                  fi
                  done





                  share|improve this answer






























                    2














                    A previous answer mentions the command gio trash, which is fine as far as it goes. However, on server machines, there is no equivalent of a trash directory. I've written a Bash script that does the job; on (Ubuntu) desktop machines, it uses gio trash. (I've added alias tt='move-to-trash' to my alias definitions file; tt is a mnemonic for "to trash".)



                    #!/bin/bash
                    # move-to-trash

                    # Teemu Leisti 2018-07-08

                    # This script moves the files given as arguments to the trash directory, if they
                    # are not already there. It works both on (Ubuntu) desktop and server hosts.
                    #
                    # The script is intended as a command-line equivalent of deleting a file from a
                    # graphical file manager, which, in the usual case, moves the deleted file(s) to
                    # a built-in trash directory. On server hosts, the analogy is not perfect, as
                    # the script does not offer the functionalities of restoring a trashed file to
                    # its original location nor of emptying the trash directory; rather, it is an
                    # alternative to the 'rm' command that offers the user the peace of mind that
                    # they can still undo an unintended deletion before they empty the trash
                    # directory.
                    #
                    # To determine whether it's running on a desktop host, the script tests for the
                    # existence of directory ~/.local/share/Trash. In case it is, the script relies
                    # on the 'gio trash' command.
                    #
                    # When not running on a desktop host, there is no built-in trash directory, so
                    # the first invocation of the script creates one: ~/.Trash/. It will not
                    # overwrite an existing file in that directory; instead, in case a file given as
                    # an argument already exists in the custom trash directory, the script first
                    # appends a timestamp to the filename, with millisecond resolution, such that no
                    # existing file will be overwritten.
                    #
                    # The script will not choke on a nonexistent file. It outputs the final
                    # disposition of each argument: does not exist, was already in trash, or was
                    # moved to the trash.


                    # Exit on using an uninitialized variable, and on a command returning an error.
                    # (The latter setting necessitates appending " || true" to those arithmetic
                    # calculations that can result in a value of 0, lest bash interpret the result
                    # as signalling an error.)
                    set -eu

                    is_desktop=0

                    if [[ -d ~/.local/share/Trash ]] ; then
                    is_desktop=1
                    trash_dir_abspath=$(realpath ~/.local/share/Trash)
                    else
                    trash_dir_abspath=$(realpath ~/.Trash)
                    if [[ -e $trash_dir_abspath ]] ; then
                    if [[ ! -d $trash_dir_abspath ]] ; then
                    echo "The file $trash_dir_abspath exists, but is not a directory. Exiting."
                    exit 1
                    fi
                    else
                    mkdir $trash_dir_abspath
                    echo "Created directory $trash_dir_abspath"
                    fi
                    fi

                    for file in "$@" ; do
                    file_abspath=$(realpath -- "$file")
                    file_basename=$( basename -- "$file_abspath" )
                    if [[ ! -e $file_abspath ]] ; then
                    echo "does not exist: $file_abspath"
                    elif [[ "$file_abspath" == "$trash_dir_abspath"* ]] ; then
                    echo "already in trash: $file_abspath"
                    else
                    if (( is_desktop == 1 )) ; then
                    gio trash "$file_abspath" || true
                    else
                    move_to_abspath="$trash_dir_abspath/$file_basename"
                    while [[ -e "$move_to_abspath" ]] ; do
                    move_to_abspath="$trash_dir_abspath/$file_basename-"$(date '+%Y-%m-%d-at-%H:%M:%S.%3N')
                    done
                    # While we're reasonably sure that the file at $move_to_abspath does not exist, we shall
                    # use the '-f' (force) flag in the 'mv' command anyway, to be sure that moving the file
                    # to the trash directory is successful even in the extremely unlikely case that due to a
                    # run condition, some other thread has created the file $move_to_abspath after the
                    # execution of the while test above.
                    /bin/mv -f "$file_abspath" "$move_to_abspath"
                    fi
                    echo "moved to trash: $file_abspath"
                    fi
                    done





                    share|improve this answer




























                      2












                      2








                      2







                      A previous answer mentions the command gio trash, which is fine as far as it goes. However, on server machines, there is no equivalent of a trash directory. I've written a Bash script that does the job; on (Ubuntu) desktop machines, it uses gio trash. (I've added alias tt='move-to-trash' to my alias definitions file; tt is a mnemonic for "to trash".)



                      #!/bin/bash
                      # move-to-trash

                      # Teemu Leisti 2018-07-08

                      # This script moves the files given as arguments to the trash directory, if they
                      # are not already there. It works both on (Ubuntu) desktop and server hosts.
                      #
                      # The script is intended as a command-line equivalent of deleting a file from a
                      # graphical file manager, which, in the usual case, moves the deleted file(s) to
                      # a built-in trash directory. On server hosts, the analogy is not perfect, as
                      # the script does not offer the functionalities of restoring a trashed file to
                      # its original location nor of emptying the trash directory; rather, it is an
                      # alternative to the 'rm' command that offers the user the peace of mind that
                      # they can still undo an unintended deletion before they empty the trash
                      # directory.
                      #
                      # To determine whether it's running on a desktop host, the script tests for the
                      # existence of directory ~/.local/share/Trash. In case it is, the script relies
                      # on the 'gio trash' command.
                      #
                      # When not running on a desktop host, there is no built-in trash directory, so
                      # the first invocation of the script creates one: ~/.Trash/. It will not
                      # overwrite an existing file in that directory; instead, in case a file given as
                      # an argument already exists in the custom trash directory, the script first
                      # appends a timestamp to the filename, with millisecond resolution, such that no
                      # existing file will be overwritten.
                      #
                      # The script will not choke on a nonexistent file. It outputs the final
                      # disposition of each argument: does not exist, was already in trash, or was
                      # moved to the trash.


                      # Exit on using an uninitialized variable, and on a command returning an error.
                      # (The latter setting necessitates appending " || true" to those arithmetic
                      # calculations that can result in a value of 0, lest bash interpret the result
                      # as signalling an error.)
                      set -eu

                      is_desktop=0

                      if [[ -d ~/.local/share/Trash ]] ; then
                      is_desktop=1
                      trash_dir_abspath=$(realpath ~/.local/share/Trash)
                      else
                      trash_dir_abspath=$(realpath ~/.Trash)
                      if [[ -e $trash_dir_abspath ]] ; then
                      if [[ ! -d $trash_dir_abspath ]] ; then
                      echo "The file $trash_dir_abspath exists, but is not a directory. Exiting."
                      exit 1
                      fi
                      else
                      mkdir $trash_dir_abspath
                      echo "Created directory $trash_dir_abspath"
                      fi
                      fi

                      for file in "$@" ; do
                      file_abspath=$(realpath -- "$file")
                      file_basename=$( basename -- "$file_abspath" )
                      if [[ ! -e $file_abspath ]] ; then
                      echo "does not exist: $file_abspath"
                      elif [[ "$file_abspath" == "$trash_dir_abspath"* ]] ; then
                      echo "already in trash: $file_abspath"
                      else
                      if (( is_desktop == 1 )) ; then
                      gio trash "$file_abspath" || true
                      else
                      move_to_abspath="$trash_dir_abspath/$file_basename"
                      while [[ -e "$move_to_abspath" ]] ; do
                      move_to_abspath="$trash_dir_abspath/$file_basename-"$(date '+%Y-%m-%d-at-%H:%M:%S.%3N')
                      done
                      # While we're reasonably sure that the file at $move_to_abspath does not exist, we shall
                      # use the '-f' (force) flag in the 'mv' command anyway, to be sure that moving the file
                      # to the trash directory is successful even in the extremely unlikely case that due to a
                      # run condition, some other thread has created the file $move_to_abspath after the
                      # execution of the while test above.
                      /bin/mv -f "$file_abspath" "$move_to_abspath"
                      fi
                      echo "moved to trash: $file_abspath"
                      fi
                      done





                      share|improve this answer















                      A previous answer mentions the command gio trash, which is fine as far as it goes. However, on server machines, there is no equivalent of a trash directory. I've written a Bash script that does the job; on (Ubuntu) desktop machines, it uses gio trash. (I've added alias tt='move-to-trash' to my alias definitions file; tt is a mnemonic for "to trash".)



                      #!/bin/bash
                      # move-to-trash

                      # Teemu Leisti 2018-07-08

                      # This script moves the files given as arguments to the trash directory, if they
                      # are not already there. It works both on (Ubuntu) desktop and server hosts.
                      #
                      # The script is intended as a command-line equivalent of deleting a file from a
                      # graphical file manager, which, in the usual case, moves the deleted file(s) to
                      # a built-in trash directory. On server hosts, the analogy is not perfect, as
                      # the script does not offer the functionalities of restoring a trashed file to
                      # its original location nor of emptying the trash directory; rather, it is an
                      # alternative to the 'rm' command that offers the user the peace of mind that
                      # they can still undo an unintended deletion before they empty the trash
                      # directory.
                      #
                      # To determine whether it's running on a desktop host, the script tests for the
                      # existence of directory ~/.local/share/Trash. In case it is, the script relies
                      # on the 'gio trash' command.
                      #
                      # When not running on a desktop host, there is no built-in trash directory, so
                      # the first invocation of the script creates one: ~/.Trash/. It will not
                      # overwrite an existing file in that directory; instead, in case a file given as
                      # an argument already exists in the custom trash directory, the script first
                      # appends a timestamp to the filename, with millisecond resolution, such that no
                      # existing file will be overwritten.
                      #
                      # The script will not choke on a nonexistent file. It outputs the final
                      # disposition of each argument: does not exist, was already in trash, or was
                      # moved to the trash.


                      # Exit on using an uninitialized variable, and on a command returning an error.
                      # (The latter setting necessitates appending " || true" to those arithmetic
                      # calculations that can result in a value of 0, lest bash interpret the result
                      # as signalling an error.)
                      set -eu

                      is_desktop=0

                      if [[ -d ~/.local/share/Trash ]] ; then
                      is_desktop=1
                      trash_dir_abspath=$(realpath ~/.local/share/Trash)
                      else
                      trash_dir_abspath=$(realpath ~/.Trash)
                      if [[ -e $trash_dir_abspath ]] ; then
                      if [[ ! -d $trash_dir_abspath ]] ; then
                      echo "The file $trash_dir_abspath exists, but is not a directory. Exiting."
                      exit 1
                      fi
                      else
                      mkdir $trash_dir_abspath
                      echo "Created directory $trash_dir_abspath"
                      fi
                      fi

                      for file in "$@" ; do
                      file_abspath=$(realpath -- "$file")
                      file_basename=$( basename -- "$file_abspath" )
                      if [[ ! -e $file_abspath ]] ; then
                      echo "does not exist: $file_abspath"
                      elif [[ "$file_abspath" == "$trash_dir_abspath"* ]] ; then
                      echo "already in trash: $file_abspath"
                      else
                      if (( is_desktop == 1 )) ; then
                      gio trash "$file_abspath" || true
                      else
                      move_to_abspath="$trash_dir_abspath/$file_basename"
                      while [[ -e "$move_to_abspath" ]] ; do
                      move_to_abspath="$trash_dir_abspath/$file_basename-"$(date '+%Y-%m-%d-at-%H:%M:%S.%3N')
                      done
                      # While we're reasonably sure that the file at $move_to_abspath does not exist, we shall
                      # use the '-f' (force) flag in the 'mv' command anyway, to be sure that moving the file
                      # to the trash directory is successful even in the extremely unlikely case that due to a
                      # run condition, some other thread has created the file $move_to_abspath after the
                      # execution of the while test above.
                      /bin/mv -f "$file_abspath" "$move_to_abspath"
                      fi
                      echo "moved to trash: $file_abspath"
                      fi
                      done






                      share|improve this answer














                      share|improve this answer



                      share|improve this answer








                      edited Jul 9 '18 at 0:35

























                      answered Jun 26 '18 at 13:05









                      Teemu LeistiTeemu Leisti

                      214210




                      214210























                          1














                          Here is a open source nodejs-based version (if you want to know, what happens under the hood, or need this in a project), that also has command line support (if you are happy, if it just works.



                          > trash pictures/beach.jpg





                          share|improve this answer




























                            1














                            Here is a open source nodejs-based version (if you want to know, what happens under the hood, or need this in a project), that also has command line support (if you are happy, if it just works.



                            > trash pictures/beach.jpg





                            share|improve this answer


























                              1












                              1








                              1







                              Here is a open source nodejs-based version (if you want to know, what happens under the hood, or need this in a project), that also has command line support (if you are happy, if it just works.



                              > trash pictures/beach.jpg





                              share|improve this answer













                              Here is a open source nodejs-based version (if you want to know, what happens under the hood, or need this in a project), that also has command line support (if you are happy, if it just works.



                              > trash pictures/beach.jpg






                              share|improve this answer












                              share|improve this answer



                              share|improve this answer










                              answered Aug 15 '17 at 17:44









                              Frank NockeFrank Nocke

                              476421




                              476421























                                  0














                                  Updating @Radu Rădeanu answer. Since Ubuntu is telling me to use gio instead...



                                  So, to trash some_file (or folder) use



                                  gio trash some_file


                                  To go dumpster diving use



                                  gio list trash://


                                  To empty trash



                                  gio trash --empty





                                  share|improve this answer




























                                    0














                                    Updating @Radu Rădeanu answer. Since Ubuntu is telling me to use gio instead...



                                    So, to trash some_file (or folder) use



                                    gio trash some_file


                                    To go dumpster diving use



                                    gio list trash://


                                    To empty trash



                                    gio trash --empty





                                    share|improve this answer


























                                      0












                                      0








                                      0







                                      Updating @Radu Rădeanu answer. Since Ubuntu is telling me to use gio instead...



                                      So, to trash some_file (or folder) use



                                      gio trash some_file


                                      To go dumpster diving use



                                      gio list trash://


                                      To empty trash



                                      gio trash --empty





                                      share|improve this answer













                                      Updating @Radu Rădeanu answer. Since Ubuntu is telling me to use gio instead...



                                      So, to trash some_file (or folder) use



                                      gio trash some_file


                                      To go dumpster diving use



                                      gio list trash://


                                      To empty trash



                                      gio trash --empty






                                      share|improve this answer












                                      share|improve this answer



                                      share|improve this answer










                                      answered 6 hours ago









                                      BarmaleyBarmaley

                                      1113




                                      1113






























                                          draft saved

                                          draft discarded




















































                                          Thanks for contributing an answer to Ask Ubuntu!


                                          • Please be sure to answer the question. Provide details and share your research!

                                          But avoid



                                          • Asking for help, clarification, or responding to other answers.

                                          • Making statements based on opinion; back them up with references or personal experience.


                                          To learn more, see our tips on writing great answers.




                                          draft saved


                                          draft discarded














                                          StackExchange.ready(
                                          function () {
                                          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f213533%2fcommand-to-move-a-file-to-trash-via-terminal%23new-answer', 'question_page');
                                          }
                                          );

                                          Post as a guest















                                          Required, but never shown





















































                                          Required, but never shown














                                          Required, but never shown












                                          Required, but never shown







                                          Required, but never shown

































                                          Required, but never shown














                                          Required, but never shown












                                          Required, but never shown







                                          Required, but never shown







                                          Popular posts from this blog

                                          GameSpot

                                          connect to host localhost port 22: Connection refused

                                          Getting a Wifi WPA2 wifi connection