find and remove multiple file using linux command - linux

I have a directory named classes which contains a lot of sub-directories -
classes
|-security
|-registration
|-service
....
Each of these directory contains a lot of java files and their compiled classes files. I want to remove all the class file.
Going to classes directory I can list out all the class file using find command -
$ find . -name *.class
Is there any command in linux to remove all the classes file under the classes directory.

The usual answer uses the -exec option of find:
find . -name "*.class" -exec rm {} \;
Be sure to quote the wildcard, to ensure that it is passed into find (rather than globbed by the shell, first).
For further discussion, see these questions:
Command line: piping find results to rm
Linux why can't I pipe find result to rm?

Use xargs with pipe lining -
$ find . -name *.class | xargs rm *

Related

Copy specific files recursively

This problem has been discussed extensively but I couldn't find a solution that would help me.
I'm trying to selectively copy files from a directory tree into a specific folder. After reading some Q&A, here's what I tried:
cp `find . -name "*.pdf" -type f` ../collect/
I am in the right parent directory and there indeed is a collect directory a level above. Now I'm getting the error: cp: invalid option -- 'o'
What is going wrong?
To handle difficult file names:
find . -name "*.pdf" -type f -exec cp {} ../collect/ \;
By default, find will print the file names that it finds. If one uses the -exec option, it will instead pass the file names on to a command of your choosing, in this case a cp command which is written as:
cp {} ../collect/ \;
The {} tells find where to insert the file name. The end of the command given to -exec is marked by a semicolon. Normally, the shell would eat the semicolon. So, we escape the semicolon with a backslash so that it is passed as an argument to the find command.
Because find gives the file name to cp directly without interference from the shell, this approach works for even the most difficult file names.
More efficiency
The above runs cp on every file found. If there are many files, that would be a lot of processes started. If one has GNU tools, that can be avoided as follows:
find . -name '*.pdf' -type f -exec cp -t ../collect {} +
In this variant of the command, find will supply many file names for each single invocation of cp, potentially greatly reducing the number of processes that need to be started.

How to send files from folder recursively to command in linux console?

I want to generate docs for coffee-script files. I want to use Docco.
When i use:
docco client/coffee/*
it throws error. I think because folders are in file list.
When i use:
docco client/coffee/*.coffee
it cant' find some files, because i havent anithing in root folder.
How to give all *.coffee files recursievly to command in console?
There are several ways to do it
$ find client/coffee/ -name '*.coffee' -exec docco {} +
$ find client/coffee/ -name '*.coffee' | xargs docco
However, note that the latter way does not work if there is space in file name, unless you use find -print0 with combination of xargs -0.
Additionally, if you are using bash, you can use **/*.coffee with setting shopt -s globstar

Bash script to recursively step through folders and delete files

Can anyone give me a bash script or one line command i can run on linux to recursively go through each folder from the current folder and delete all files or directories starting with '._'?
Change directory to the root directory you want (or change . to the directory) and execute:
find . -name "._*" -print0 | xargs -0 rm -rf
xargs allows you to pass several parameters to a single command, so it will be faster than using the find -exec syntax. Also, you can run this once without the | to view the files it will delete, make sure it is safe.
find . -name '._*' -exec rm -Rf {} \;
I've had a similar problem a while ago (I assume you are trying to clean up a drive that was connected to a Mac which saves a lot of these files), so I wrote a simple python script which deletes these and other useless files; maybe it will be useful to you:
http://github.com/houbysoft/short/blob/master/tidy
find /path -name "._*" -exec rm -fr "{}" +;
Instead of deleting the AppleDouble files, you could merge them with the corresponding files. You can use dot_clean.
dot_clean -- Merge ._* files with corresponding native files.
For each dir, dot_clean recursively merges all ._* files with their corresponding native files according to the rules specified with the given arguments. By default, if there is an attribute on the native file that is also present in the ._ file, the most recent attribute will be used.
If no operands are given, a usage message is output. If more than one directory is given, directories are merged in the order in which they are specified.
Because dot_clean works recursively by default, use:
dot_clean <directory>
If you want to turn off the recursively merge, use -f for flat merge.
dot_clean -f <directory>
find . -name '.*' -delete
A bit shorter and perform better in case of extremely long list of files.

How does one find and copy files of the same extension, in different directories, to a single directory in linux?

So, How Do I find and copy all files,
*.a
that are in,
~/DIR{1,2,3,...}
to
~/tmp/foo?
Assumed you meant recursively copy everything of type .a from some source location.
Haven't verified yet, but this should do that.
find <root-of-search> -type f -name '*.a' -exec cp {} /tmp/foo \;
replace with the top of wherever you want to search from. You might have to throw quotes around *.a, and you might have to replace escape the ending semicolon by putting it in single quotes rather than back-slashing it.
In a bash shell:
cp ~/DIR*/*.a ~/tmp/foo
find ~/DIR{1,2,...} -name *.a print0 | xargs -i -0 cp '{}' ~/tmp/foo

linux to compile multiple java file

here is my directory structure.
/user/a
/user/b
/user/b
inside folder a,b,c there is a file person.java (it is the Same file, just a one line modification.
now, on my shell, im on my /user/ directory and i try to do
javac */person.java
the shell returns the following error,
person.java:14: duplicate class: person
Is there anything to resolve this?
I think the problem here might be, that javac tries to compile everything in one go, which naturally results in duplicated class definitions.
A simple way to resolve this would be
find . -name '*.java' -exec javac {} \;
Edit:
Or to be more precise find . -name 'person.java' -maxdepth 2 -exec javac {} \;
I would go for the small shell script:
for f in */person.java; do
javac $file
done
First line find all the files person.java in a sub-directory, second line compile the file.

Resources