find command is one of the versatile command in UNIX and Linux and I used it a lot in my day to day work. I believe having knowledge of find command in UNIX and understanding of its different usage will increase your productivity a lot in UNIX. If your works involve lots of searching stuff on Linux machine or if you are a java or C++ programmer and your code resides in UNIX, find command can greatly help you to look for any word inside your source file in the absence of an IDE, It is the alternative way of searching things in UNIX. grep is another Linux command which provides similar functionality like find but in my opinion later is much more powerful than grep in UNIX.
I have been sharing my experience on Unix and Linux command and its different options, usage and example and this article is in continuation of my earlier post like How to convert IP address to hostname in Linux . If you are new here you may find those tips useful for your day 2 day development and support work.
10 tips on find command in UNIX
Here I am listing down some of the way I use find in Unix or Linux box regularly, I hope this would help some one who is new in UNIX find command or any developer who has started working on UNIX environment. this list is by no means complete and just some of my favorites , if you have something to share please share via commenting.
How to run last executed find command in Unix – Example 1
!find will repeat the last find command executed. It saves lot of time if you re searching for something and you need to execute same command again and again. In fact "!" can be used with any command to invoke previous run of that command.
find . -name "*.java" --last find command executed
./OnlineStockTrading.java
./StockTrading.java
How to find files which has been modified less than one day, minute or hour in Unix:
find -mtime is used to search files based upon modification time. This is infact my favorite find command tips while looking out some production issues just to check which files have been modified recently, could be likely cause of issue, believe me it helps a lot and many a times gives you enough hint of any problem due to intended or unintended file change. Along with –mtime, there are two more options related to time, find -atime which denote last accessed time of file and find –ctime denotes last changed time. + sign is used to search for greater than, - sign is used to search for less than and without sign is used for exact. For example find –mtime -1 will search all files which has been modified
javin@testenv1 ~/java : find . -mtime -1 (find all the files modified less than 1 day)
.
./StockTrading.java
javin@testenv1 ~/java : find . -mtime +1 (find all the files modified more than 1 day)
./.vimrc
./OnlineStockTrading.java
./StockTrading.java~
In this example since we have only modified StockTrading.java some time back it has shown on find –mtime -1, rest of files are not touched today so they are appearing as modified more than 1 day while there is no file which has been modified exactly one day.
How to find all the files and directories which holds the 777 permission in Unix box – Exmaple 3
find –perm option is used to find files based upon permissions. You can used find –perm 444 to get all files which allows read to owner, group and others. If you are not sure how those 777 and 444 numbers comes up, see my post on file and directory permission in Unix and some chmod examples to change permissions in Unix.
./.vimrc
./OnlineStockTrading.java
Example 4 – Case insensitive search using find in Unix
On a different note find and grep command is also a favorite topic during Unix interview and interview often asked questions during interviews on both system admin and application developer jobs.
UNIX find command and xargs Example
Example 5 - How to delete temporary files using find command in Unix?
In order to delete files you can use either –delete option of find command or use xargs in combination. Its better to create house keeping script for such task which can perform cleanup on periodic basis.
Use of xargs along with find gives you immense power to do whatever you want with each search result. See another example below , also its worth considering use of -print0 to avoid problems with white space in the path when piping to xargs (use with the xargs -0 option) as suggested by Ebon Elaza.
Example 6 - How to find all text file which contains word Exception using find command in Unix ?
find . –name "*.java" –print | xargs grep “MemoryCache”, this will search all java files starting from current directory for word "MemoryCache". we can also leave -print option in all cases because its default for UNIX find command as pointed out by Ben in comments. You can further sort result of find command using Sort command in unix.
Example 7 - Finding files only in current directory not searching on sub directories:
While using find command I realized that some time I only need to find files and directories that are new , only in the current directory so I modified the find command as follows. –type option can be used to specifiy search for only file, link or directory and maxdepth specifies how deep find has to search.
Means type file, last modified 15 minutes ago, only look at the current directory. (No sub-directories)
Example 8 – How to find files based on size in Unix and Linux
Following find example shows how you can use find –size option to find files based upon certain size. This will find all files in current directory and sub-directory, greater than some size using find command in Unix:
This find example lists all files that are greater than 10,000 bytes, but less than 50,000 bytes:
Example 9 – How to find files some days older and above certain size
We can combine –mtime and –size to find files which are some days old and greater than some size in Unix. Very common scenario where you want to delete some large old files to free some space in your machine. This example of find command will find which are more than 10 days old and size greater than 50K.
10) You can use "awk" in combination of find to print a formatted output e.g. next command will find all of the symbolic links in your home directory, and print the files your symbolic links points to:
"." says starts from current directory and include all sub directory and "-type l" says list all links.
Hope you find this useful , please share how you are using find commands and we can benefit from each others experience and work more efficiently in UNIX.
Tip:
$* : $* is one of the special bash parameter which is used to expands positional parameters from position one.
if you give double quotes and expansion is done within double quotes, it only expands to a single word and corresponding value of each parameter will be separated by the first letter of the IFS environment variable defined in bash. Do let me know how do you find these find examples .
How to use find command on file names with space in Unix:
./cash equity trading ./equity~
ls: cannot access ./cash: No such file or directory
ls: cannot access equity: No such file or directory
ls: cannot access trading: No such file or directory
-r--r--r-- 1 stock_trading cash_domain trading 0 Jul 15 11:42 ./equity~
xargs: WARNING: a NUL character occurred in the input. It cannot be passed through in the argument list. Did you mean to use the --null option?
ls: cannot access ./cash: No such file or directory
ls: cannot access equity: No such file or directory
ls: cannot access trading: No such file or directory
-rw-r--r-- 1 stock_trading cash_domain trading 0 Jul 21 09:54 ./cash equity trading
-r--r--r-- 1 stock_trading cash_domain trading 0 Jul 15 11:42 ./equity~
In conclusion always use find -print0 along with xargs -0 if you see slightest possibilities of file names containing space in UNIX or Linux.
Important point about find command in Unix and Linux:
10 example of networking command in Unix
10 Example of tar command in Unix
5 Example of kill command in Unix and Linux
VI Editor examples and tips for beginners
Unix command tutorials for beginners and intermediate
Thanks for the efforts on gathering useful content and sharing here. You can find more question and answers on Unix and Linux in the following forum.
ReplyDeleteUnix and Linux technical discussion forum