- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Find and Delete Files and Directories on Linux
In this article we are going to understand about the find command in Linux and also how to use the find command to delete files and directories in Linux.
The find command
The find command in Linux is a powerful command-line utility tool that helps you search,find or filter for files and directories based on the matching pattern specified by the user and allows you to perform subsequent operations on the result you get.The operations can be printing the files found, or deleting , reading the contents etc.
The file searching will be done starting from the current location and will go on recursively to all the directories, sub-directories present in the hierarchy. The user can limit the search to a certain level from the current directory by providing the required directory or subdirectory as the matching pattern.
The find command allows you to search by file , directory , name, date of creation of the file or directory, date of modification , owner and permissions.
Syntax
Following is the syntax for find command −
$ find [path] [options] [expression]
Parameters
All the parameters are optional and by default find command will take the current working directory and list all the files present in the hierarchy.
path − Here path is the location where you want to search for the file.
options − The options param is nothing but a pattern for the find command to search for the file.For example it can be the name of the file or folder, permission, creation or modification time or date.
expression − The action you want to perform on the result. For example -delete, -print etc. The expression can be used in different ways and will get to know more in detail in the examples we try below.
Example
In this example to perform the delete operation we are going to make use of -delete expression. Consider you want to delete a .txt file from your current working directory, the command for same is as follows:
find . -name filepattern -delete
In the above example the (.) refers to the current working directory.
The -name option is used to get the file pattern for example in our case it can be the name of the file for example test.txt or it can also be file extension (“.txt”). IF filename is given it will delete only the file with that name , if the file extension is given it will delete all the files with .txt in that working directory.
The -delete expression will perform the file deletion.
Let us try the same in command line.
$ ls demo/ img.jpg img2.jpg img3.jpg img4.jpg test.txt xyz.txt
In the current directory I have the demo/ sub-directory and files. Let us try to delete test.txt from it.
localhost:~/testFindCommand# find . -name test.txt -delete
The above command will delete the file and will not give you any output .But if you check now the folder the test.txt will be gone as shown below.
localhost:~/testFindCommand# ls demo img.jpg img2.jpg img3.jpg img4.jpg xyz.txt
Using the same command lets try to delete all the files with .jpg extension. If you see in folder demo/ you will get following files:
localhost:~/testFindCommand/demo# ls abc.txt img5.jpg
So, when you try to delete files with the .jpg extension all the files from your current directory and also the sub-directory demo with .jpg will get deleted. Let us run the command and check the result.
localhost:~/testFindCommand# find . -name '*.jpg' -delete
Now if you check the folder you will get the following output −
localhost:~/testFindCommand# ls demo xyz.txt
Inside demo/ you will have the following details
localhost:~/testFindCommand# ls demo xyz.txt localhost:~/testFindCommand# cd demo localhost:~/testFindCommand/demo# ls abc.txt
So now from your current folder and the sub folders you will not have any .jpg files.
Example
In this case we are going to delete a directory. So let us know what patterns and options can be used to delete a directory.
The -delete option will allow you to delete the given directory only if the directory is empty.IF the directory has any files present it will throw an error. Let us check one example. So in our current working directory we have the following files and sub-directory.
localhost:~/testFindCommand# ls demo testDemo xyz.txt
We have a demo and testDemo sub-directory , let us check their contents :
localhost:~/testFindCommand# cd demo localhost:~/testFindCommand/demo# ls abc.txt
The demo folder is not empty and has a file abc.txt. The testDemo folder is empty as shown below:
localhost:~/testFindCommand# cd testDemo localhost:~/testFindCommand/testDemo# ls localhost:~/testFindCommand/testDemo#
Now, let us use the find command with -delete on demo and testDemo.
find . -name demo -delete Or, find . -type d -name demo -delete //The -type d in above command is referred for directories.
The above command does not delete the demo folder and throws error as shown below:
localhost:~/testFindCommand# find . -name demo -delete find: ./demo: Directory not empty
Now, let us try to delete the empty directory testDemo.
localhost:~/testFindCommand# find . -name testDemo -delete localhost:~/testFindCommand# ls demo xyz.txt
The empty directory got deleted without any issue. So please keep in mind to use -delete option to delete empty directories.
Example
The exec option allows you to delete all the contents of the directories.The problem we ran into using -delete option will get sorted using -exec option. Before we get into the details of using the -exec command let us understand how to use it.
We are going to make use of the following expression with the find command.
-exec rm -rf {} \; − This allows you to make use of rm (remove command) with exec.All the files present in the directory and subdirectories will be recursively removed with -rf option in it. The files found are placed in the curly braces {} placeholder.
-exec rm -rf {} + − This command is more or less similar to the above one with only difference: it makes use of + sign at the end. The performance with + is said to be better when looking for matching files in comparison to (;). Also when you use (;) you need to escape it with (\) whereas + you don;t need to escape it.
-exec rm -rfi {} + − This command is also similar, but it has the -i option that stands for interactive and it will ask for your permission before deleting the file.
Let us test the command with examples:
-exec rm -rf {} \;
localhost:~/testFindCommand# find . -type d -name demo -exec rm -rf "{}" \; find: ./demo: No such file or directory localhost:~/testFindCommand# ls xyz.txt
-exec rm -rf {} +
localhost:~/testFindCommand# find . -type d -name test -exec rm -rf "{}" + localhost:~/testFindCommand#
-exec rm -rfi {} +
localhost:~/testFindCommand# find . -type d -name demo -exec rm -rfi "{}" + rm: descend into directory './demo'? y rm: descend into directory './demo/test'? y rm: descend into directory './demo/test/abc'? y rm: descend into directory './demo/test/abc/tt'? y rm: remove directory './demo/test/abc/tt'? y rm: remove directory './demo/test/abc'? y rm: remove directory './demo/test'? y rm: remove directory './demo'? y localhost:~/testFindCommand#
Example
You can make use of xargs command with the result that you get from find command to delete the directory or file. Command to delete all files with “*.txt” extension
find . -name “*.txt” | xargs rm -rf localhost:~/testFindCommand# ls demo xyz.txt localhost:~/testFindCommand# find . -type f -name "*.txt"|xargs rm -rf localhost:~/testFindCommand# ls demo
Command to delete the directory with xargs.
localhost:~/testFindCommand# ls demo xyz.txt localhost:~/testFindCommand# find . -name demo | xargs rm -rf localhost:~/testFindCommand# ls xyz.txt