2

I'd like to use find command to search all files in one directory:

find . * -exec ll {} \;

Result shows four (!) results per file:

  • file123
  • ./test/file123
  • file123
  • test/file123

Is it possible to show just the second result ./test/file123 using find-out-of-the-box-command (so without using things like | grep '^\./')?

I'm using latest version of HP/UX.

  • 2
    Where do you have this command from? just use find . - or just - ll. The difference is that find will find files recursively. Also note. Don't use ll in scripts! ll is just an alias (In Ubuntu it means ls -alFh). This is not portable in any ways. – pLumo Sep 10 '18 at 9:10
5

You are getting four lines of output because you you ask find to search the current directory (.) as well as everything that * expands to.

For the current directory, find will proceed to invoke ll on the things that it finds, which will be the current directory, the test directory and the file in the test directory. This lists file123 twice.

It will then search everything that * expands to, which will be the test directory. Now it will use ll on test again and on the file within. This lists your file twice again.

Instead, if you're looking for a regular file, use -type f. And you don't have to use ll (or ls) to get the results from find as it will list the found pathnames by default. Also, just give find the current directory (or test) as the search path:

find . -type f

If you know that you are looking for a file with the exact name file123:

find . -type f -name 'file123'

If you want ls -l-like output rather than a pathname:

find . -type f -name 'file123' -ls
  • If you want to limit the results to files within those top-level directories, like "test", you might consider using the -maxdepth and -mindepth option as well. In this case: -mindepth 2 and -maxdepth 2. – Todd Walton Sep 10 '18 at 15:32
  • @ToddWalton, that's a GNU extension, now supported by a few other implementations but I doubt HPUX's is one of those. See also the better -depth 2 with NetBSD find. See delete folders older than 1 day for instance for a POSIX equivalent – Stéphane Chazelas Sep 10 '18 at 18:00
5
find . * -exec ll {} \;

Passes ., and the list of non-hidden files in the current directory (expanded from * by the shell), including the test subdirectory (and if any of those files started with - or if there was a file called ! or (, )... that would cause find to report a syntax error).

So you're telling find to look for files recursively in ., but also in test. So find will find test twice and also test/file123 twice.

ll which I suppose is a script that does ls -l is a tool to list details of files or for files of type directory, the details of all the files in that directory (unless you pass the -d option).

So ll will be called with ./test and test as arguments in which cases if will list the details of the files in its content (including test/file).

And it will be called with ./test/file123 and test/file123.

Here, you'd want:

find . -exec ls -ld {} +

(using + means that more than one file will be passed as arguments to ls which is more efficient and also lets ls sort that list of files).

Your find may even support a -ls predicate which behaves a bit list ls -lsid:

find . -ls
  • I never use find . myself because this is redundant. find will always search in the current directory if the directory is unspecified. – Fox Sep 10 '18 at 13:48
  • 2
    @Fox, that's a GNU extension. Many find implementations (including the original one as probably found on the OP's HPUX) require at least one file. The POSIX find specification does as well. – Stéphane Chazelas Sep 10 '18 at 13:54
0

Please note that find will search recursively in all the directories.
Find will return not only files, but also directories!

If you want to list all files within the test directory, all you need is the following command:
find test/ -type f

Also, I don't know what you are trying to achieve, but check your find manpage... You are using -exec ll {} \; and I suspect there's probably a find option that will do what you need (filter files by size, modification date, etc.)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.