Locate and Delete files in Linux
We can use the locate command with various choices for more complicated specifications. To delete all files in a path specified by {dir-to-search} that follow a pattern {pattern}.
find {dir-to-search} -type f -name {pattern} -exec rm -f {} \;
Example:
find luv -type f -name "*.txt" -exec rm -f {} \;
We may slightly change the above command to delete everything that fits the sequence {pattern}, including directories within {dir-to-search}:
find {dir-to-search} -name {pattern} -exec rm -rf {} \;
Internally, modern implementations of the find command support the delete feature. The -delete flag is used to override the rm instruction, while the –depth flag tells find to process the contents of the directory before the directory itself:
find {dir-to-search} -type f -name {file-name-pattern} -depth -delete
How to Delete Files in Linux?
Linux comes with several tools that can assist us in removing Directories and files. We always need to delete many files and folders based on a set of requirements. To complete our mission quickly, knowing a few basic commands and their variations is beneficial.
- Use caution when using the commands below, particularly those that use regular expressions or search patterns with the find command. An incorrect expression or pattern will result in the deletion of important data/system files and non-intended files.
- Often have a current copy of critical data and device files.
- Use caution when running those commands, particularly if you’re using Sudo or as the superuser (root).
Table of Content
- 1. Delete file by using “unlink” in Linux
- 2. Delete a single file in Linux
- 3. Delete Multiple files in Linux
- 4. Locate and Delete files in Linux
- 5. Empty files should be found and deleted
- 7. Permissions are used to locate and delete files