Monday, April 23, 2012

find files by name or part of name or extension

   In one of our earlier articles on find, we saw about finding files depending on their modification time. In this article, we will see about the finding files using the filename or using a part of the file name or using the extension.

1. To find all the files ending with .c :
$ find . -name "*.c"
      The '.' indicates current directory. The default behavior of find makes this command to search in the current directory and also in all the sub-directories under the current directory for the files ending with .c.

2. To search for all the .c files only in the current directory:
$ find . -maxdepth 1 -name "*.c"
    The maxdepth switch is used to control the number of levels to descend. maxdepth value of 1 indicates to descend only one level, which in other words is only the current directory alone.

3. To find all the .c and .h files:
$ find . -name "*.[ch]"
     [ch] will match either c or h. And hence .c and .h both will be matched.

4. To find all the .cpp and .h files:
$ find .  \( -name *.cpp -o -name *.h \)
     In the earlier example, we could use [ch] since both were one character. The same cannot be done in this example. Hence, we need to give two separate conditions on the 'name' switch using the OR(-o) condition. The backslash(\) is given to escape the bracket.

5. To find all the files whose names begin with 'f' :
$ find . -name "f*"
6. To find all the files whose names begin with 'f' and end with '.c' :
$ find . -name "f*.c"
7. To find all the files except the .c files:
$ find . ! -name "*.c"
   The '!' is used to negate the condition.

8. To find all the files other than the .c and .h files:
$ find . !  \( -name "*.c" -o -name "*.h" \)
      Since the condition inside the brackets tell to find .c or .h files, the negation makes it to search for files anything other than .c and .h file.

9. To find all the files:
$ find . -type f
     The type switch can be used to find files of specific type. Without the type switch in the above example, the find command would have given files and directories as well. '-type f' indicates to find the files alone. To find the directories alone, we can use '-type d'.

10. To find all the .c files which contain the word 'stdio':
$ find . -name "*.c" -exec grep -l stdio '{}' \;
    Using the exec switch, we can execute more commands on the output of the find command. Here, we grep for the word stdio on all the files  resulted from the find command and display the matching file names alone. '{}' is a place holder for the output of the find command.

   The same can also be achieved as below:
$ find . -name "*.c" | xargs  grep -l stdio
11. To find all the .c files which does not contain the word 'stdio':
$ for i in `find . -name "*.c"`
   grep -q stdio $i || echo $i
    This cannot be achieved only with the find command itself like the earlier examples. We loop on the files found by the find command, and print those files names which does not contain the word stdio. -q option of grep is to suppress the default grep output from getting printed since only the file names should get printed.

12.  The find command output always displays the file names with the relative file path. To get the file names alone without the relative path:
$ find . -name "*.c" | sed 's^.*/^^'
    The sed command removes everything till the last slash and hence only the filename remains.
Related Posts Plugin for WordPress, Blogger...

1 comment: