find command

Using "find" command in *NIX systems

1) To find a file say "hello.c" in /usr/xyz directory, do
#find /usr/xyz -name "hello.c"

If you dont know the exact location, you can search in "/" dir, and if you dont know the exact file name, you can use "*" like
#find / -name "he*.c" or
#find / -name "he*.c*"

2) To find a string in some specific files, lets say you want to find "some function" in all C++ files (having extension ".cpp") in, lets say "/usr/xyz/prj1" directory,

#find /usr/xyz/prj1 -name "*.cpp" | xargs grep "some function"

3) You want to do something with the result of find command, eg you want to see listing in time order (ls -ltr) of the output of find command
#find . -name "a*.pdf" -exec ls -ltr {} +
Here we will be seeing all the pdf files that start with "a" in reverse time wise list (ls -ltr)

or lets say you want to delete all the files with .tmp extension in /mytmp dir, you can do so by
#find /mytmp -name "*.tmp" -exec rm {} +

No comments:

Books I like

  • Inside C++ Object Model
  • Unix Network Programming - Stevens
  • Professional-c++-programmer-to-programmer - wrox
  • Beautiful-code-leading-programmers-explain-how-they-think-theory-in-practice