20 Unix FIND Command Examples For A Linux User

"Hope is a Waking Dream"
--Aristotle 
As the name, the "Find" command in Unix is used to find files or directories in the system. It has got a good number of options to help us find files in Unix based on time, user, etc. It is a basic Unix Command and it is available by default in most Unix versions - Linux, Solaris, etc.
Here is compiled a list of "Find" command examples that would assist us in our daily Unix tasks:

1. List all the files and folders, including the sub-folders starting from the current directory:
[root@centos55 sarat]# find .
.
./.mozilla
./.mozilla/plugins
./.mozilla/extensions
./filerename.txt
./database
./database/response
./database/response/emca.rsp
./database/response/standard.rsp
.........{truncated}

2. List files only in the current directory, don't drill down the subdirectories:
[root@centos55 sarat]# find . -maxdepth 1
.
./.mozilla
./filerename.txt
./database
./.bashrc
./.bash_logout
...... {truncated}
We can use -maxdepth to drill down to different levels of sub-directories e.g. "-maxdepth 3" to drill down to 3 levels.

3. Find only the folders/directories:
[root@centos55 sarat]# find  .  -maxdepth 1 -type d
.
./.mozilla
./database
./perl_modules
./perl_script

4. Find only the files:
[root@centos55 sarat]# find  .  -maxdepth 1 -type f
./filerename.txt
./.bashrc
./.bash_logout
./10201_database_linux32.zip
./.bash_profile
./.viminfo
./.bash_history

5. Find files with name:
[root@centos55 sarat]# find . -name "test.pl"
./perl_modules/IO-Tty-1.02/test.pl
./perl_modules/Expect-1.21/test.pl

6. Find files through the whole system:
[sarat@centos55 ~]$ find / -name "*.xml" 2>/dev/null
/etc/maven/maven2-depmap.xml
/etc/purple/prefs.xml
/etc/gconf/gconf.xml.defaults/%gconf-tree-mn.xml
/etc/gconf/gconf.xml.defaults/%gconf-tree-xh.xml
/etc/gconf/gconf.xml.defaults/%gconf-tree-ru.xml
/etc/gconf/gconf.xml.defaults/%gconf-tree-nso.xml
...................{truncated}
The re-direction "2>/dev/null" is used to redirect the error messages that may show up whenever the user does not have read permissions on a folder.

7. Find files specifying different directories:
[root@centos55 sarat]# find /opt /var . -name "*.txt"
/opt/tmp.txt
/opt/file.txt
/opt/oracle/product/10.2.0/db_1/OPatch/docs/Users_Guide.txt
................{truncated}
/opt/oracle/product/10.2.0/db_1/jdbc/Readme.txt
/var/www/icons/small/README.txt
/var/cache/yum/addons/mirrorlist.txt
./database/stage/Components/oracle.dbjava.common/10.2.0.1.0/1/DataFiles/Expanded/filegroup2/Readme.txt
./database/stage/Components/oracle.dbjava.common/10.2.0.1.0/1/DataFiles/Expanded/filegroup1/Readme.txt
................{truncated}

8. Find files with ignore case of file-name(case insensitive find):
[root@centos55 sarat]# find  . -iname "readme.txt"
./database/stage/Components/oracle.dbjava.common/10.2.0.1.0/1/DataFiles/Expanded/filegroup2/Readme.txt
./database/stage/Components/oracle.dbjava.common/10.2.0.1.0/1/DataFiles/Expanded/filegroup1/Readme.txt
./database/stage/Components/oracle.javavm.client/10.2.0.1.0/1/DataFiles/Expanded/filegroup2/readme.txt
./database/stage/Components/oracle.javavm.client/10.2.0.1.0/1/DataFiles/Expanded/filegroup1/readme.txt

9. Find files with size greater than 50MB:
[root@centos55 sarat]# find . -size +50M
./database/stage/Components/oracle.rdbms.install.seeddb/10.2.0.1.0/1/DataFiles/Expanded/filegroup1/Seed_Database.dfb
./10201_database_linux32.zip

10. Find files with size between 50MB and 100MB:
[root@centos55 sarat]# find . -size +50M -size -100M
./database/stage/Components/oracle.rdbms.install.seeddb/10.2.0.1.0/1/DataFiles/Expanded/filegroup1/Seed_Database.dfb

11. Find files of a particular user:
[root@centos55 sarat]# find / -name "readme.txt" -user oracle
/opt/oracle/product/10.2.0/db_1/javavm/doc/readme.txt
/opt/oracle/product/10.2.0/db_1/oc4j/j2ee/home/applib/readme.txt
/opt/oracle/product/10.2.0/db_1/oracore/zoneinfo/readme.txt
/opt/oracle/product/10.2.0/db_1/olap/api/doc/readme.txt
/opt/oracle/product/10.2.0/db_1/precomp/doc/pro1x/readme.txt
/opt/oracle/product/10.2.0/db_1/install/readme.txt

Time related find files:
Use -mtime for modification time in days, -atime for last accessed time in days, and -ctime for last changed time. Similarly, for time in minutes use -mmin, -amin, -cmin.

12. Find files modified less than 10 mins ago:
[sarat@centos55 ~]$ find . -maxdepth 1 -mmin -10
.
./filerename.txt
./.viminfo

13. Find files modified more than 10 mins ago:
[sarat@centos55 ~]$ find . -maxdepth 1 -mmin +10
./.mozilla
./database
./.bashrc
./.bash_logout
./10201_database_linux32.zip
./.bash_profile
./.bash_history
./perl_modules
./perl_script

14. Find files accessed more than 10 mins back but less than 30 min:
[sarat@centos55 ~]$ find . -cmin +10 -cmin -30
.
./filerename.txt
./.viminfo

15. Find files modified less than 1 day ago:
[sarat@centos55 ~]$ find . -maxdepth 1 -mtime -1
.
./filerename.txt
./.viminfo
./.bash_history
./perl_modules
./perl_script

16. Find files modified more than 1 day ago:
[sarat@centos55 ~]$ find . -maxdepth 1 -mtime +1
./.mozilla
./database
./.bashrc
./.bash_logout
./10201_database_linux32.zip
./.bash_profile

17. Find and delete all .dat files under current directory:
# find . -name "*.dat" -exec rm -rf '{}' \;
The argument '{}' passes each found file into the "rm -rf" command line. The "\;" argument indicates the end of exec command.
Or we can use another command with xargs:
# find . -name "*.dat" xargs rm -rf

18. Find files which contains a particular word(searching for word "strict"in all .pl files):
[sarat@centos55 ~]$ find . -name "*.pl" | xargs grep "strict"
./perl_modules/IO-Tty-1.02/test.pl:use strict;
./perl_modules/Expect-1.21/test.pl:use strict;
Similarly using exec:
[sarat@centos55 ~]$ find . -name "*.pl" -exec grep "strict" '{}' \; -print

19. Find files and sort by size:
[sarat@centos55 ~]$ find . -type f -size +10M | xargs du -sh | sort -rn
639M    ./10201_database_linux32.zip
90M     ./database/stage/Components/oracle.rdbms.install.seeddb/10.2.0.1.0/1/DataFiles/Expanded/filegroup1/Seed_Database.dfb
35M     ./database/stage/Components/oracle.sysman.console.db/10.2.0.1.0/1/DataFiles/filegroup8.jar
27M     ./database/stage/Components/oracle.rdbms/10.2.0.1.0/1/DataFiles/filegroup26.jar
26M     ./database/stage/Components/oracle.sysman.console.db/10.2.0.1.0/1/DataFiles/filegroup9.jar
25M     ./database/stage/Components/oracle.javavm.containers/10.2.0.1.0/1/DataFiles/filegroup5.jar
24M     ./database/stage/Components/oracle.javavm.server/10.2.0.1.0/1/DataFiles/filegroup1.jar
22M     ./database/stage/Components/oracle.sysman.repository.core/10.2.0.1.0/1/DataFiles/Expanded/filegroup3/emDB.jar
21M     ./database/stage/Components/oracle.jdk/1.4.2.0.8/1/DataFiles/sol_jre_lib.1.1.jar
20M     ./database/stage/Components/oracle.swd.jre/1.4.2.8.0/1/DataFiles/filegroup2.jar

20. Find and print folder size excluding particular folder:
[root@centos55 ]# find . -maxdepth 1 -type d |  egrep -v 'database' | xargs du -sh
1.3G    .
24K     ./.mozilla
4.4M    ./perl_modules
8.0K    ./perl_script

No comments:

Post a Comment