0

I want to look into a folder for some files but I do not want to look into any subdirectories.

I do not want to exclude a directory but All!

I have tried this from

find "$dir" -name '*.out' -type d -prune

but the results is the results are nothing. Any help?

0

Did you try looking at the manual?

The option you are looking for is -maxdepth, as @StevenPenny already wrote.

One reason why results are nothing is (from the manual)

If the whole expression contains no actions other than -prune or -print, -print is performed on all files for which the whole expression is true.

So as your options include -prune, there is no implicit -print. Your command would only prune directories named *.out.

If you really want to use -prune, do this:

find "$dir"/* -type d -prune -o -name '*.out' -print

Note that this uses "$dir"/*, because "$dir" is a directory and would be pruned. This negates the advantage of find that it is not limited by the maximal argument length

  • 1
    By using "$dir"/* as the starting point, you would miss any hidden names in the $dir directory. – Kusalananda Sep 20 '18 at 19:45
2

It seems as if you don't actually need find at all here.

for pathname in "$dir"/*.out; do
    [ ! -f "$pathname" ] && continue

    # do whatever you need to do to "$pathname" here
done

After the -f test, "$pathname" would point to a regular file or a symbolic link to a regular file. Note that hidden files would be skipped as * does not match filenames starting with a dot (unless the dotglob shell option is set in bash, which you may or may not be using).

With find:

find "$dir" -mindepth 1 -type d -prune -o -type f -name '*.out' -print

The -mindepth 1 causes the starting directory to not get pruned by -type d -prune. The -print should be replaced with the action that you would want to take on the found pathnames (which will be of regular files that has names ending with .out).

Or,

find "$dir" ! -path "$dir" -type d -prune -o -type f -name '*.out' -print

This only uses standard find options and prunes all directories that are not the same as the starting path.

Alternatively,

find "$dir" -maxdepth 1 -type f -name '*.out' -print

Using -maxdepth 1 would, more simply, stop find from descending into any of the start directory's subdirectories.

The -mindepth and -maxdepth options, although commonly available, are extensions to the standard find command, and if your implementation of find does not have them, you would have to go with the shell loop or the find alternative that uses -prune only (with -path).

1

The simplest way to acomplish that task (find files in a folder excluding all subdirectories) would be

find $dir -maxdepth 1 -type f -name '*.out' -o '.*.out'

as @StevenPenny and @RalfFriedlal have wrote the maxdepth option is what you are looking for.

From find manual: -maxdepth levels Descend at most levels (a non-negative integer) levels of directories below the starting-points. -maxdepth 0 means only apply the tests and actions to the starting-points themselves.

-type f : You are searching for regular files -name '*.out' -o '.*.out' : the -o means or and allows you to include in your search eventuali hidden files with the extension .out.

Sorry for my bad english :(

  • 1
    find would find hidden files without you having to do anything special. – Kusalananda Sep 20 '18 at 21:18
  • @Kusalanda: Sorry, you are right, my fault. There is not need to use -o '..out' – C. La Mosca Sep 20 '18 at 21:26
1

With zsh:

printf '%s\n' $dir/*.out(^/)  # files of any type except directory (excluding
                              # hidden ones)
printf '%s\n' $dir/*.out(-^/) # same but also excludes symlinks to directories
printf '%s\n' $dir/*.out(.)   # regular files only (excluding hidden ones)
printf '%s\n' $dir/*.out(-.)  # regular files or symlinks to regular files
                              # (excluding hidden ones)
printf '%s\n' $dir/*.out(D-.) # same, but include hidden ones.

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.