Skip to main content

find command in unix

 

Find Command in Unix and Linux Examples

Find is one of the powerful utility of Unix (or Linux) used for searching the files in a directory hierarchy. The syntax of find command is


find [pathnames] [conditions]


Let see some practical exercises on using find command.

1. How to run the last executed find command?


!find


This will execute the last find command. It also displays the last find command executed along with the result on the terminal.

2. How to find for a file using name?


find -name "sum.java"

./bkp/sum.java

./sum.java


This will find all the files with name "sum.java" in the current directory and sub-directories.

3. How to find for files using name and ignoring case?


find -iname "sum.java"

./SUM.java

./bkp/sum.java

./sum.java


This will find all the files with name "sum.java" while ignoring the case in the current directory and sub-directories.

4. How to find for a file in the current directory only?


find -maxdepth 1 -name "sum.java"

./sum.java


This will find for the file "sum.java" in the current directory only

5. How to find for files containing a specific word in its name?


find -name "*java*"

./SUM.java

./bkp/sum.java

./sum.java

./multiply.java


It displayed all the files which have the word "java" in the filename

6. How to find for files in a specific directory?


find /etc -name "*java*"


This will look for the files in the /etc directory with "java" in the filename

7. How to find the files whose name are not "sum.java"?


find -not -name "sum.java"

.

./SUM.java

./bkp

./multiply.java


This is like inverting the match. It prints all the files except the given file "sum.java".

8. How to limit the file searches to specific directories?


find -name "sum.java"

./tmp/sum.java

./bkp/var/tmp/files/sum.java

./bkp/var/tmp/sum.java

./bkp/var/sum.java

./bkp/sum.java

./sum.java


You can see here the find command displayed all the files with name "sum.java" in the current directory and sub-directories.

a. How to print the files in the current directory and one level down to the current directory?


find -maxdepth 2 -name "sum.java"

./tmp/sum.java

./bkp/sum.java

./sum.java


b. How to print the files in the current directory and two levels down to the current directory?


find -maxdepth 3 -name "sum.java"

./tmp/sum.java

./bkp/var/sum.java

./bkp/sum.java

./sum.java


c. How to print the files in the subdirectories between level 1 and 4?


find -mindepth 2 -maxdepth 5 -name "sum.java"

./tmp/sum.java

./bkp/var/tmp/files/sum.java

./bkp/var/tmp/sum.java

./bkp/var/sum.java

./bkp/sum.java


9. How to find the empty files in a directory?


find . -maxdepth 1 -empty

./empty_file


10. How to find the largest file in the current directory and sub directories


find . -type f -exec ls -s {} \; | sort -n -r | head -1


The find command "find . -type f -exec ls -s {} \;" will list all the files along with the size of the file. Then the sort command will sort the files based on the size. The head command will pick only the first line from the output of sort.

11. How to find the smallest file in the current directory and sub directories


find . -type f -exec ls -s {} \; | sort -n -r | tail -1


Another method using find is


find . -type f -exec ls -s {} \; | sort -n  | head -1


12. How to find files based on the file type?

a. Finding socket files


find . -type s


b. Finding directories


find . -type d


c. Finding hidden directories


find -type d -name ".*"


d. Finding regular files


find . -type f


e. Finding hidden files


find . -type f -name ".*"


13. How to find files based on the size?

a. Finding files whose size is exactly 10M


find . -size 10M


b. Finding files larger than 10M size


find . -size +10M


c. Finding files smaller than 10M size


find . -size -10M


14. How to find the files which are modified after the modification of a give file.


find -newer "sum.java"


This will display all the files which are modified after the file "sum.java"

15. Display the files which are accessed after the modification of a give file.


find -anewer "sum.java"


16. Display the files which are changed after the modification of a give file.


find -cnewer "sum.java"


17. How to find the files based on the file permissions?


find . -perm 777


This will display the files which have read, write, and execute permissions. To know the permissions of files and directories use the command "ls -l".

18. Find the files which are modified within 30 minutes.


find . -mmin -30


19. Find the files which are modified within 1 day.


find . -mtime -1


20. How to find the files which are modified 30 minutes back


find . -not -mmin -30


21. How to find the files which are modified 1 day back.


find . -not -mtime -1


22. Print the files which are accessed within 1 hour.


find . -amin -60


23. Print the files which are accessed within 1 day.


find . -atime -1


24. Display the files which are changed within 2 hours.


find . -cmin -120


25. Display the files which are changed within 2 days.


find . -ctime -2


26. How to find the files which are created between two files.


find . -cnewer f1 -and ! -cnewer f2


So far we have just find the files and displayed on the terminal. Now we will see how to perform some operations on the files.

1. How to find the permissions of the files which contain the name "java"?


find -name "*java*"|xargs ls -l


Alternate method is


find -name "*java*" -exec ls -l {} \;


2. Find the files which have the name "java" in it and then display only the files which have "class" word in them?


find -name "*java*" -exec grep -H class {} \;


3. How to remove files which contain the name "java".


find -name "*java*" -exec rm -r {} \;


This will delete all the files which have the word “java" in the file name in the current directory and sub-directories.

Similarly you can apply other Unix commands on the files found using the find command. I will add more examples as and when i found.

If you like this post, then please share it on Google by clicking on the 
+1 button.

 

Comments

Popular posts from this blog

PL / SQL practice programs

  PL / SQL  practice programs 1. Write a program to print the following format WELCOME TO PL/SQL PROGRAMMING   BEGIN   DBMS_OUTPUT.PUT_LINE('WELCOME   TO   PL/SQL   PROGRAMMING'); END; /   2. Write a program to print the numbers from 1 to 100   DECLARE N NUMBER(3):=1; V VARCHAR2(1000); BEGIN WHILE N <=1000 LOOP V:=V||''||N; N:=N+1; END LOOP; DBMS_OUTPUT.PUT_LINE(V); END; / 3. write a program to print the even numbers from 1 to 100 DECLARE N NUMBER(3):=0; BEGIN WHILE N <=100 LOOP N:=N+2; DBMS_OUTPUT.PUT_LINE(N); END LOOP; END; / 4. Write a program to print the odd numbers from 1 to 100 DECLARE N NUMBER(3):=1; BEGIN WHILE N <=100 LOOP N:=N+2; DBMS_OUTPUT.PUT_LINE(N); END LOOP; END; / 5. write a program for multiplication table DECLARE A NUMBER(2):=&A; B   NUMBER(2):=1; C   NUMBER(3); BEGIN WHILE B <

RDBMS MINI PROJECT

  Capgemini class room training.   RDBMS MINI PROJECT ( SPRINT ) LIBRARY MANAGEMENT SYSTEM   Table of Contents Serial No. Topic Name Content Page No. 1.   Introduction 1.1 Setup checklist for mini project 3     1.2 Instructions 3 2.   Problem statement   2.1 Objective 4     2.2 Abstract of the project 4     2.3 Functional components of the project 4     2.4 Technology used 5 3.   Implementation in RDBMS LOT 3.1 Guidelines on the functionality to be built 6 4.   Evaluation 4.1 Evaluation 7     1.      Introduction This document outlines a mini project for the RDBMS LOT

sample tables

  --sample tables DROP TABLE EMP; DROP TABLE DEPT; DROP TABLE BONUS; DROP TABLE SALGRADE; DROP TABLE DUMMY;   CREATE TABLE EMP        (EMPNO NUMBER(4) NOT NULL,         ENAME VARCHAR2(10),         JOB VARCHAR2(9),         MGR NUMBER(4),         HIREDATE DATE,         SAL NUMBER(7, 2),         COMM NUMBER(7, 2),         DEPTNO NUMBER(2));   INSERT INTO EMP VALUES         (7369, 'SMITH',   'CLERK',      7902,         TO_DATE('17-DEC-1980', 'DD-MON-YYYY'),   800, NULL, 20); INSERT INTO EMP VALUES         (7499, 'ALLEN',   'SALESMAN',   7698,         TO_DATE('20-FEB-1981', 'DD-MON-YYYY'), 1600,   300, 30); INSERT INTO EMP VALUES         (7521, 'WARD',    'SALESMAN',   7698,         TO_DATE('22-FEB-1981', 'DD-MON-YYYY'), 1250,   500, 30); INSERT INTO EMP VALUES         (7566, 'JONES',   'MANAGER',    7839,         TO_DATE('2-APR-1981', 'DD-MON-YYYY'),   2975, NULL,