Tuesday, March 10, 2015

Finding Files / Directories in Unix / Linux

There is always need to search files or specific 'text_string' in any file in the applications with unix / linux OS servers. Here i am specifying certain commands which will always help you for such requirements and make your task easier.

To Search Files : find . -type f –name ‘File_Name.ext’

To Search Directory : find . -type d -name "Folder_Name";



To Search String Character within File :
grep –i “TEXT_STRING” ./File_Name
or
cat /path/to/file | grep -i 'TEXT_STRING'

String Search through VI Editor :
vim / vi File_Name ( Enter ) Then
/TEXT_STRING ( Forward slash {/} then the string to search, type n for next occurrence of string & shift+n for previous occurrence )

Find & Replace String Character in File:
vi File_Name ( Enter ) Then :
%s/Old_String/New_String/g

To Search all Files with path with specific String:
find . -type f -exec grep -il "String_Text" {} \;       

To Find files of certain types containing specific 'String_Text':
find . -type f -name "*.*" -exec grep -il "String_Text" {} \;  

[Note - Use File name or *.File_Extension in place of *.*]



  • To Find & list out files of UID - appadmin having size > 20MB which are five days older.
find . -user appadmin -type f -size +20M -mtime +5 -exec ls -lrt {} \;



  • To Read Specific Line of file:

sed -n '100p' File_Name

Where - 100 is line number for file : File_Name

Note - In Above commands:
# i for Ignore case
# s for search
# g for globally
# f for File
# d for directory / folder
# exec - for Execution
# {} for File-name Placeholder
# \; To end & execute the command

Additional Information :
For executing text make use as below:

  • 'Text' => For text without blank space or single word use Single Quote - '
  • "Text String" => For text with spaces in between or group of words use double quote - "


No comments:

Post a Comment