Friday, 22 January 2016

Linux / Unix: Find And Remove Files With One Command

Linux / Unix: Find And Remove Files With One Command.




This is really very easy in Linux/Unix. This is the reason why Linux/Unix is so popular in most of the industries.  

COMMAND :-

find - search for files in a directory hierarchy
 


SYNOPSIS 

find [-H] [-L] [-P] [-D debugopts] [-Olevel] [path...] [expression]

SYNTAX

find <dir-name> <criteria> <action>

dir-name - The directory which you want to search.
criteria - This is searching criteria, what you want to search.
action(if required) - This is command action you want to execute.

There are many options or expressions you can use to find your relevant files and folders.

Example :- The below command for finding all files with extension .xml


dev@jdg-developer-desktop ~ $ find soap_projects/  -name "*.xml"
 

soap_projects/folk-enabler-no-sec--soapui-project.xml
soap_projects/aim-localhost-tomcat-soapui-project.xml
soap_projects/roamenable-service-soapui-project.xml
soap_projects/search-enabler-soapui-project.xml


Find all the files , which file name contains "data" and extensions with .txt

dev@jdg-developer-desktop ~/workspace_test $ find .  -name "*data*.txt"  

./test_data_file.txt



Example for finding and delete all all files with "find" command.


find . -name "*data*.txt" -exec rm -rf {} \;

Example for finding and delete all .xml files.

find / -name "*.xml" -exec rm -f {} \;


Example for finding and delete all .xml files.

find /searchdirectory -name "*.xml" -exec rm -f {} \;


Delete command for all file with find command result :-

-exec rm -rf {} \;



Hope it will help you.



Follow for more details on Google+ and @Facebook!!!

Find More :-