Monday, 14 December 2015

Advance Uses Of FIND Command

In this article we will some of the advance uses of find command. Use of these concepts will make thing easier and provides the modularity to the scripts. Please follow previous posts on find for more clarity.

Display all the properties of a file
$ find . -type f -mtime -5 -exec ls -ltr {} \;

$ find . -type f -ctime -5 -exec ls -ltr {} \;
$ find . -type f -atime -5 -exec ls -ltr {} \;
$ find . -type f -size -10M -exec ls -ltr {} \;
Note: Make Sure to use -type switch while using -exec switch of find.

Remove Files/Archiving Implementation
$ find . -type f -name *.csv -atime +30 -exec rm -f {} \;
$ find . -type f -name *.csv -mtime +30 -exec rm -f {} \;
$ find . -type f -name *.csv -ctime +30 -exec rm -f {} \;

$ find . -type f -name *.csv -size +100M -exec rm -f {} \;
Note: Make Sure to use -type switch while using -exec switch of find.

Empty Files and Directories:
$ find . -type f -empty
$ find . -type d -empty


Find files other than .txt
$ find . -not -name "*.txt"
$ find . ! -name "*.txt"


Find files starting with abc and not ending with .txt
$ find . -name 'abc*' ! -name '*.txt'

Find files Ending With .c or .txt
$ find . -name '*.c' -o -name '*.txt'


Find the file in present directory and its chiled directory(till 2nd level of tree) only
$ find . -maxdepth 2 -name "*abc*"

Find the file in present directory and its child directory and child's child directory (till 3rd level of tree) only
$ find . -maxdepth 3 -name "*abc*"

How to print the files in the subdirectories between level 2 and 5
$ find -mindepth 3 -maxdepth 6 -name "*abc*"

Note: From where you are starting the search is Level 0.By using maxdepth/mindepth we can restrict the find to search in complete tree.

No comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...