Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Find and Delete Files and Directories on Linux
In this article we are going to understand the find command in Linux and how to use it to delete files and directories effectively.
The find Command
The find command in Linux is a powerful command-line utility that helps you search for files and directories based on specified criteria and perform operations on the results. It can search by name, type, permissions, modification time, and other attributes.
The search starts from a specified location and traverses recursively through all subdirectories in the hierarchy. You can limit the search scope by specifying particular directories or using various filtering options.
Syntax
$ find [path] [options] [expression]
Parameters
path The starting location for the search (defaults to current directory if omitted)
options Search criteria such as
-name,-type,-size, etc.expression Actions to perform on found items like
-delete,-print,-exec
Deleting Files with find
Basic File Deletion
To delete a specific file using the -delete option:
find . -name "filename.txt" -delete
Let's see this in action. Starting with these files:
$ ls demo/ img.jpg img2.jpg img3.jpg img4.jpg test.txt xyz.txt
Delete a specific file:
$ find . -name "test.txt" -delete $ ls demo img.jpg img2.jpg img3.jpg img4.jpg xyz.txt
Deleting Files by Pattern
To delete all files with a specific extension (note the single quotes to prevent shell expansion):
$ find . -name '*.jpg' -delete $ ls demo xyz.txt
This command recursively deletes all .jpg files from the current directory and all subdirectories.
Deleting Directories
Empty Directories
The -delete option can only remove empty directories. If a directory contains files, it will produce an error:
$ find . -type d -name "demo" -delete find: ./demo: Directory not empty
For empty directories, it works without issues:
$ find . -type d -name "emptydir" -delete
Non-Empty Directories with -exec
To delete directories with contents, use the -exec option with rm:
| Command | Description |
|---|---|
-exec rm -rf {} \; |
Delete each found item individually |
-exec rm -rf {} + |
Delete multiple items in batches (more efficient) |
-exec rm -rfi {} + |
Interactive deletion with confirmation prompts |
Examples of Directory Deletion
Non-interactive deletion:
$ find . -type d -name "demo" -exec rm -rf {} +
Interactive deletion:
$ find . -type d -name "demo" -exec rm -rfi {} +
rm: descend into directory './demo'? y
rm: remove directory './demo/subdir'? y
rm: remove directory './demo'? y
Using xargs for Deletion
The xargs command provides an alternative approach by piping find results to rm:
Delete files by extension:
$ find . -name "*.txt" | xargs rm -rf
Delete directories:
$ find . -type d -name "dirname" | xargs rm -rf
Safety Tips
Always test your find command with
-printbefore using-deleteUse
-maxdepthto limit search depth and avoid accidental deletionsBe careful with wildcards always quote patterns like
'*.txt'Consider using
-confirmor interactive options for important deletions
Conclusion
The find command offers powerful and flexible ways to locate and delete files and directories in Linux. While -delete works for simple cases, -exec and xargs provide more control for complex deletion tasks. Always test your commands carefully to avoid accidental data loss.
