Untitled Document
find Command

find command stands for Extended grep - we have already seen this command in chapter-12 . Lets see more examples on egrep command

1. Basic example -

print the list of files and directories from current directory having permission as 644

find . -perm 644

2. Find the files from a directory which are not accessed from last one year

Here we will use -atime ( access time ) option and as we have to find files only hence we will add -type option with argument as f ( file ) . So here goes our command .

find . -atime +365 -type f

3. Print or find the directories having permissions 755

In our previous example we have used -type f ( fies ) to find files now to find dirtectories we have to use -type d

find . -perm 755 -type d 

4. Print or find the files having permissions 544

find . -perm 544 -type f

5. Print or Find the files having .ksh extension

Here we will using special character * to get all the files having .ksh extension . Also we will use -name option of find command , which tells find to search the files with name having .ksh as a extension .

find . -name "*.ksh" -type f 

6. Print or Find the files owned or created by a user

We can find the files owned / created by a user using option -user of find command . So to find the files owned by user mangesh run the following command .

find . -user "mangesh" -type f 

7. Print or Find the files which are not modified from an year

To find these files we will user -mtime option along with number of days as an argument to it . So to find files which are not being modified from last one year we will run find as -.

find . -mtime +365 -type f 

8. Print or Find the files which are not modified from 90 days and having .ksh extension

Run the above command as it is and just add -name "*.ksh" to it .

find . -mtime +90 -name "*.ksh" -type f 

9. Print or Find the files which are owned by an user where file name is case sensitive

We have alread seen an example - on this , but in that we were sure about filename case . Sometimes we don't know whether the files are in capital letters or small letters. So inorder to make our find case insensitive use -iname ( i - stands for insensitive).

find . -iname "*.ksh" -type f 

10. Limit the find search to n levels of sub-directories

By default find searches from root directory and if you have mentioned any particular location then it start looking into current directory as well as its all subsequent directories . If we want to limit this search to some level then we can use maxdepth option along with level number upto which we want to find as an argument ot it .So if we want to find all .ksh files from current directory to 2 level down then our command will be like this

find . -name "*.ksh" -maxdepth 2 -type f 

11. Print or Find all the files without .ksh extension

Here we have to use negation operator ! to omit .ksh files so our command will be like this .

find . ! -name "*.ksh" -type f 

12. Print or Find all the files with .ksh or .txt extension

Here we have to use -o ( OR) operator along with the files to be searched in paranthesis . so our command will be like this .

Note - We have to escape the praranthesis to remove their special meaning .

find . \( -name "*.ksh" -o -name "*.csv" \) -type f 

13. Print or Find all the files with .ksh or .txt extension and llist it details on terminal ( like ls command )

Just run below command as it is and see the output printed on your terminal

find . \( -name "*.ksh" -o -name "*.csv" \) -type f -ls 

14. Remove the old log files which are not accessed from last 365 days

Here we will use -exec option - which helps us to execute any command on UNix/ Linux operating system . The syntax to use -exec is like this -

-exec <unix_commad> { } \;

so now add this to our previsous command of finding files having acces time more than 365. The complete command will be like this .

find . -atime +365 -exec rm { } \;

15. Find all the .csv and .ksh extension files and copy it to another directory

Here we will first find the .csv and .txt files , as discussed in example - 12 and then just -exec cp { } <destination_path> \; . Run below command to copy the files .

find . \( -name "*.ksh" -o -name "*.csv" \) -type f -exec cp { } /v/user/mangesh \;

16. Find all the .csv and .ksh extension files and copy it to another directory with .bak extension

Please run below command . Just add {}.bak after the last / of the destination path .

find . \( -name "*.ksh" -o -name "*.csv" \) -type f -exec cp { } /v/user/mangesh/{}.bak \;

17. Combining commands . Find the files older than an year and send it admin via email

Here is the command for this

find . -atime +365 -type f | mailx -s "Files older than 365 days " mangesh 
Untitled Document