Working with FIND command

Hi ,
In /home/etc/files path ran the following command

find . -name 'ABC*' | wc -l 

The output of the above command is 25 as expected

In path /home path ran the following command

find . -name '/home/etc/files/ABC*' | wc -l 

The output of the abvoe command is 0 .

Why the above command is not giving the correct output when executing in the home directory ?

Please Help

The name argument should be a file name (or glob), not a path. . (the path) should contain the path.

find /home/etc/files -name "ABC*" | wc -l
1 Like

The dot in

find . 

invokes the search from present working directory. The command is trying to find files under present directory and with name

/home/etc/files/ABC*

If you had a file as below then it would be appearing in the result.

/home/etc/files/home/etc/files/ABC*

To find for files under /home/etc/files enter below command

find /home/etc/files -name 'ABC*' | wc -l
1 Like

Your analysis is incorrect.

-name only matches against a file's name (the basename of a multi-component pathname). There are two characters that are forbidden in UNIX filenames: / (which is reserved as the pathname component delimiter) and the null byte (which the C standard library uses to terminate strings). Since the pattern argument in -name '/home/etc/files/ABC*' contains one of the illegal characters, / , that usage of -name can never match anything and will always evaluate falsely, hence zero matches.

Regards,
Alister

1 Like