Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Remove some files recursively

 
Anoop Krishnan
Ranch Hand
Posts: 163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a directory ../classes under which i have a lot of subdirectories
../classes/com/financial/argon
../classes/com/financial/argon/util
../classes/com/orion/util
...............
............
All these directories contains java & class files now i want to remove all the java files which is any where in this directory structure by giving a command from ../classes
I tried as follows rm -Rf *.java & rm -rf *.java but it doesn't work
 
Ryan Senior
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you would like to delete a directory and all of it's subdirectories, type rm -rf <directory>
so if i wanted to delete my entire usr directory, I would type
rm -rf /usr
be careful, this isn't windows, if you type rm -rf /, it will delete everything without warning because -f means force (no matter what)
post another message if that doesn't do the trick
 
Amol Dharmadhikari
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anoop,
I dont know whether it can be done by a single unix command. But the alternative is to write a simple shell/PERL script which takes the directory name, goes through all the files/directories which lie below it, and delete those having extention '.java'
Amol
 
Tim Holloway
Saloon Keeper
Pie
Posts: 18214
53
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
find . -name "*.java" -print0 | xargs -0r rm -f
The "find" command is a treasure too little known. I got the above text by doing "info find", selecting "common tasks" then "cleaning up". Actually, I think I have been getting away with a shorter version:
find . -name "*.java" | xargs rm -f
But I'd test that someplace harmless, first.
 
Roseanne Zhang
Ranch Hand
Posts: 1953
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I use ant build in task "Delete" to do the same, and it is platform independent.

However, Tim's way works great!!! Learn something new everyday. Thanks a lot!

SCJD Study Group has been moved to http://www.developergroup.org/
[This message has been edited by Roseanne Zhang (edited September 29, 2001).]
 
Eric Handbury
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I wrote a java program that recursively goes through sub-directories deleting files of a user-defined suffix. I use it to clear out old .class files. I can send it to you if you post your e-mail address.
Eric.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic