I know when I started working in unix and now after learning find command in unix how much difference it make. I really love your practical UNIX command examples. Unix find command is something like google search in unix. name "*.tmp" -print | xargs grep "ERROR" name *.log -print | xargs grep "ERROR"Ībove command wont run.becoz there is no quotes in throws So I still cannot figure out how the -prune works.įind. The following prune works, but the -mtime +5 doesn't work. It still brings back subdirectories.īrings back all subdirectories.
Your prune example does not work on Solaris.
type f -name "*.txt" -exec rm -f \ you may want to use the -delete option of the find command. Hi Eric, Ebon, Thomas and Hobbit Thanks for bringing point about "print0", I missed that but its indeed worth to remember. Thanks Anonymous for letting us know about "-ls" option with find, I usually use xargs with many commands so it became an habit. Especially good for cygwin users dealing with files created by non-programmers.įor the "xargs ls -l", there is a find option "-ls" that performs the same in my experience. The -print0 separates its output with null bytes, and -0 tells xargs its args are separated by null bytes. When using xargs and you know some files will contain spaces, use -print0 and -0, like so: 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'. name '*.txt' -print0 | xargs -0 grep -i 'some text'
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. 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. If you are new here, you may find these tips useful for your day-to-day development and support work.Įxcellent examples, but did you know you can omit all of those "-print" parameters? Print is the default command for find.
#Unix find file how to#
If you can be even able to remember all the 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.īy the way, I have been sharing my experience on Unix and Linux command, and their different options, usage, and example, and this article are in continuation of my earlier post like how to convert an IP address to the hostname in Linux. Like any other command strength to find lies in its various options, which is worth learning, but, to be frank, hard to remember. It is the alternative way of searching for things in UNIX grep is another Linux command which provides similar functionality like find but in my opinion, later is much more potent than grep in UNIX.
#Unix find file code#
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 significantly help you to look for any word inside your source file in the absence of an IDE. I believe having a 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 systems, e.g. The find command is one of the most versatile commands in UNIX and Linux, and I used it a lot in my day-to-day work.