The find command is one of the versatile commands in UNIX and Linux and I used it a lot in my day to day work. I believe having good knowledge of find command in UNIX and understanding of its different options and usage will increase your productivity a lot in UNIX based operating system e.g. Redhat Linux or Solaris. If you are a QA, support personnel, and your works involve lots of searching text 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.
Like any other command strength of find lies in its various options, which is worth learning, but, to be frank, hard to remember. If you can even able to remember all options mentioned in this article, you will be taking much more advantage of the find command, than average developers, QA, support people and Linux users.
By the way, 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 the hostname in Linux. If you are new here you may find those tips useful for your day-to-day development and support work.
Here I am listing down some of the ways I use to find in Unix or Linux box regularly, I hope this would help someone 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.Like any other command strength of find lies in its various options, which is worth learning, but, to be frank, hard to remember. If you can even able to remember all options mentioned in this article, you will be taking much more advantage of the find command, than average developers, QA, support people and Linux users.
By the way, 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 the hostname in Linux. If you are new here you may find those tips useful for your day-to-day development and support work.
10 Tips on find command in UNIX
If you love to read books, you can also take a look at The Linux Command Line: A Complete Introduction, a great book and must read for any programmer, tester, system administrator, security guy or developers, who work on UNIX and Linux based environment. It not only teaches about find and grep but also introduces several useful commands, which probably gone unnoticed by many of us
How to run last executed find command in Unix – Example 1
!find will repeat the last find command executed. It saves a lot of time if you re searching for something and you need to execute the same command again and again. In fact "!" can be used with any command to invoke the previous run of that command, it's one of the special shell characters.
javin@testenv1 ~/java : !find
find . -name "*.java" --last find command executed
./OnlineStockTrading.java
./StockTrading.java
find . -name "*.java" --last find command executed
./OnlineStockTrading.java
./StockTrading.java
How to find files which have been modified less than one day, minute or hour in Unix:

javin@testenv1 ~/java : find . -mtime 1 (find all the files modified exact 1 day)
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~
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 hold the 777 permission in Unix box – Example 3
javin@testenv1:~/java $ find . -perm 644
./.vimrc
./OnlineStockTrading.java
./.vimrc
./OnlineStockTrading.java
I use this find command example to find out all the executable files, you can also modify it to find all the read-only files or files having written permission etc by changing permissions e.g. to find all read-only files in current directory: find . –perm 555 Here "." or period denotes the current directory. You can replace it with any directory you want.
Example 4 – Case insensitive search using find in Unix
How to do case insensitive search using find command in Unix? Use option “-i" with name, by default find searches are case sensitive. This option of the find is extremely helpful while looking for errors and exceptions in the log file.
find . –iname "error" –print ( -i is for ignore )
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 and xargs command Example
Now we will see some UNIX find command example combined with xargs command. A combination of find and xargs can be used to do whatever witch each file found by find command, for example, we can delete that file, list content of that file or can apply any comment on that file.
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. It's better to create housekeeping script for such task which can perform cleanup on a periodic basis.
find . -name "*.tmp" -print | xargs rm –f
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 whitespace 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 the word "MemoryCache". we can also leave -print option in all cases because its default for UNIX finds command as pointed out by Ben in comments. You can further sort the result of find command using Sort command in unix.
find . –name "*.txt" –print | xargs grep "Exception"
Example 7 - Finding files only in current directory not searching on subdirectories:
While using find command, I realized that sometimes I only need to find files and directories that is new, only in the current directory so I modified the find command as follows. You can use find –type option to specifiy search for the only file, link or directory and maxdepth specifies how deep find has to search.
find . -maxdepth 1 -type f -newer first_file
Another way of doing it is below:
find . -type f -cmin 15 -prune
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:
find . -size +1000c -exec ls -l {} \;
Always use a c after the number, and specify the size in bytes, otherwise, you will get confuse because find -size list files based on the size of the disk block. to find files using a range of file sizes, a minus or plus sign can be specified before the number. The minus sign means "less than," and the plus sign means "greater than." Suppose if you want to find all the files within a range you can use find command as in below example of find:
find . -size +10000c -size -50000c -print
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. A 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.
find . -mtime +10 -size +50000c -exec ls -l {} \;
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:
find . -type l -print | xargs ls -ld | awk '{print $10}'
"." says starts from current directory and include all subdirectory 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 parameters 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 .
Here is a nice summary slide of some useful find command examples, which will help you to get more from this excellent command line tool in UNIX and Linux:
How to use find command on file names with space in Unix:
I have received a lot of comments from my readers on not mentioning about find -print0 and xargs -0 on find examples, so I thought to include this as well. When we don't specify any expression after find command the default option is -print which prints the name of each found files followed by \n or newline.since we mostly pipe the output of find command to xargs -print could cause a problem if file name itself contain a new line or any form of white space. To resolve this issue instead of -print use -print0.
The difference between find -print and find -print0 is, print0 display file name on the stdout followed by an "NUL" character and then you can use xargs -0 commands to process file names with a null character.
let's see UNIX find command example with a file name having space in them:
The difference between find -print and find -print0 is, print0 display file name on the stdout followed by an "NUL" character and then you can use xargs -0 commands to process file names with a null character.
let's see UNIX find command example with a file name having space in them:
javin@testenv1:~/test find . -name "*equity*" -print
./cash equity trading ./equity~
./cash equity trading ./equity~
You see here "cash equity trading" has space in there name
javin@testenv1:~/test find . -name "*equity*" -print | xargs ls -l
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~
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~
Now if we pass this to xargs, xargs treat them as three separate files.
javin@testenv1:~/test find . -name "*equity*" -print0 | xargs ls
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
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
Now to solve this, we have used find command with -print0 which appends NUL character on file name but without xargs -0, xargs command would not able to handle those inputs.
javin@testenv1:~/test find . -name "*equity*" -print0 | xargs -0 ls -l
-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~
-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~
Now you can see with find -print0| xargs -0 it looks good
In short, always use find -print0 along with xargs -0 if you see slightest possibilities of file names containing space in UNIX or Linux.
In short, 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:
Here are some of the important and interesting things to know about powerful find command, most of these points are contributed by various people in comments and big thanks to all of them for sharing their knowledge, you should definitely check out comments to know more about find command:
1. find –print and find is same as –print is a default option of the find command.
2. find –print0 should be used to avoid any issue with white space in file name or path while forwarding output to xargs, also use xargs -0 along with find –print0.
3. find has an option called –delete which can be used in place of -exec rm {} \;
Further Learning
Linux Command Line Basics
Linux Command Line Interface (CLI) Fundamentals
Learn Linux in 5 Days and Level Up Your Career
Related post:
- How Linux Works: What Every Superuser Should Know
- 10 example of networking command in Unix
- 10 Example of tar commands in Unix
- 5 Example of kill commands in Unix and Linux
- VI Editor examples and tips for beginners
- Unix command tutorials for beginners and intermediate
- The Linux Command Line: A Complete Introduction
48 comments :
Excellent examples, but did you know you can omit all of those "-print" parameters? Print is the default command for find.
Piping a list of file nnames through "xargs grep" will fail if "find" locates files that contain spaces in their names, since by default "xargs" splits the incoming list on spaces. In that case, use "-print0" on the "find" command to separate file names with nulls, and "-0" on "xargs" to tell it the file names are null separated.
For example,
$ cd ~/Documents
$ find . -name '*.txt' -print0 | xargs -0 grep -i 'some text'
Note I've also quoted '*.txt' on the find command to prevent the shell from expanding any file names in the current directory that may end with '.txt'.
When using xargs and you know some files will contain spaces, use -print0 and -0, like so:
find . ... -print0 | xargs -0 cmd
The -print0 separates its output with null bytes, and -0 tells xargs its args are separated by null bytes. Especially good for cygwin users dealing with files created by non-programmers.
For the "xargs ls -l", there is a find option "-ls" that performs the same in my experience.
Thanks Anonymous for letting us know about "-ls" option with find , I usually use xargs with many commands so it became an habit.
Hi Eric , Ebon , Thomas and Hobbit Thanks for bringing point about "print0" , I missed that but its indeed worth to remember.
how about using `find . -type f -name "*.txt" -exec rm -f {} \;` instead of xarg?
Nice post Javin
This is my new blog
http://java-j2ee-hibernate-spring.blogspot.com/
Hi Anonymous,
you are right we can use `find . -type f -name "*.txt" -exec rm -f {} \;` instead of xarg as well , both will serve the same purpose. I am bit more comfortable with xargs then -exec so I prefer to use xargs along with find command in unix but you can use -exec as well.
Hi Anonymous,
instead of -exec rm {} \; you may want to use the -delete option of the find command.
Your prune example does not work on Solaris. It still brings back subdirectories.
find . -type f -mtime +5 -prune
Brings back all subdirectories. The following prune works, but the -mtime +5 doesn't work. So I still cannot figure out how the -prune works.
find . \( -name "*/output/*" -o -name "*/logs/*" \) -prune -mtime +5 -print
find . -name *.log -print | xargs grep "ERROR"
above command wont run..becoz there is no quotes in that...it throws
paths must precede expression
Usage: find [-H] [-L] [-P] [path...] [expression]
-bash-3.2$
Below is the corrected command:
find . -name "*.tmp" -print | xargs grep "ERROR"
unix find command is something like google search in unix. I really love your practical UNIX command examples. I know when I started working in unix and now after learning find command in unix how much difference it make. unix find command is most powerful utility I come across.
does anyone know what is these special characters $* , $@, $$, $#, #_, $?, specially $@ and $# I can see this in most of bash script but don't understand it. $# mostly seen on top.
Hi Anonymous, $* or $# or $@ are special bash parameters they have special meaning inside bash script, $# is used to count number of element, $$ used to get process ID , $* and $@ are similar and used to expand positional parameter but they behave differently if they come inside double quotes, $? is used to get exit status of last executed command and $! is used to get process ID of most recent executed command, you can get detailed description here list of special bash parameters in Unix
how about grep command , do you also use grep command in unix or you just use find command. I know unix find command is great but I have found unix grep more easy to use than find command. Its personal choice but find and grep are both powerful and effective.
fantastic examples of find command, though some find examples are ok, I like most of examples and looking forward for more tips on unix find command.
Hi, I am working on support for electronic trading DMA (Direct to market) access system. which is used to trade cash equities, stocks and options.my system is client connectivity system which connect to different client using FIX Protocol and I want to thank your for your Unix tips on find command and your tips on FIX Protocol, I have benefited a lot.
your find examples will fail if any file will contain spaces in between them. you should use -print0 with find and -0 with xargs to safely handle files which has space in there name.
Can we use grep command in unix in place of unix find command whenever required. I find syntax of grep is more easy than find command and wanted to use grep as much possible than unix find command.
@Anonymous, both find and grep commands has there own place and both can be used though I see unix find command more powerful than grep command in unix.
Hi Guys.... The post is quiet useful. Came across this while trying to get a solution to another problem, am stuck in. So thought to share it here.
I have a set of XML Files in a directory, and I need to rename it according to business requirements. The requirement says, that I need to rename the files to a value of an element in the XML File.
Say the XML file, has an element named ; with value as 987654. Now I need to take this value '987654', and rename my file to 987654.xml .
I am trying to get it through a UNIX Shell script, but getting stuck some where or the other.
Any help or hint will be very much helpful....!!!!
unix find command is my favorite. completely agree with power of find command and thank for those unix find examples extremely useful for beginners.
HI I have one question,
i have some jar files in a directory and in subdirectories , i need to find the jar having some filename , how come we do using the find command ?
Poor old Unix! Any good file manager could/should do this with a few clicks without the need of such cryptic commands. Why these commands are not more intuitiv? This is still 1970's style where every bit and byte counted but nowadays it should be more ergonomic and user (not nerd) oriented - even on a text console. It would be great that Unix evolves this way one day.
Hi, my requirment is i want to see the last modified file in my directory using find?
guys ..you can get more details about find command on
unixbasic.blogspot.com
thanks a lot for this find one liners in Linux. I have been using your find command in Linux and Unix operating system from last few weeks and it helped me a lot. I also liked your grep command tutorial in Unix and Linux .those are my favorite. please do share some more useful unix and Linux command examples.
I think best example of find command in unix is finding files by modified time, creation time etc. but power of unix find command is you can not only find files by names, types, modified time but also on several other attributes of file like finding read only files, finding all executable files in linux etc.
Very good examples. All are very much useful in the daily work of any one who uses UNIX.Thanks ,,,
unix command to find last modified file and time of file/directory
@Anonymous you can use 'ls -lrt' to find last modified file and time. find can only search files in range of time specified by -mtime I guess.
few of my own find command tips :
man find | grep --context=5 regex
This is my way to know about any find command line option , it will find word regex and print 5 lines around it, which is more than enough to know about how to use find with regex option
find -regex '.*.html'
-regex allows you to apply regular expression along with find. it apply regular expression on path name, instead of search for file. Since all files in current directory starts with . it matches pattern starts with . than any character any number of time and ending with .html
find -depth is another option which is similar to find -maxdepth
fantastic tutorial on find command. You have done an awesome job by compiling these examples on find command. I always forget different options like -mtime or -size, now I can simply take a printout of this and have with me.
When i use the find command, it gives me the complete path of the file (/home/.../.txt). Is there any way by which I can only get the without the complete path?
@ Anonymous written on August 27.... From my small understanding of unix, you could type a command that uses piping using a keyword: ls /home | grep keyword | more
I believe that would show it.
Another useful example of find command is that you can you can use find command to find all soft link in any directory on any UNIX based operating system e.g. Linux, Solaris or BSD. by using find -type you can see all symbolic links :
find /home/username -type -l -print
will display all soft links in /home/username directory. isn't it great use of find command ?
Hi, I want to find all files which is more than one month old in Unix box, particularly in log directory and then want to create a tar file or zip file of that with name as file-month-year e.g. app-december-2012.tar or .gz. I have figured out command to find all files which are more than 31 days old in log directory e.g.
find . -type f -mtime +31 -print
but I don't know how to create a tar file out of them and later remove all those files from directory. Please share UNIX script for doing that.
is there any way to run just ones "find" and output to 4 different files based on different mtime ?
find /DATA/HR -type f -mtime -30 -ls >> $data_feed/hr/dlt30.dat
find /DATA/HR -type f -mtime +30 -mtime -60 -ls >> $data_feed/hr/d30to60.dat
find /DATA/HR -type f -mtime +60 -mtime -90 -ls >> $data_feed/hr/d60to90.dat
find /DATA/HR -type f -mtime +90 -mtime -182 -ls >> $data_feed/hr/m3to6.dat
what is find command to search a file in linux?
What is find command to find a string in file unix
what is find command to search a string in a file?
what is find command to delete files
What is find command to find large files in unix
you're not doing yourself any favors for asking for answers to your homework. figuring it out on your own is parting the learning process! read the man page!
Instead of xargs -0 one can also use xargs --null option to work with files with space. Its very useful to find some files in directory and then find something inside those files. I love this command truly powerful.
find . -name "*.txt" print0 | xargs --null grep "John"
On linux find, -exec supports the \+ as well as \;
This appends the filenames to the same command and consequently executes faster as it doesn't need to spin up a new process each time.
For instance:
find . -iname '*.txt' -exec rm {} \;
would execute
rm a.txt
rm b.txt
...
find . -iname '*.txt' -exec rm {} \+
would execute
rm a.txt b.txt ...
This may not work with other versions, for instance the one that comes with SunOS. Check the man page.
great learning experince
Anonymous
I have tried to answer your questions
q) what is find command to search a file in linux?
ans) find . -type f -name "file1*"
What is find command to find a string in file unix
ans) same as next question so same answer
Q) what is find command to search a string in a file?
ans ) Below command will search all files with the name "file" and will search a pattern "udzial Means share" in them
find . -name "file" | xargs grep -i "udzial means share"
q) what is find command to delete files
ans) below command will find all file of size > 20 MB and will delete them
find . -size +20000000c|xargs rm -f
also if you want to delete by some name pattern
find . -name "udzial*" |xargs rm -f
q) What is find command to find large files in unix
Ans) below command will find all file of size greater than 10 KB(10000 bytes)
find . -size +10000c
regards
www.udzial.com
Udzial Means Share
I have a file name in one of my folder and i want to assign that file name to a variable after searching in that folder.. can u pls tell me a sample for getting file name to a variable
@anonomous
If you have a file, why to find that ?Anyways, here is what i tried. You would be required to try more yourself.
var=`find . -name "file_name" -print`
It was very userful blog. Thanks for sharing.
find . -name "*equity*" -print
./equity
./cash equity trading
Post a Comment