How to delete useless file in Ubuntu

Using gedit creates lot of temp files of pattern “*~”. And if you are working in multiple directories its really hard to get to all these directories and delete these files.
Also, people want to delete “Thumbs.db” from windows directories but don’t know how.

Here is a solution:

This will find all the files,

find /path_to_directory -type f -name "file_pattern"

To delete these files, you can use

find /path_to_directory -type f -name "file_pattern" -exec rm -f {} \;

where /path_to_directory is path to directory from which you want to delete the files having pattern or name file_pattern recursively.

For example, to remove files like program.cpp~ created by gedit you can use

find /home/user -type f -name "*~" -exec rm -f {} \;

and to remove Thumbs.db from windows directories you need to do

find /media -type f -name "Thumbs.db" -exec rm -f {} \;

If you want to examine the files that are going to be deleted before doing it, you should execute the same command, changing ‘rm’ for ‘ls’, like this:

find /path_to_directory -type f -name "file_pattern" -exec ls -f {} \;

In this way you may control the list of files to delete.

PS: \; is necessary.
I hope this helps.

2 thoughts on “How to delete useless file in Ubuntu”

  1. nice one abhinav …!

    but why do we need to delete these files using commands ?? The files ending with *~ are hidden and can be seen using ” View > Show Hidden Files ” option. We can simply delete them from there (Shift+delete) 🙂

    However, is there any command to stop these files from being formed ??

    1. By using ”View > Show Hidden Files” you have to go through several folders to delete or access these files. But using this command will check the directory and its subdirectory for the file.
      To check the formation of “*~” files, instead of using gedit or kate, you should use vim.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s