Ask Ubuntu is a question and answer site for Ubuntu users and developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

This is for a java project, but the same concept can be applied more generally:

Basically, I have a projects with all *.java files located in some sub directory of src. Now I want to grab all directories with the name test in that directory tree and move them into a new directory called tests, e.g.:

          -> test



How would I best do that?

share|improve this question
Does it need to create the parent directories tests/com and tests/com/a1 for you? – gertvdijk Dec 17 '12 at 16:28
not tests/ but the others, yes. That's why said it has to keep directory structure - that is, it has to create them if they aren't already there – Cubic Dec 17 '12 at 16:36
are there other files, or just the *.java ones? – philshem Dec 17 '12 at 17:10
is there some reason that you can't just drag and drop in the file browser? – ImaginaryRobots Dec 17 '12 at 18:11
@ImaginaryRobots Other than that that won't do what I asked? No. – Cubic Dec 17 '12 at 18:46

Please note that the meanings of move mv and copy cp are distinct and well defined for terminal usage. mv means that the files will no longer exist in the old location, while cp means that a duplicate is made in the new location.

Simple solution (notice that I use mv, but you may want to use cp first and then remove the old files once the copying is what you want):

cp src/com/a1/test/test*.java tests/com/a1/test/

More elegant solution (this one moves the files, not copies):

cd src/com/
find . -iname "test*.java" -type d -exec mv {} ../../tests/com/ \;

If the second solution doesn't work for your specific test case, we can iterate...

share|improve this answer
This doesn't do what I asked. I need to move ALL test folders from ALL sub folders to tests, keeping the directory structure. – Cubic Dec 17 '12 at 18:47
In order to find all the files in "test" folders, run this command: find . -type d -name "test" -exec ls -l {} \; Use the type -d flag after the dot to search only directories and the type -f flag for files. You can figure out how to move the files based on the above answer. – philshem Dec 17 '12 at 19:26

You'll need to write a shell script for this task. I'll give it a try and comment the lines.

The first line here is just a variable where you put the absolute directory of your src tree.


The next line executes find. The command will put every directory path with the name test into the variable TESTDIRS.

TESTDIRS=$(find $SRCDIR -type d -name test)

Now you iterate over all found test directories

for dir in $TESTDIRS; do

This takes the actual path and removes everything including src. So you get the structure for creating the new path.


Now create the new directories.

  mkdir -p tests/$NEWDIR

Move the old test directory to the newly created place.

  mv $dir tests/$NEWDIR

end the iteration


As far as I understand you this should what you want. But please test it first. There might be some caveats and running the script as is might lead to data loss.

share|improve this answer

I use one these commands to copy all my jpg files and skip all the db, raw etc files. Either one should work for your needs.

Using rsync:

 rsync -a --prune-empty-dirs --include '*/' --include '*.jps' --exclude '*'  source/ target/

Or find with exec:

find . -name '*.jpg' -exec cp --parents \{\} /target \;
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.