How to empty Trash through command line in Ubuntu

How to empty Trash through command line in Ubuntu

Operating System : Ubuntu 12.10 n l

I checked this practical in Ubuntu 12.10 n l .

To empty the trash in Ubuntu through command line follow the given steps

Step 1 : First check is Trash folder exist. By ls -la command you can see other files and folders inside Trash dir.

linux@tuxworld:~$ ls -la ~/.local/share/Trash/ 
total 20
drwx------  5 linux linux 4096 Jul 17  2012 .
drwxr-xr-x 28 linux linux 4096 Jun 21 20:36 ..
drwx------  2 linux linux 4096 Apr  2 07:51 expunged
drwx------  2 linux linux 4096 Jun 18 21:02 files
drwx------  2 linux linux 4096 Jun 18 21:02 info

Step 2: Now change to directory files.

cd linux@tuxworld:~$ cd ~/.local/share/Trash/files

ls -la ~/.local/share/Trash/files

Step 3 : Now remove all files and directory inside ~/.local/share/Trash/files

rm -fr  ~/.local/share/Trash/files/*

Now check Trash in Desktop, it would be empty now.

5 thoughts on “How to empty Trash through command line in Ubuntu”

  1. What would be the command to empty ALL trash files, including the ones that are in other partitions, and other drives, and USB stick etc.? Can this be done with one single command, since the trash icon shows full whenever there is trash in partitions as well as the local trash area.

    Thank you in advance.

    • Hello Walt,

      Yes it is possible but in this case you have to make script for it.
      (1)By-default the trash directory exist to User’s Home directory. In a single installed Operating System you may have many users hence rather than going to each user’s home directory. We can create a script. For eg. Our all user’s home directory path is in /home then you can use this loop command.

      First check,in which user’s home dir Trash exist. This command not only help to show existence of Trash but we can also see is our command okay to run.

      login as superuser,

      sudo su -
      ls -1d /home/* |while read abc;do ls -la "$abc/.local/share/Trash/files/";done

      To remove

      ls -1d /home/* |while read abc;do rm -fr "$abc/.local/share/Trash/files/";done

      (2) Now second scenario is with different partition, I am assuming multiple Ubuntu booting system. In this case the running operating system must have these partition in some mounted dir. Which you can see by running command df -Th

      You have to just change the script or one liner command and give absolute path of mounted partition.

      for eg. you mounted other filesystem and now mounted dir. are like this

      /partition2 /dev/sda2
      /partition3 /dev/sda3

      for i in partiton2 partition3;do find "$/i" -name Trash >> trash_path;done

      This will list in which partition the Trash exist to which users of that particular Operating system

      for i in `cat trash_path`;do ls -la  "$i/files";done

      Note: I just write dirty bash script one liner. Before removing anything, first check. Modify the script as per your Operating system.


Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.