Finding Something

Using tools to find files, keywords in the Linux Filesystem

For using regular expressions with the following commands, checkout the Regular Expressions Cheatsheet

GREP

Find keywords inside files

Finding Keywords inside files recursively and print the file names

grep -rl "<keyword>" <path>
# grep -rl "service" /root

Using regular expression

Some other useful CLI options for grep

Flag
Meaning

-i

Ignore case

-c

Print count of matching lines

-v

Return all lines which don't match the pattern

FIND

Find files on the file system and maybe modify permissions (in bulk) if needed.

#Syntax:
#find options starting/path expression

find / -name abc.txt 

Find files based on the time they were modified

-mtime

find . -name abc.txt -mtime -7
#find abc.txt modified within the last 7 days

Find files recursively

-maxdepth -> At max how many subdirectories should it traverse

-mindepth -> Ignore the results before the depth specified.

-mount -> Find files on this FS only.

find /mnt -name abc.txt -maxdepth 6 -mindepth 3 -mount
# Look for abc.txt within /mnt, ignore directories at depth 1 and 2; Look only
# in directories at level 3,4,5,6. Also, don't look at any other FS (ext4,efs,etc.) 
# mounted inside this path.

Set File Permissions in bulk

-exec -> Execute shell commands

find . -type f -name "*.txt" -exec chmod 644 {} \;
find . -type d -exec chmod 750 {} \;

Last updated