Use the FIND command non-recursively

Solved
Hello,
does anyone know how to use the find command without descending into the directory tree?

7 answers

  1. Moderator
    You use the option -maxdepth
     find . -name '*.c' -maxdepth 1 -type f 

    --> all the files in the current directory.

    Johan
    Gates vous a donné les fenêtres.
    GNU nous a donné toute la maison.
    15
    1. For AIX/Solaris/etc you can use -prune
      (I just saw that the answer has already been given..)
      0
      1. I bring up the subject from his grave in case someone comes across it during a search.
        In fact, there is a solution with prune:

        find . \( -type d ! -name . -prune \) -o -type f -print

        1) We indicate that directories that are not called "." are not traversed
        2) We request the display of files
        7
        1. Que Dieu te bénisse ;-)
          0
        2. You're welcome.
          0
        3. And otherwise, under AIX, you just need to use the ls command without filters (the limitation of ls only appears with filters).
          So ls -1 | grep TOTO to filter all files in the current directory that contain "TOTO"
          or ls -1 | awk '/^TOTO.*$/' to filter all files in the current directory that start with TOTO (etc...)
          => no recursion
          to delete (example with all files starting with TOTO:

          ls -1 | awk '/^TOTO.*$/{system("rm -f " $0)}'
          0
        4. Another example (that worked under HPUX)

          To delete all files older than 3 days in this directory....
          find . \( -type d ! -name . -prune \) -o -type f -mtime +3 -exec echo rm {} \; > cleanup.sh

          then simply run cleanup.sh after verification.
          0
        5. Thank you for bringing this solution back up. It has served me well (2011).
          0
      2. No, no, we can or we can't, but we must know that it only works under Linux, that's it.
        In principle, we should close this subject and consider it resolved.
        0
        1. oh, with some | and ls we should be able to make it, right?
          0
          1. Hello

            I have the same question but for AIX/Solaris/etc, that is to say not Linux!
            -[min/max]depth does not exist.

            Personally, I think it's impossible, but someone might have another opinion?
            0
            1. ben fais un man find pour plus d'infos su la commande find de linux.

              find dossier_ds_lequel_tu_cherches -name nom_du_fichier_a_chercher

              Bahan

              Close the world.||.txen eht nepO
              0
              1. Hey buddy.
                Since there are 15,000 lines of manual -> you are out of sync.
                0