Monday, May 13, 2013

find - Files/Directories search Command in Unix

find is used to search files or directories in particular directories and its sub-directories. Which is very powerful search command in unix.

Syntax : find [start search locations] -name "<file name or dierctory name>" 

Examples :
$find / -name ‘searchFileName’ -type f

find the file name ‘searchFileName’ start from the root directory and subdirectory

 $find . –name ‘searchFileName’ -type f
Find the file name ‘searchFileName’ start from the current directory  (where  this command should executed) and its sub directory

$ find . -name "*.htm*" -type f
find the files end with .htm from current directory.

$ find . -name "*" -type f -size  +1000
    This is used to find all files with size greater than 1000k from current directory.

$ find . -name foo.bar -type f  2> /dev/null
    This is used to find foo.bar file ,suppose it does not have any access permission it eliminate error message( 2>/dev/null).

$ find . -type d
This is used to find all directory only.

$ find . -type f
This is used to find all normal files from current directory.

Finding files according to date and time

$find /home -atime +7 -type f

-atime +7: All files that were last accessed more than 7 days ago
-atime 7: All files that were last accessed exactly 7 days ago
-atime -7: All files that were last accessed less than7 days ago

$find /home -mtime +7 -type f

-mtime +7: All files that were last modified more than 7 days ago
-mtime 7: All files that were last modified exactly 7 days ago
-mtime -7: All files that were last modified less than7 days ago

The power of find with grep

$find . –name ‘*.txt’ –exec rm {} \;

This is used to find all .txt files and deleted all.

$find . -name '123' -exec grep 'python' {} \;

This is used to find ‘python’ word in a file name 123 and it shows line which occur in a file.

$find . -name '123' -exec grep -l 'python' {} \;

This is used to find ‘python’ word in a file name 123 and it shows the path of the file.

$find . -maxdepth 1 -name "*" -type f -exec egrep -i '(search_string1|search_string2)' {}\;

This is used to find the file name and its content which is occured the search string1 and search string2 from the current directory. it wont proceed the search process to subdirectories. Can control search directories using maxdepth.

$find . -maxdepth 1 -name "*" -type f -exec egrep -il '(search_string1|search_string2)' {}\;

This is used to find the file names only which is occured the search string1 and search string2 from the current directory. it wont proceed the search process to subdirectories.

find . -maxdepth 1 -name "*" ! \( -name "*.dat*" -o -name "*.log*" -o -name "*.csv*" \) -type f


This is used to find all the files except the file end with .dat, .log and .csv



1 comment:

Price List Query for Item

 SELECT qph.list_header_id,        qph.name,        qph.description,        qphh.start_date_active,        qphh.currency_code,        q...