+ Reply to Thread
Results 1 to 6 of 6

How to delete *.class files from all subdirectories?

  1. How to delete *.class files from all subdirectories?

    Hi,

    I forgot how to delete all *.class files (Java classes) from the current
    and all the subdirectories.

    I thought it is:
    rm -Rf *.class

    But to my surprise, it didn't work.

    Thank you.

  2. Re: How to delete *.class files from all subdirectories?

    www wrote:
    > Hi,
    >
    > I forgot how to delete all *.class files (Java classes) from the current
    > and all the subdirectories.


    find . -name "*.class" -type f -print0 | xargs -0 rm -f


    Janis

    >
    > I thought it is:
    > rm -Rf *.class
    >
    > But to my surprise, it didn't work.
    >
    > Thank you.


  3. Re: How to delete *.class files from all subdirectories?

    On Apr 17, 12:19 pm, www wrote:
    > Hi,
    >
    > I forgot how to delete all *.class files (Java classes) from the current
    > and all the subdirectories.
    >
    > I thought it is:
    > rm -Rf *.class
    >
    > But to my surprise, it didn't work.
    >
    > Thank you.


    find . -name "*.class"|xargs rm -rf


  4. Re: How to delete *.class files from all subdirectories?

    2007-04-17, 12:19(-04), www:
    > Hi,
    >
    > I forgot how to delete all *.class files (Java classes) from the current
    > and all the subdirectories.
    >
    > I thought it is:
    > rm -Rf *.class
    >
    > But to my surprise, it didn't work.

    [...]

    Once you know it's the shell, not rm that expands *.class into a
    list of file names, it shouldn't be a surprise anymore.

    Some shells (zsh and recent versions of ksh93 with the -G
    option) have recursive globbing:

    rm -- **/*.class

    But you could also use find.

    find . -name '*.class' -exec rm {} +

    But not that it will delete hidden files as well. If you don't
    want it:

    find . -name '*.class' ! -name '.*' -exec rm {} +

    Or if you do want to remove them with zsh:

    rm -- **/*.class(D)

    If you don't want to remove .class *directories*:

    rm -- **/*.class(^/)
    (there's no equivalent with ksh93 though)

    or

    find . -name '*.class' ! -type d -exec rm {} +

    --
    Stéphane

  5. Re: How to delete *.class files from all subdirectories?

    On Tue, 17 Apr 2007 12:19:03 -0400, www
    wrote:
    >
    >
    > Hi,
    >
    > I forgot how to delete all *.class files (Java classes) from the current
    > and all the subdirectories.
    >
    > I thought it is:
    > rm -Rf *.class
    >
    > But to my surprise, it didn't work.
    >
    > Thank you.


    find . -name '*.class' -exec rm {} +
    with some older versions of find:
    find . -name '*.class' | xargs rm

    rm -rf can delete a directory and all its contents. Unquoted wildcards
    are replaced by matching filenames in the current directory before the
    command is executed.


    --
    "Freedom is still the most radical idea of all."
    -- Nathaniel Branden

  6. Re: How to delete *.class files from all subdirectories?

    2007-04-17, 16:59(+00), Bill Marcum:
    [...]
    > find . -name '*.class' -exec rm {} +
    > with some older versions of find:
    > find . -name '*.class' | xargs rm

    [...]

    But xargs is not meant to process find's output (unless you use
    the non-standard -0 and find's -print0).

    xargs expects its input in a very specific format, and that's
    not a newline separated list as in find's output. Anyway,
    strictly speaking is not post-processable as nothing prevents a
    file path to contain a newline character itself.

    So:

    find . -name '*.class' | xargs rm

    only works in the very specific case where none of the file
    paths contain any space, tab, newline, backslash, single or
    double quote characters.

    So it's OK for a one of if you know the file names don't contain
    such characters. But that's certainly not a command line you
    should put in scripts for instance.

    --
    Stéphane

+ Reply to Thread