Pages

Saturday, April 15, 2017

find command with examples

find will be used to search files and folders. Below are some of the additional options of find command 

1) Maxdepth and Mindepth 

# -maxdepth n  - will search only to n levels

-mindept n - will start search from level n through remainder of direcoty eg : 3 will begin search. level2 and continue to end. if the folder has only two sub directories then we wont get result

2)
Ignore some directories while searching in a path

prune option will be used to ignore the directory while searching in a path.


find [path] - prune -o [your usual condition] 

find / -path /etc -prune -o  -name '*conf'

for example if you want to avoid searching .conf files in etc but exculde ssh directory then the condtion will be like

To Ignore multiple directories while searching

find . -type d \(-path dir 1 -o -path dir 2 -o -path dir 3\) -prue -o -print 

1) grep in sub directories on files which has space in name's 

We can use getfacl and setfacl to backup and restore permission. Below is the example.


# find . -name "*.yaml"  -print0 | xargs -0  grep -i "orabat"

To find empty files and folders


# find / -empty -type f
# find / -empty  -type d

To find  files/directories of above/below particular size

# find / -type f -size +100M
# find / -type f -size -100M

To find files owned by particular user/group

# find / -UID 510
# find / -GID 510
# find / -nouser
# find / -nogroup

print 0 option
find -print0 True; print the full file name on the standard output, followed by a null character (instead of the newline character that -print uses).  This allows file names that contain newlines or  other  types of white space to be correctly interpreted by programs that process the find output.  This option corresponds to the -0 option of xargs.

print0 will print  the output in a single line

-print0 True; print the full file name on the standard output, followed by a null character (instead of the newline character that -print uses).  This allows file names that contain newlines or  other  types of white space to be correctly interpreted by programs that process the find output.  This option corresponds to the -0 option of xargs.

No comments:

Post a Comment