Mayfield Global Knowledge Center



How do I delete all files of a particular extension from a directory and it's sub-directories?

In this article, we will be deleting all .txt files from our target directory and all sub-directories below it.

  1. From the desktop, press Alt+F2 to launch the Run Application window.
  2. Type: gnome-terminal then press Enter. This will launch a user terminal (similar to Windows command prompt).
  3. Navigate to the parent directory that contains the files you wish to remove by typing:cd <directory> Press Enter to execute the command.
  4. Verify that you are in the correct directory by typing:ls -al then press Enter. You should see the target files. The -al option is used to display any hidden files.
  5.  The next step will completely and irrevocably delete ALL files (in our example, all .txt files) in the current or working directory. Be absolutely certain that this is what you intend to do.
  6. At the terminal prompt, type:find . -name "*.txt" -exec rm {} \; then press Enter. All .txt files have been removed.
  7. Type:ls -al then press Enter to confirm that all .txt files were removed.
  8. This procedure can be used to remove files of any type (assuming that you are the owner of said files). For example, to remove all .html files, you would simply change the statement to:find . -name "*.html" -exec rm {} \; then press Enter.

Would you like to...


del.icio.us

Print this page Print this page

Email this page Email this page

Post a comment Post a comment

Subscribe me

Add to favoritesAdd to favorites

User Opinions (8 votes)

100% thumbs up 0% thumbs down

How would you rate this answer?

Helpful
Not helpful
Thank you for rating this answer.