0% found this document useful (0 votes)
15 views3 pages

Grep and Find Handling Commands

The document provides a comprehensive overview of grep and file handling commands in a Unix-like operating system. It includes commands for removing files and directories, searching for content within files using grep with various options, and utilizing the find command for locating files based on different criteria. Examples are provided for each command to illustrate their usage effectively.

Uploaded by

iwantsatyam
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views3 pages

Grep and Find Handling Commands

The document provides a comprehensive overview of grep and file handling commands in a Unix-like operating system. It includes commands for removing files and directories, searching for content within files using grep with various options, and utilizing the find command for locating files based on different criteria. Examples are provided for each command to illustrate their usage effectively.

Uploaded by

iwantsatyam
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Grep and File Handling Commands

Grep: Global Regular Expression Print

File Removal Commands


 Remove all `.txt` files at once:
rm *.txt
 Remove files based on pattern:
rm filename*
Example: rm da*
 Remove a specific file:
rm filename
 Remove an empty folder:
rmdir foldername
 Remove everything in a directory:
rm *

Using `grep` to Search Content in Files


 Basic search in a file:
grep word filename
Example: grep carlo customer.sql
 Search similar words Ignoring case sensitivity:
grep -i word filename
Example: grep -i grass file1
 Show everything except a specific word:
grep -v word filename
Example: grep -v grass file1
 Ignore case sensitivity and remove word:
grep -iv word filename
Example: grep -iv grass file1, It will search for grass word ignoring case sensitivity and
then remove it
 Count occurrences of a word:
grep -c word filename
Example: grep -c grass file1, Number of times grass word occurred
 Count, ignoring case:
grep -ic word filename
Example: grep -ic grass file1
 Search for exact word match:
grep -w word filename
Example: grep -w grass file1
 Show line number for matched word:
grep -nw word filename
Example: grep -nw grass file1
 Search in multiple files (case-insensitive):
grep -i word file1 file2
Example: grep -i grass file1 file2
 Search and hide filename in output:
grep -ih word file1 file2
Example: grep -ih grass file1 file2
 Search for multiple words:
grep -ie word1 -e word2 -e word3 file1 file2
 Alternative using `egrep`:
egrep "word1|word2|word3" file1 file2 file3
 Show filenames where a word exists:
grep -l word file1 file2
Example: grep -l grass file1 file2
 Use word list from one file to search in another:
grep -f file1 file2
 Search words starting with 'as':
grep ^as file1 file2
 Search words ending with 'ti':
grep ti$ file1 file2
 Check if a word exists (quietly):
grep -q word filenames
echo $?
0 = found, 1 = not found
 List files starting with a given name:
ls | grep file
 Recursive search in folder:
grep -R "keyword" foldername
Example: grep -R "grass" foldername

`find` Command Usages


 Find file by exact size:
find . -size 19M
 Size shortcuts:
Bytes: -c, KB: -K, MB: -M, GB: -G
 Find files in a size range:
find /home/Prashant/ -size +15M -size -25M
 Change file size:
truncate -s size filename
Example: truncate -s 25M file3
 Find file by name:
find . -iname filename
Example: find . -iname file1
 Find files owned by root:
find . -user root
 Find files owned by specific user:
find . -user username
Example: find . -user xyz
 Find empty files:
find . -empty
 Find and remove empty files:
find . -empty -exec rm {} \;
 Find and remove files by name pattern:
find . -name "file*" -exec rm {} \;
 Find files older than N days:
find -mtime number_of_days
Example: find -mtime 10
 Find files created after another file:
find . -newer filename
Example: find . -newer index.html

You might also like